Number Guessing Game Help
Mar 3, 2012 at 3:48am UTC
Below is my code for a number guessing game. The problem seems to be that if you guess the number on the first try it doesn't display "Phenomenal! You got it on your first try!" but instead skips to the next level. It wont generate a random number in this code but the number, the computer is always 'thinking of' is 1 so u can test it. Ty and please help
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231
#include<iostream>
using namespace std;
int main()
{
srand(time(0));
string m;
int number;
int guess, maxrand, j;
char playAgain='y' ;
int c;
cout << "** Guess The Number Game **\n" ;
cout << "The goal of this game is to guess the number I am thinking\n" ;
while (playAgain !='n' )
{
cout <<"Select Mode:" <<endl;
cout <<"" <<endl;
cout <<"Level Mode (l)" <<endl;
cout <<"Practice Mode (p)" <<endl;
cout <<"" <<endl;
cin >> m;
cout <<"" <<endl;
if (m == "l" )
{
j = 25;
int q = 0;
do {
++q;
cout <<"*** Level " <<q<<" ***" <<endl;
cout <<"" <<endl;
if (q==1)
{
cout <<"Stats:" <<endl;
cout <<"" <<endl;
cout <<"Max Tries : 10" <<endl;
cout <<"Range : 1 - 25" <<endl;
cout <<"" <<endl;
cout <<"Chance of Winning Level: 40% (10 is 40% of 25)" <<endl;
cout <<"Chance of Losing Level: 60% (25-10=15. 15 is 60% of 25)" <<endl;
cout <<"Chance of Guessing Right Number: 4% (1 is 4% of 25)" <<endl;
cout <<"Chance of Guessing Wrong Number: 96% (25-1=24. 24 is 96% of 25)" <<endl;
cout <<"Probability of Winning: Likely" <<endl;
cout <<"" <<endl;
}
if (q==2)
{
cout <<"Stats:" <<endl;
cout <<"" <<endl;
cout <<"Max Tries : 10" <<endl;
cout <<"Range : 1 - 50" <<endl;
cout <<"" <<endl;
cout <<"Chance of Winning Level: 20% (10 is 20% of 50)" <<endl;
cout <<"Chance of Losing Level: 80% (50-10=40. 40 is 80% of 50)" <<endl;
cout <<"Chance of Guessing Right Number: 2% (1 is 2% of 50)" <<endl;
cout <<"Chance of Guessing Wrong Number: 98% (50-1=49. 49 is 98% of 50)" <<endl;
cout <<"Probability of Winning: Probably" <<endl;
cout <<"" <<endl;
}
if (q==3)
{
cout <<"Stats:" <<endl;
cout <<"" <<endl;
cout <<"Max Tries : 10" <<endl;
cout <<"Range : 1 - 100" <<endl;
cout <<"" <<endl;
cout <<"Chance of Winning Level: 10% (10 is 10% of 100)" <<endl;
cout <<"Chance of Losing Level: 90% (100-10=90. 90 is 90% of 100)" <<endl;
cout <<"Chance of Guessing Right Number: 1% (1 is 1% of 100)" <<endl;
cout <<"Chance of Guessing Wrong Number: 99% (100-1=99. 99 is 99% of 100)" <<endl;
cout <<"Probability of Winning: Unlikly" <<endl;
cout <<"" <<endl;
}
int tries = 0;
cout<<"Im thinking of a number between 1-" <<j<<". Take a guess (10 tries only): " ;
number=1;
cin>>guess;
++tries;
cout <<" " <<endl;
cout <<" " <<endl;
while (guess!=number)
{ if (tries > 10)
{ cout <<"" <<endl;
cout <<"GAME OVER!" <<endl;
cout <<"You had 10 tries and could not guess the number!" <<endl;
cout <<"" <<endl;
cout <<"Btw the number was " <<number<<"!" <<endl;
guess = number;
q = 3;}
if (tries < 10)
{
do {
if (guess>j)
{cout <<" " <<endl;
cout <<"Number is only 1-" <<j<<"! Guess again: " ;
cin>>guess;
}
if (guess<0)
{cout <<" " <<endl;
cout <<"Number is only 1-" <<j<<"! Guess again: " ;
cin>>guess;
}
} while (guess>j || guess<0);
}
if (guess>number)
{
cout<<"Too high, Guess again: " ;
cin>>guess;
++tries;
}
if (guess<number)
{
cout<<"Too low, Guess again: " ;
cin>>guess;
++tries;
}
if (guess==number)
{
if (tries == 1)
{
cout <<" " <<endl;
cout <<"Phenomenal! You got it on your first try!" <<endl;
cout <<" " <<endl;
}
if (tries > 1)
cout <<" " <<endl;
cout<<"Congratulations! You got it in " << tries <<" Guesses!" << endl;
cout <<"" <<endl;
if (q == 3)
{cout <<"" <<endl;
cout <<"Congratulations, you beat all the levels!!!!!!!!!!" <<endl;
cout <<"" <<endl;
}
}
}
if (q == 1);
{ j = 50;}
if (q ==2)
{ j = 100;}
} while (q != 3);
}
if (m == "p" )
{
cout <<" " <<endl;
cout << "Select difficulty mode:\n" ;
cout <<"" <<endl;
cout << "1 : Easy (1-25)\n" ;
cout << "2 : Medium (1-50)\n" ;
cout << "3 : Difficult (1-100)\n" ;
cout <<" " <<endl;
cin >> c;
cout << "\n" ;
switch (c)
{
case '1' : maxrand = 25;
j = 25;
break ;
break ;
case '2' : maxrand = 50;
j = 50;
break ;
case '3' : maxrand = 100;
j = 100;
break ;
default : exit(0);
break ;
}
cout<<"Im thinking of a number between 1-" <<j<<". Take a guess: " ;
int number=1+rand() % maxrand;
cin>>guess;
cout <<" " <<endl;
int tries = 0;
++tries;
cout <<" " <<endl;
while (guess!=number)
{ do {
if (guess>j)
{cout <<" " <<endl;
cout <<"Number is only 1-" <<j<<"! Guess again: " ;
cin>>guess;
}
if (guess<0)
{cout <<" " <<endl;
cout <<"Number is only 1-" <<j<<"! Guess again: " ;
cin>>guess;
}
}
while (guess>j || guess<0);
if (guess>number)
{
cout<<"Too high, Guess again: " ;
cin>>guess;
++tries;
}
if (guess<number)
{
cout<<"Too low, Guess again: " ;
cin>>guess;
++tries;
}
}
if (guess==number)
{
cout <<" " <<endl;
cout<<"Congratulations! You got it in " << tries <<" Guesses!" << endl;
}
}
do { cout <<"" <<endl;
cout << "Would you like to play again? y/n: " ;
cin >> playAgain;
cout <<" " <<endl;
} while (playAgain !='y' && playAgain !='n' );
}
cout << "Okay, see you next time!" << endl;
cout << "Thank you for playing a game by Anmol Sethi" << endl;
cout <<"" <<endl;
system ("pause" );
}
Btw its for a School Probability Project.
Last edited on Mar 3, 2012 at 3:49am UTC
Mar 3, 2012 at 4:28am UTC
Please Someone Help!
Mar 3, 2012 at 4:29am UTC
You can simplify the following code :
1 2 3 4 5 6 7 8
if (guess==number)
{
if (tries == 1)
{
cout <<" " <<endl;
cout <<"Phenomenal! You got it on your first try!" <<endl;
cout <<" " <<endl;
}
to
if (guess == number && tries == 1) cout << "Phenomenal!"
and I can't tell without pluggin this into my ide but you should check the entry once for victory before you enter the while loop.
like
1 2 3 4 5 6 7 8 9 10 11 12
cin >> number;
if (number == j)
{
++tries;
cout << "Phenominal! You got it on the first try!" ;
}
else
do
{
//code here
//include counter
} while (number != j && count <= 10)
Last edited on Mar 3, 2012 at 4:30am UTC
Mar 3, 2012 at 4:39am UTC
ty ill try right now :D
Mar 3, 2012 at 4:42am UTC
YOUR MY NEW BEST BUDDY CAUSE I THINK IT WORKED :D
Mar 3, 2012 at 4:46am UTC
IT DID :D
Mar 3, 2012 at 4:47am UTC
Congrats (;
Topic archived. No new replies allowed.