### Simple solution, major frustration

 ``12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788`` ``````#include #include #include #include using namespace std; int main(){ //Variable declaration string name(" "); int round, cntTries = 0; int guessSelection = 0; char again = 'y'; again = toupper(again); //Random number generator srand(time(0)); int high = 100; int low = 0; //Output + selection process cout << "Guess the number game" << endl; cout << "\nPlease enter your name" << endl; getline(cin, name); cout << "Hello " << name << endl; cout << "Would you like me to guess your number or would you like to have a shot\n" << "at guessing my number?" << endl; cout << "(For your number type '1' :: For my number type '2')" << endl; cin >> guessSelection; //Loops of selection //Selection = 1 while(again=='y'){ if(guessSelection == 1){ int myNum = 0; int compGuess = 0; char answer = 'n'; cout << "Please enter your number on a scale of 1-100 using integers only\n" << "No decimal values allowed (type -1 at anytime to exit)" << endl; cin >> myNum; compGuess=(1+rand()%100); if(myNum > 100 || myNum < 0){ cout << "You have entered a value too high or too low.. Must be between 1 & 100" << endl; } while(answer == 'n' || answer == 'N' && compGuess != myNum){ cout << "Is this your number? " << compGuess << endl; cin >> answer; if(answer == 'n' || answer == 'N'){ if(compGuess > myNum){ cout << "Too high.. " << endl; high = compGuess; compGuess = rand() % (high - low + 1) - low; } else if(compGuess < myNum){ cout << "Too low.. " << endl; low = compGuess; rand()%(high - low + 1) + low; } } } } //Selection = 2 if(guessSelection == 2){ int myGuess=0; int compNum=0; compNum=(1+rand()%100); cout << "You have chosen to guess my number.. Have at it!" << endl; cout << "Make a guess.. (1-100)" << endl; cin >> myGuess; while(myGuess!=compNum){ cout << "Keep guessing.. " << endl; cin >> myGuess; if(myGuess == compNum){ cout << "You guessed it!" << endl; } if(myGuess > compNum){ cout << "Too high, try again." << endl; } else if(myGuess < compNum){ cout << "Too low, try again." << endl; } } } //Selection = something other than 1 or 2 else{ cout << "You've made an invalid selection.." << endl; } cout << "Would you like to play again? y or n" << endl; cin >> again; } } ``````

Whenever I run the program, it's fine until I get to the point where it asks which game they want to play. if I select 1, it'll ask for my number, if I give a number bigger than 30, the program quits. I didn't program it to do that. If I pick a number 30 or below, it'll run it fine, but if the number the computer guesses is too low, it'll repeat that same number over and over and not change. Also, if I select option 2, the program will quit.

example of 1st issue:
Select number: 25
too high: 45
too high: 30
too high: 15
too low: 15
too low: 15
.....
Last edited on
Edited my code: It works, to an extent. I can't exit my loop with -1
 ``1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889`` ``````#include #include #include #include using namespace std; int main(){ //Variable declaration string name(" "); int round, cntTries = 0; int guessSelection = 0; char again = 'y'; again = toupper(again); //Random number generator srand(time(0)); int high = 100; int low = 0; //Output + selection process cout << "Guess the number game" << endl; cout << "\nPlease enter your name" << endl; getline(cin, name); cout << "Hello " << name << endl; cout << "Would you like me to guess your number or would you like to have a shot\n" << "at guessing my number?" << endl; cout << "(For your number type '1' :: For my number type '2')" << endl; cin >> guessSelection; //Loops of selection //Selection = 1 do{ if(guessSelection == 1){ int myNum = 0; int compGuess = 0; char answer = 'n'; cout << "Please enter your number on a scale of 1-100 using integers only\n" << "No decimal values allowed (type -1 to exit)" << endl; cin >> myNum; compGuess=(1+rand()%100); if(myNum > 100){ cout << "You have entered a value too high.. Must be between 1 & 100" << endl; } while(answer == 'n' || answer == 'N' && compGuess != myNum){ cout << "Is this your number? " << compGuess << endl; cin >> answer; if(answer == 'n' || answer == 'N'){ if(compGuess > myNum){ cout << "Too high.. " << endl; high = compGuess; compGuess = rand() % (high - low + 1) + low; } else if(compGuess < myNum){ cout << "Too low.. " << endl; low = compGuess; compGuess = rand() % (high - low + 1) + low; } } } } //Selection = 2 if(guessSelection == 2){ int myGuess=0; int compNum=0; compNum=(1+rand()%100); cout << "You have chosen to guess my number.. Have at it!" << endl; cout << "Make a guess.. (1-100)" << endl; cin >> myGuess; while(myGuess!=compNum){ cout << "Keep guessing.. " << endl; cin >> myGuess; if(myGuess == compNum){ cout << "You guessed it!" << endl; } if(myGuess > compNum){ cout << "Too high, try again." << endl; } else if(myGuess < compNum){ cout << "Too low, try again." << endl; } } } //Selection = something other than 1 or 2 else{ cout << "You've made an invalid selection.." << endl; } cout << "Would you like to play again? y or n" << endl; cin >> again; } while(again == 'y' || again == 'Y'); } ``````
 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293`` ``````#include #include #include #include using namespace std; int main(){ //Variable declaration string name(" "); int round, cntTries = 0; int guessSelection = 0; char again = 'y'; again = toupper(again); //Random number generator srand(time(0)); int high = 100; int low = 0; //Output + selection process cout << "Guess the number game" << endl; cout << "\nPlease enter your name" << endl; getline(cin, name); cout << "Hello " << name << endl; cout << "Would you like me to guess your number or would you like to have a shot\n" << "at guessing my number?" << endl; cout << "(For your number type '1' :: For my number type '2')" << endl; cin >> guessSelection; //Loops of selection //Selection = 1 do{ if(guessSelection == 1){ int myNum = 0; int compGuess = 0; char answer = ' '; cout << "Please enter your number on a scale of 1-100 using integers only\n" << "No decimal values allowed (type -1 to exit)" << endl; cin >> myNum; compGuess=(1+rand()%100); if(myNum > 100){ cout << "You have entered a value too high.. Must be between 1 & 100" << endl; cout << "Again?" << endl; cin >> again; } cout << "Is this your number? " << compGuess << endl; cin >> answer; while(answer == 'n' || answer == 'N' && compGuess != myNum){ cout << "What about this: " << compGuess << endl; cin >> answer; if(compGuess > myNum){ cout << "Too high.. " << endl; high = compGuess; compGuess = rand() % (high - low + 1) + low; } else if(compGuess < myNum){ cout << "Too low.. " << endl; low = compGuess; compGuess = rand() % (high - low + 1) + low; } } } //Selection = 2 if(guessSelection == 2){ int myGuess=0; int compNum=0; compNum=(1+rand()%100); cout << "You have chosen to guess my number.. Have at it!" << endl; cout << "Make a guess.. (1-100)" << endl; cin >> myGuess; while(myGuess!=compNum){ cout << "Keep guessing.. " << endl; cin >> myGuess; if(myGuess == compNum){ cout << "You guessed it!" << endl; } if(myGuess > compNum){ cout << "Too high, try again." << endl; } else if(myGuess < compNum){ cout << "Too low, try again." << endl; } } } //Selection = something other than 1 or 2 if(guessSelection > 2){ cout << "You've made an invalid selection.." << endl; } cout << "Would you like to play again? y or n" << endl; cin >> again; } while(again == 'y'); } ``````

Progressing slightly, but now I'm truly at a stalemate. I've spent ~ 35 - 40 min trying different solutions to my problem, and now I'm stuck.

I can't get myNum > 100 to actually work correctly, it'll pretend I entered a number and computer starts guessing.
I also can't get the computer to properly guess, say he guess 22 and it says too low, there's a chance he'll guess 22 again either later, or directly after guessing 22..

Please, any help.. at all.. Test the code out for yourself, and let me know what ya think, what a solution, anything. Not homework, just projects during class break.
bump..
Make it so you can't enter bellow 1 or above 100 so a glitch doesn't happen.
(This won't solve your problem, just make it more user friendly.)
Last edited on
Okay, I got it. It took awhile but it works. I tested it like 300 times. Here is the code: (the underlined stuff is what I added and edited.)

 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126`` ``````#include #include #include #include using namespace std; int used[100]; int main(){ //Variable declaration string name(" "); int round, cntTries = 0; int guessSelection = 0; char again = 'y'; again = toupper(again); //Random number generator srand(time(0)); int high = 100; int low = 0; //Output + selection process cout << "Guess the number game" << endl; cout << "\nPlease enter your name" << endl; getline(cin, name); cout << "Hello " << name << endl; cout << "Would you like me to guess your number or would you like to have a shot\n" << "at guessing my number?" << endl; cout << "(For your number type '1' :: For my number type '2')" << endl; cin >> guessSelection; //Loops of selection //Selection = 1 do{ if(guessSelection == 1){ int myNum = 0; int compGuess = 0; char answer = ' '; cout << "Please enter your number on a scale of 1-100 using integers only\n" << "No decimal values allowed (type -1 to exit)" << endl; cin >> myNum; compGuess=(1+rand()%100); if(myNum > 100){ ///////////////////////////////////////////| //////////////////////What I added/eddited V for (int a=0; a>101; a++){ used[a]=0; } cout << "You have entered a value too high.. Must be between 1 & 100" << endl; cout << "Again?" << endl; cin >> again; } answer = 'n'; int number = 0; int clear = 1; int dc; while(answer == 'n' || answer == 'N' && compGuess != myNum){ number++; while (clear == 0){ dc = 1; for (int a=0; a<101; a++){ if (compGuess == used[a]){ if(compGuess > myNum){ compGuess = rand() % (high - low + 1) + low; dc = 5; } else if(compGuess < myNum){ compGuess = rand() % (high - low + 1) + low; dc = 5; } } } if (dc == 5){ clear = 0;} else goto next; } next: clear = 0; used[number] = compGuess; cout << "What about this: " << compGuess << endl; cin >> answer; if(compGuess > myNum){ cout << "Too high.. " << endl; high = compGuess; } else if(compGuess < myNum){ cout << "Too low.. " << endl; low = compGuess; } number++; used[number] = compGuess; } } ///////////////////////////////////////////^ //////////////////////What I added/eddited | //Selection = 2 if(guessSelection == 2){ int myGuess=0; int compNum=0; compNum=(1+rand()%100); cout << endl<< compNum << endl; cout << "You have chosen to guess my number.. Have at it!" << endl; cout << "Make a guess.. (1-100)" << endl; cin >> myGuess; while(myGuess!=compNum){ cout << "Keep guessing.. " << endl; cin >> myGuess; if(myGuess == compNum){ cout << "You guessed it!" << endl; } if(myGuess > compNum){ cout << "Too high, try again." << endl; } else if(myGuess < compNum){ cout << "Too low, try again." << endl; } } } //Selection = something other than 1 or 2 if(guessSelection > 2){ cout << "You've made an invalid selection.." << endl; } cout << "Would you like to play again? y or n" << endl; cin >> again; } while(again == 'y'); }``````

Try it. I hope it works.
Last edited on
One error whit what I just posted. If the user enters the number '0' for the computer to guess it glitches. Make it so it says you can't enter bellow 1.
I'll try it out tonight! Thanks for the reply!
My only issue is.. When I enter a number greater than 100, it'll still continue on with the game as if I entered something below 100.
Just use `if` to tell it not to go above or bellow those numbers.
Topic archived. No new replies allowed.