New to C++, Loops Help

Hello, I have an assignment to write a random number guessing game that generates a random number between 5 and 15 and asks the user to guess what the number is. If the user's guess is higher than the random number, my program should display "Too high, try again." If it's lower, it should display "Too low, try again." I'm supposed to use a loop that repeats and keeps a count of the number of guesses until the random number is guessed correctly. My program should display the number of guesses along with a congratulatory message. Then I'm supposed to give the user the opportunity to play the game again or quit.

This is my 3rd update.
The code compiles but I think my random number formula is off and I don't understand loops very well because my code loops infinitely.

My source code:
#include<iostream>
#include<string>
#include<cstdlib>
#include<ctime>
#include<cmath>

using namespace std;

int main()
{
bool guess;
int randomNum;
unsigned int seed = time(0);
srand(seed);
char tryagain;

do{

cout<<"This is a random number guessing game! \nThe number is between 5 and 15. Can you guess it?"<<endl;
cin>>guess;

randomNum = 5 + rand() % 15;

if (guess == randomNum){
cout<<"Congratulations. You figured out my number!"<<endl;
}else while(guess != randomNum)
if (guess>randomNum){
cout<<"Too high. Try again."<<endl;
}else if (guess<randomNum){
cout<<"Too low. Try again."<<endl;
}else if (guess > 15 || guess < 5){
cout<<"Guess did not stay within the 5-15 range. Type Y to try again."<<endl;
cin>>tryagain;}
}while (tryagain == 'Y' || tryagain == 'y');
return 0;
}

Help would be appreciated.
Last edited on
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
int main()
{
    int guess;
    int randomNum;
    unsigned int seed = time(0);
    srand(seed);
    char tryagain;

    do{
        cout<<"This is a random number guessing game! \nThe number is between 5 and 15. Can you guess it?"<<endl;
        cin>>guess;

        randomNum = 5 + rand() % 15;

        if (guess > 15 || guess < 5)
            cout<<"Guess did not stay within the 5-15 range. Type Y to try again."<<endl;
            
        else if (guess == randomNum)
            cout<<"Congratulations. You figured out my number!"<<endl;
        
        else
            while(guess != randomNum)
                if (guess>randomNum)
                    cout<<"Too high. Try again."<<endl;
                else if (guess<randomNum)
                    cout<<"Too low. Try again."<<endl;

    } while (tryagain == 'Y' || tryagain == 'y');
    return 0;
}


I suppose the first major problem you have is the while loop on line 22. It is an infinite loop because you never change the variables of guess or randomNum inside of it. You're going to want to keep asking the user for a new guess every time.

I'd suggest writing some psuedocode to examine the flow of logic that you want from your program if you already haven't done so. :P

[Edit]
Also, randomNum = 5 + rand() % 15; will actually result in a number btween 5 and 19 which I do not believe you want. rand()%15 will randomly generate a number between 0 and 15 and then you add 5.
Last edited on
Thanks for the reply, I appreciate it. I know I have to add something like guess++;
I just don't understand what that does, or rather HOW I could ask the user for a new guess every time. :/
you're actually really close. Your logic is reversed. You want your while loop and then your if/else-if/else statements. so something like:
1
2
3
4
while ( guess != randNum )
{
    //check to see if user is right. If not, use cin to keep getting a new guess from them.
}
Last edited on
Thank you so much for the assistance but I'm still confused. I modified my source code using your notes and now it looks like this:


#include<iostream>
#include<string>
#include<cstdlib>
#include<ctime>
#include<cmath>

using namespace std;

int main()
{
bool guess;
int randomNum;
unsigned int seed = time(0);
srand(seed);
char tryagain;

do{

cout<<"This is a random number guessing game! \nThe number is between 5 and 15. Can you guess it?"<<endl;
cin>>guess;

randomNum = rand()%15 + 5;
while(guess != randomNum){
if (guess>randomNum)
cout<<"Too high. Try again."<<endl;
guess++;
}else if (guess<randomNum){
cout<<"Too low. Try again."<<endl;
guess++;}

}while (tryagain == 'Y' || tryagain == 'y');
return 0;
}

Does this look like I'm on the right track?

It still runs infinitely >:[
Last edited on
It looks good; the only thing you seem to be missing is that you never are asking the user for anything to change tryagain, so it will never change once you enter the loop.
I tried entering 15, the highest possible answer, and it spit out infinite "Too low. Try again."
What I'm asking is how to prompt the user to guess again?
Close. You're looking for something more along the lines of:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
cout<<"This is a random number guessing game! \nThe number is between 5 and 15. Can you guess it?"<<endl;
cin>>guess;
int tries = 1;
while(guess != randomNum)
{
    if ( guess > randNum )
    {
        cout<<"Too high. Try again: ";
        cin >> guess;
    }
    else if ( guess < randNum )
    {
        cout<<"Too low. Try again.";
        cin >> guess;
    }
    ++tries;
}


Your braces were off in the code you gave.
Notice how as long as the user guesses incorrectly, you have to keep prompting them to try again. You also needed two independent variables: one to hold the user's guess and one to keep track of tries.
Last edited on
GRRR... this is so frustrating. The help you offered me Keene still runs infinitely. I know I'm not supposed to just ask for the answer and I'm not trying to but I just don't understand. This must not be the right place for the assistance that I guess I need. X[
You changed guess to a bool. Change it back to an int. :P That ought to stop the infinite loop.
That did it! Thank you very much. I can probably figure the rest out now and I appreciate all of your 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
#include<iostream>
#include<ctime>
using namespace std;

int main()
{
	int guess;
	int randomNum;
	char tryagain;
	int count;

	srand(static_cast<unsigned>(time(NULL)));

	do
	{
		system("cls");

		int ran = rand()%15;
		randomNum  = ran < 5 ? ran + 5 : ran;	
		cout<<randomNum<<endl;

		cout<<"This is a random number guessing game! \nThe number is between 5 and 15. Can you guess it?"<<endl;
		cin>>guess;

		count = 1;

		while(guess != randomNum)
		{
			count++;

			if (guess > 15 || guess < 5)
			{
				cout<<"Guess did not stay within the 5-15 range. Type Y to try again."<<endl;
			}
			else if(guess > randomNum)
				cout<<"Too high. Try again."<<endl;
			else 
				cout<<"Too low. Try again."<<endl;

			cin>>guess;
		}

		cout<<"Correct!!"<<endl<<endl;
		cout<<"you took "<<count<<" chance(s)"<<endl<<endl;
		cout<<"Do you want to try again:( y for yes): ";
		cin>>tryagain;

	}while (tryagain == 'Y' || tryagain == 'y');
	return 0;
}
Topic archived. No new replies allowed.