### Hi-Low Game

Hi, my name is Mike and this is my first question on cplusplus.com. I am a beginner that barely knows the basics. I am writing a simple High/Low Game where the user guesses whether a random number will be high or low. I have noticed through trial and error, that when the random number is over 7 (8,9,10,11,12,13) the program does not display what is suppose to happen. Although, if the number is less than 7 (6,5,4,3,2,1) it displays everything correctly. I believe it has something to do with the conditional statements but I've have not figured it out. Thanks for any help in advance.

 ``1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889`` ``````#include #include //Allows for random number #include using namespace std; int main() { int points = 1000; // Original Value int addpoints; int bet; int prediction; int randomnumber; char again; while (again!='n'&&points>0) //will play game until user does not want to play or points = 0 { cout << "You have " << points << " points" << endl; //Displays amount of points and ask user for bet cout << "How many points to risk?? "; cin >> bet; while(bet>points) { cout << "You don't have " << bet << " points!!!\n"; bet-=bet; //Cant bet more than amount of points the user has cout << "How many points to risk?? "; cin >> bet; } cout << "Predict ( 1=High , 0=Low ) "; // High/Low Prediction cin >> prediction; srand (time(NULL)); //generates seed for random number randomnumber = rand() % 13 + 1; //generates random number 1-13 cout << randomnumber; if (randomnumber==7) //if random number = 7 the user only loses bet { cout << "\nNumber is 7" << endl; cout << "You only lose your bet" << endl; bet-=bet; // resets bet back to 0 } else if (prediction==1&&randomnumber<=6) //Guess High and number low = Lose { cout << "Number is " << randomnumber << endl; cout << "You Lose." << endl; points-=bet; //bet is subtracted from points bet-=bet; } else if (prediction=0&&randomnumber>7&&randomnumber<=13) // Guess Low and number high = Lose { cout << "Number is " << randomnumber << endl; cout << "You Lose." << endl; points-=bet; bet-=bet; } else if (prediction==1&&randomnumber>7&&randomnumber<=13) // Guess High and number high = Win { cout << "Number is " << randomnumber << endl; cout << "You Win." << endl; addpoints=bet*2; points+=addpoints; bet-=bet; } else if (prediction==0&&randomnumber<=6) // Guess Low and number low = Win { cout << "Number is " << randomnumber << endl; cout << "You Win." << endl; addpoints=bet*2; //Doubles bet points+=addpoints; //adds doubled bet to points bet=bet-bet; } cout << "Play again??(y or n) "; cin >> again; // asks user to play again system ("CLS"); // clear screen } cout << "Final Amount = " << points; // if user stops playing or points = 0 if (points==0) { cout << "You ran out of points!!!!!!"; } return 0; }``````
@mickeraiello

Line 51 should have double equals also, as you have on lines 38, 44, 58 and 66.

Also, you should move the srand, line 33, to before the start of the while loop, line 16. It doesn't need to be called more than once.
Last edited on
OMG!!!!! That is such a silly mistake. I've looked over the code so many times and couldn't find that little error. Thanks for pointing it out. I appreciate the help.
@mickeraiello

No problem. Just FYI, we all do little mistakes like that. It's just a part of programming glithes.
Topic archived. No new replies allowed.