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.
#include <iostream>
#include <ctime> //Allows for random number
#include <cstdlib>
usingnamespace 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
}
elseif (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;
}
elseif (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;
}
elseif (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;
}
elseif (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;
}
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.