# Number Guessing Game Help

Printable View

• March 2nd, 2012, 10:01 PM
Anmol444
Number Guessing Game Help
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

#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.
• March 3rd, 2012, 06:39 AM
D_Drmmr
Re: Number Guessing Game Help
Quote:

Originally Posted by Anmol444
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

Please use code tags when posting code to preserve indentation.
Did you step through your program with the debugger and watch the values of your variables? If not, then you should do that first. Learning how to debug a program is part of learning how to program.
• March 4th, 2012, 12:40 PM
longbodie99
Re: Number Guessing Game Help
- must #include <time.h> if want to use time()
- must #include <string> if want to use string
- if you define string m then you can not use cin >> m, it only works if you define m as character. Correct syntax is getline (cin, m);
See getline and cin.getline for more info if you want to input a string.
- some error with ; please correct
Quote:

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
this happened in ''level mode'' as your program is
- number always is 1 because you did not generate a random number but define num =1 in your code, find and you will see , that why number always is 1.
- it doesn't display "Phenomenal! You got it on your first try!" but instead skips to the next level
because you put it in loop while(guess!=number) .... so that never got chance to be executed because it going to execute when you guessed it wrong

- on ''practice mode'' - it doesn't work because of your wrong syntax when you use case() loop but I can see you did generate a random number, fix it
• March 4th, 2012, 01:37 PM
D_Drmmr
Re: Number Guessing Game Help
Quote:

Originally Posted by longbodie99
- if you define string m then you can not use cin >> m, it only works if you define m as character. Correct syntax is getline (cin, m);

You can use the stream operator with a string, but it will only read one word (up to the next whitespace). getline will read until the next newline character.