I have a new book and i am about 1/5 through it. It had an example code for a guess the number game and then for exercises it asked you to make a program that made the computer guess a number you enter.
I kinda had to get creative with it but i don't know if i did exactly what the book asked. Is this good? Tell me if there is a way to do this more efficiently.
[code] #include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
int main() {
srand(time(0));
int winningnumber;
cout << "enter a number and the computer will guess it!\n";
cin >> winningnumber;
int tries = 0, guess;
do {
cout << "Guess a fucking number already\n";
guess = rand() % 100 + 1;
if(guess < winningnumber) {
cout << "god you are such a fucking wanker, TOO LOW!\n\n";
tries++;
}
if(guess > winningnumber) {
cout << "why do you fail at life? WHY DID I DO YOUR WIFE? because too high!\n\n";
tries++;
}
} while(guess != winningnumber);
cout << "OMG i think you fucking won, BIG WHOOP\n";
cout << "It took you " << tries << " tries to do this shit, kill yourself\n\n\n\n\n\n";
system("pause");
return 0;
}
[/code]
If you enter a negative number or a number larger than 100 then you'll loop forever, never guessing the right number.
And your program is quite bad at guessing, he'll try guessing with numbers lower than the last one even though he was told it was too low...
And don't use system("pause"). Just don't. (I'm so tired of explaining it, I'll leave that to someone else)
cin.get(); , i realize. But for some reason when i make the exe it doesn't recognize cin.get().
It runs right through the program even if i enter cin.get();
It's really annoying.
Flush the buffer?
Can you translate that into first grader language? I don't understand what "Flushing the buffer" means :/
[QUOTE=Helpful stalker;18812156]Can you translate that into first grader language? I don't understand what "Flushing the buffer" means :/[/QUOTE]
It means taking a shit
[QUOTE=r4nk_;18812255]It means taking a shit[/QUOTE]
Yeah, that's a pretty good analogy.
The buffer is your colon, and all the characters you entered from the keyboard is the corn you ate earlier.
But the analogy breaks down (or just gets a little weird) once you explain what the colon and the shit is for: Normally, you'd want to take a little shit at a time if it is possible to do so, and have the program carefully inspect the corn nibblets it finds in the shit. But flushing the buffer is just shitting everything you got into the toilet and wiping afterwards. That shit will not be carefully inspected by your program.
best analogy ever
That sounds good but i still don't know what to do O.O
anyways i made a better program.
[code]#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
int main() {
srand(time(0));
int winningnumber;
cout << "enter a number and the computer will guess it!\n";
cin >> winningnumber;
int tries = 0, guess;
guess = rand() % 100 + 1;
do {
cout << "Guess a fucking number already\n";
if(guess < winningnumber) {
cout << "god you are such a freaking wanker, TOO LOW!\n\n";
tries++;
guess++;
}
if(guess > winningnumber) {
cout << "why do you fail at life? WHY DID I DO YOUR WIFE? because too high!\n\n";
tries++;
guess--;
}
} while(guess != winningnumber);
cout << "OMG i think you fucking won, BIG WHOOP\n";
cout << "It took you " << tries << " tries to do this shit, kill yourself\n\n\n\n\n\n";
cin.get();
return 0;
}
[/code]