I need help making a number guessing game.

Write your question here.
Hi I have just started coding and was wondering if you could help me out.
It keeps telling me the answer. I also need to make it ask if you want to play again. I would apperaicate some help because I have been working on this for awhile.
also I it has to be between 5-15
and I also need to give the user the opportunity to play the game again or quit.
Thanks
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
  #include <iostream>
#include <cstdlib>          // Needed for random numbers
#include <ctime>
using namespace std;

int main()
{
	int guess, tries=0, randomNum;
    // Will hold the randomly generated integer

	srand(time(0));// Seed the random generator
	randomNum = 5 + rand() % 11; //random number between 5-15
   
do
{
     cout<< "Guess a number between 5 and 15 "<<endl;
    cout<<randomNum<<endl;
	cin>>guess;
	tries++;

    if(randomNum < guess)
       cout<<"Too high, Try Again."<<endl;
    else if(guess<randomNum)
       cout<<"Too low, Try again."<<endl;
    else
       cout << "\nCongratulations. you guessed the number and it took you \n " << tries << " time to find out!\n";

}while (guess != randomNum);

cin.ignore();
cin.get();



}
Random number is displayed because you do so on line 17.

And I also need to give the user the opportunity to play the game again or quit.


To do this you have to wrap the
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
randomNum = 5 + rand() % 11; //random number between 5-15
   
do
{
     cout<< "Guess a number between 5 and 15 "<<endl;
     cin>>guess;
     tries++;

    if(randomNum < guess)
       cout<<"Too high, Try Again."<<endl;
    else if(guess<randomNum)
       cout<<"Too low, Try again."<<endl;
    else
       cout << "\nCongratulations. you guessed the number and it took you \n " << tries << " time to find out!\n";

}while (guess != randomNum);
part into another loop for example :

1
2
3
4
5
6
7
8
9
10
11
12
13
14
bool done = false;

while(!done)
{
    //your number guessing code here

   cout << "Another try? (Y/N)? : ";
   char ch;
   cin >> ch;
   if(ch == 'N') done = true;
}

cin.ignore();
cin.get();

Last edited on
thanks also it keeps showing the answer. How would I fix that?
I keeps showing me the random number. How would I fix that?
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
#include <iostream>
#include <cstdlib>          // Needed for random numbers
#include <ctime>           // For the time function
#include <string>         //for a string
using namespace std;


int main()
{
	int guess, tries=0, randomNum;
    // Will hold the randomly generated integer
    char again;
	srand(time(0));// Seed the random generator
	randomNum = 5 + rand() % 11; //random number between 5-15
   
    
do
{
    randomNum = 5 + rand() % 11;
     cout<< "Guess a number between 5 and 15 "<<endl;
    cout<<randomNum<<endl;
	cin>>guess;
	tries++;

    if(randomNum < guess)
       cout<<"Too high, Try Again."<<endl;
    else if(guess<randomNum)
       cout<<"Too low, Try again."<<endl;
    else
       cout << "\nCongratulations. you guessed the number and it took you \n " << tries << " time to find out!\n";
    cout<<"Do you want to play again? (Y/N)";
    cin>>again;
}while(again == 'Y' || again == 'y');
while (guess != randomNum);

cin.ignore();
cin.get();
}

	
just delete this line form your code
cout<<randomNum<<endl;
I just figured it out. I Feel so dumb for putting it there.

#include <iostream>
#include <cstdlib> // Needed for random numbers
#include <ctime> // For the time function
#include <string> //for a string
using namespace std;


int main()
{
int guess, tries=0, randomNum;
// Will hold the randomly generated integer
srand(time(0));// Seed the random generator
randomNum = 5 + rand() % 11;//random number between 5-15


do
{
cout<< "Guess a number between 5 and 15 "<<endl;
cin>>guess;
tries++;
if(randomNum < guess)
cout<<"Too high, Try Again."<<endl;
else if(guess<randomNum)
cout<<"Too low, Try again."<<endl;
else
cout << "\nCongratulations. you guessed the number and it took you \n " << tries << " time to find out!\n";

}while (guess != randomNum);
bool done = false;

while(!done)
{
//your number guessing code here

cout << "Another try? (Y/N)? : ";
char ch;
cin >> ch;
if(ch == 'N') done = true;
if(ch == 'n') done = true;
}
cin.ignore();
cin.get();
}


you should put your do{/* . . . */}while(guess != randomNum);
inside
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
//...
while(!done)
{
     //except you have to take these 2 lines out of your while so only one random number 
     //is generated for each guessing game
     randomNum = 5 + rand() % 11;
     cout<< "Guess a number between 5 and 15 "<<endl;

     time = 0; //also null the times each time you repeat the game

     /* your do while goes here */ 

    cout << "Another try? (Y/N)? : ";
    char ch;
    cin >> ch;
    if(ch == 'N' || ch == 'n') done = true;
}

cin.ignore();
cin.get();


so that you can repeat your own loop as many times as you want. With current code you generate new random number every time you either guess it or don't.
Last edited on
Topic archived. No new replies allowed.