I created a random number game where the user tries to guess the number using a while statement with three ifs in it for high low and correct number. but after the user guesses the the number it ends the while and the program. How can I make it so the user can guess another number it he or she would like? I was thinking of making the while statement a function and calling it with main. Any guidence would be appreciated.
Thanks,
Rich

Show us what you have so far, it's much easier that way to help you!

HI,
This is a very inefficent :rolleyes: example, however it might give you an idea on how to implement your loop so that it ask for a number again.

SAMPLE OUTPUT:
NOTE:It was run on a linux machine.

smg@samoguz-desktop ~/Desktop
$ ./num

guess the number:
2
Guess number:
3
Guess number:
10
Congrat!

Want to try again? y
Guess number:
6
Guess number:
7
Guess number:
10
Congrat!

Want to try again? n
smg@samoguz-desktop ~/Desktop

CODE

#include <iostream>
using namespace std;
int main()
{
int number=10;
int x;
int your_number;;
char answer = 'y';
cout<< "\n\nguess the number:\n";
cin >> your_number;

while (your_number != number || answer == 'y')
{
if(number == your_number)
{
cout<<"Congrat!\n\n";
cout<<"\n Want to try again? ";
cin>>answer;
if(answer == 'y')
your_number = 0;
else
break;
}
else
{
cout<< "\n Guess number:\n";
cin >> your_number;
}
}

return 0;
}

I created a random number game where the user tries to guess the number using a while statement with three ifs in it for high low and correct number. but after the user guesses the the number it ends the while and the program. How can I make it so the user can guess another number it he or she would like? I was thinking of making the while statement a function and calling it with main. Any guidence would be appreciated.
Thanks,
Rich

please edit your post and insert code tags so that my eyes will not hurt so bad when I read your code.:eek: :eek:

My bad. Let me know if you can read it now. :!:

#include <iostream>
using namespace std;
int main()
{
int number=10;
int x;
int your_number;;
char answer = 'y';
cout<< "\n\nguess the number:\n";
cin >> your_number;
 
while (your_number != number || answer == 'y')
{
if(number == your_number)
{
cout<<"Congrat!\n\n";
cout<<"\n Want to try again? ";
cin>>answer;
if(answer == 'y')
your_number = 0;
else
break;
}
else
{
cout<< "\n Guess number:\n";
cin >> your_number;
}
}
 
return 0;
}
 
 
SAMPLE OUTPUT:
NOTE:It was run on a linux machine.

smg@samoguz-desktop ~/Desktop
$ ./num

guess the number:
2
Guess number:
3
Guess number:
10
Congrat!

Want to try again? y
Guess number:
6
Guess number:
7
Guess number:
10
Congrat!

Want to try again? n
smg@samoguz-desktop ~/Desktop

My bad. Let me know if you can read it now.

OK, now indent your code so it can be read. After every { indent 3-4 spaces, before every } unindent. Can't tell which open brace goes with which close brace with unformatted code.

OK, now indent your code so it can be read. After every { indent 3-4 spaces, before every } unindent. Can't tell which open brace goes with which close brace with unformatted code.

Is it better now. sorry I did it in a hurry.

#include <iostream>
using namespace std;
int main()
{
     int number=10;
     int x;
     int your_number;;
     char answer = 'y';
     cout<< "\n\nguess the number:\n";
     cin >> your_number;
 
     while (your_number != number || answer == 'y')
     {
       if(number == your_number)
       {
             cout<<"Congrat!\n\n";
             cout<<"\n Want to try again? ";
             cin>>answer;
             if(answer == 'y')
                 your_number = 0;
             else
             break;
       }
       else
       {
             cout<< "\n Guess number:\n";
             cin >> your_number;
       }
    }
 
return 0;
 
}
 
 
SAMPLE OUTPUT:
NOTE:It was run on a linux machine.
 
smg@samoguz-desktop ~/Desktop
$ ./num
 
guess the number:
2
Guess number:
3
Guess number:
10
Congrat!
 
Want to try again? y
Guess number:
6
Guess number:
7
Guess number:
10
Congrat!
 
Want to try again? n
smg@samoguz-desktop ~/Desktop

Maybe you can try out something of this sort and make it more robust and suiting to your own purpose.

using namespace std;
int main()
{
     int number=10;
     int your_number;
     char answer = 'y';

    do
    {
        cout<< "\nguess the number: ";
        cin >> your_number;

        if(number == your_number)
        {
            cout<<"\nCongrats!";
            cout<<"\nWant to try again? ";
            cin >> answer;
        }
       else if (your_number > number)
       {
            cout << "\nYour guess is a bit too high " ;
        }
       else if (your_number < number)
       {
            cout << "\nYour guess is a bit too low " ;
       }
    }
    while (answer == 'y');
    return 0;
}

Hope it helped, bye.

Is it better now. sorry I did it in a hurry.

#include <iostream>
using namespace std;
int main()
{
     int number=10;
     int x;
     int your_number;;
     char answer = 'y';
     cout<< "\n\nguess the number:\n";
     cin >> your_number;
 
     while (your_number != number || answer == 'y')
     {
       if(number == your_number)
       {
             cout<<"Congrat!\n\n";
             cout<<"\n Want to try again? ";
             cin>>answer;
             if(answer == 'y')
                 your_number = 0;
             else
             break;
       }
       else
       {
             cout<< "\n Guess number:\n";
             cin >> your_number;
       }
    }
 
return 0;
 
}
 
this is not part of your code.  ending code tag should go here 
SAMPLE OUTPUT:
NOTE:It was run on a linux machine.
 
smg@samoguz-desktop ~/Desktop
$ ./num
 
guess the number:
2
Guess number:
3
Guess number:
10
Congrat!
 
Want to try again? y
Guess number:
6
Guess number:
7
Guess number:
10
Congrat!
 
Want to try again? n
smg@samoguz-desktop ~/Desktop

#1) Does while (your_number != number || answer == 'y') do what you want? Make a truth table.

#2) If your while loop is already testing if the number is == or != just let the loop end. Then verify outside the loop it's because of the == and output the results. That will make your while loop simple and easier to debug. Just put in it the stuff that has to go in it. IOW separate the 'work' with the 'result display'

Thank you for the help. I didn't reply sooner because after the first post, telling me the way I was thinking was correct, I wanted to try and code this myself before I looked at anyone else's work. Tonight was the first time back to this forum since.
Thanks again,
Richc

This question has already been answered. Start a new discussion instead.