### number guessing game help

when I run my code it wont show the higher or lower part after 2 guesses any help would be appreciated, if I can figure out the first loop I will be able to finish the rest of the loops.

 ``12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485`` `````` Put the code you need help with here. #include #include #include #include #include using namespace std; void flush(); /*pick a number game where the user gets 4 chances to get the number right and * the user gets 3 different levels to chose from. * */ int main(int argc, char** argv) { // function type , arguments,/ int number; // store assigned number int guess; // store guess int guess_lim = 4; // limits number of guess int (rand() % 10) + 1; // number generator string answer; bool go = true; int guess_count = 1; //to keep count of guess cout << " Welcome we are going to play a number guessing game today!!! \n" " you pick what level of difficulty you want between 1 and 3. \n" " 1 being the easiest and 3 the hardest!! \n\n" " You have four chances to get this right! \n"; cin >> number; flush(); if (number < 1 || number > 3) { // to make sure proper choices are being used from user or it will go back and ask again and to assign difficulty level/ go = false; cout << "Thats is not an offered option\n "; } srand(time(NULL)); do { switch (number) { case 1:do { cout << "Guess a number between 1-10\n\n"; number = (rand() % 10) + 1; // to generate a random number between 1 and 10/ cin >> guess; flush(); if (guess <= 10 && guess > 0) { // to make sure proper choices are being used from user or it will go back and ask again/ go = false; if (guess > number) { cout << "Too high!\n"; cin >> guess; flush(); guess_count++; } if (guess < number) { cout << "Too low!\n"; cin >> guess; flush(); guess_count++; } else if (guess = number);{ cin >> guess; flush(); cout << "\nCorrect! You got it in " << guess_count << " guesses!\n";//to show how many guesses it took cin >> guess_count; flush();} if (guess_count = 4); { // if guess count gets to 4 ends program cout << "you lose out of turns!\n"; } return 0; } } while (go); ``````
I tried running your program from the code you provided. There were some syntax errors. Also what do you want your flush function to do? Here are some minor corrections to your code that i managed to do.
 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687`` ``````#include #include #include #include #include using namespace std; void flush(); /*pick a number game where the user gets 4 chances to get the number right and * the user gets 3 different levels to chose from. * */ int main(int argc, char** argv) { // function type , arguments,/ int number; // store assigned number int guess; // store guess int guess_lim = 4; // limits number of guess int (rand() % 10); // number generator string answer; bool go = true; int guess_count = 1; //to keep count of guess cout << " Welcome we are going to play a number guessing game today!!! \n" " you pick what level of difficulty you want between 1 and 3. \n" " 1 being the easiest and 3 the hardest!! \n\n" " You have four chances to get this right! \n"; cin >> number; if (number < 1 || number > 3) { // to make sure proper choices are being used from user or it will go back and ask again and to assign difficulty level/ go = false; cout << "Thats is not an offered option\n "; } srand(time(NULL)); do { switch (number) { case 1: do { cout << "Guess a number between 1-10\n\n"; number = (rand() % 10) + 1; // to generate a random number between 1 and 10/ cin >> guess; flush();// if (guess <= 10 && guess > 0) // to make sure proper choices are being used from user or it will go back and ask again/ go = false; if (guess > number) cout << "Too high!\n"; cin >> guess; flush(); guess_count++; if (guess < number) cout << "Too low!\n"; cin >> guess; flush(); guess_count++; if (guess = number) cout << "\nCorrect! You got it in " << guess_count << " guesses!\n";//to show how many guesses it took (should only have this portion because you don't need to enter in a count or guess since you have gotten the number already) if (guess_count = 4) // if guess count gets to 4 ends program cout << "you lose out of turns!\n"; } while (!go)//needs ! because it will repeat if go is false break;//assuming this is where you would end difficulty 1 so break would be needed around here } }//will need another while statement return 0;//return needs to be down here because it will break out of your function before you get to your while statement ``````

for your false statements you can nest a do while or just a while statement here is an example for your code at line 33

 ``12345`` ``````cin>>number while (while number<1 || number > 3) {cout<<"Invalid Difficulty Chosen\n"<<"Please Choose A Value Between 1-3: "; cin>>number; }``````
Last edited on
Line 19 makes no sense and is redundant.
You never seed the random number generator.
If you did seed it, you'd be generating random numbers between guesses.
Your switch statement is unnecessarily hardcoded, but I can see where you were going with it.

Take a look at the number guessing game I slapped together:
 ``12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849`` ``````#include #include #include #include void flush(); const unsigned maxGuesses=4; int random(int min, int max) { static bool bRandom=false; if(bRandom==false) { srand(time(0)); bRandom=true; } return (min+(rand()%max)); } int main() { bool winFlag=false; unsigned input; std::cout << "We're going to play a number guessing game today!" << std::endl; std::cout << "You've only got four guesses!" << std::endl; std::cout << "Choose a difficulty setting (1-3): "; std::cin >> input; flush(); unsigned max=input*10; int number=random(1, max); for(unsigned i=0; i> input; flush(); if(input==number) { winFlag=true; break; } std::string output=(input

Same functionality as yours, without the input checking.
thank you guys I am new at this and having a hell of a time with it is just part of the code that's giving me a hard time I will try to post up the whole code
this is using netbeans
Topic archived. No new replies allowed.