Having trouble with Random Number Guessing Game

Hello guys I am currently in an intro computer science class,

I am currently having trouble with an assignment, it is a random number guessing game, the guess must be between 0 and 100.

When I run the program, I only can type in one guess and the program ends

Do I have to use a while loop?

And also I don't know how to count the number of guesses and the maximum is 8.


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
#include <iostream>
#include <cstdlib>

using namespace std;

int main ()
{
        int secret;
        int guess;
        int tries;

        secret = rand() % 100+1;

        cout << "Welcome to Guess-my-Number version 0.1" << endl;

        cout << "A secret number between 1-100 generated..." << endl << endl;

        cout << "Please enter your guess? " << endl;
        cin >> guess;

        tries++;

        while (guess < 0 || guess > 100)
        {
                cout << "Invalid output." << endl;
                cout << "Please enter your guess? ";
                cin >> guess;
        }

        if (guess < secret)
                cout << "Guess is high..." << endl;

        if (guess > secret)
                cout << "Guess is low..." << endl;

        if (guess == secret)
                cout << secret << "correct" << endl;


        return 0;
}

Here's what I have so far.
This is a very difficult subject for me, any hints would be greatly appreciated thank you
Do I have to use a while loop?

Yes. You need 1 more loop.
does it matter which one I use? For? While? do-while?
I'm just confused on what I need to include inside that loop
Haha, this reminds me of something I did awhile back. You should use the while loop. If you want any reference, here was what I did. Only difference with mine is the user gets to input the range. I also recommend seeding the random with time().

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
    cout << "Can you guess the number I am thinking of?" << endl << "First, choose two numbers when prompted." << endl;
    // fNumber is the first number the users input, sNumber is the second number the users input.
    int fNumber,sNumber=0;

    cout << "First Number: ";
    cin >> fNumber;

    cout << endl << "Second Number: ";
    cin >> sNumber;


    // For the randNum to work, fNumber has to be bigger than sNumber.  This if-statement changes it if it's not.
    if (fNumber<sNumber) {
        int temp=fNumber;
        fNumber=sNumber;
        sNumber=temp;
    }

    cout << string(3, '\n') << "Ok, the number I am thinking of is between " << fNumber << " and " << sNumber << "." << endl;
    int guess=0;

    // This creates the random number between the two values the user inputed. (fNumber and sNumber)
    srand(time(0));
    int randNum = rand()%(fNumber-sNumber + 1) + sNumber;

    cout << "Can you guess it?" << endl;


    while(guess!=randNum) {
        cout << endl;
        cout << "Guess: ";
        cin >> guess;
        if (guess < randNum)
            cout << "Too low" << endl;

        if (guess > randNum)
            cout << "Too high" << endl;

    }
    if(guess==randNum) {
    string(4, '\n');
    cout << "Nice, you guessed my number!" << endl;
    cout << "You get a gazillion points!" << endl;
   

Last edited on
Thank you very much, yes I was going to add srand() after completing the program to see if it worked.
How do I get it to count the number of guesses?
@oscClippers

At the moment, all your code does is ask you once and then it ends. Also you have your operators switched.
It should be like this.
1
2
3
4
5
        if (guess > secret) // Your operators were incorrect earlier.  This is the correct way.
                cout << "Guess is high..." << endl;

        if (guess < secret) //Same with this one.
                cout << "Guess is low..." << endl;


I recommend you put the if statements under the while loop. If you need help with that, here is a good resources. http://www.cplusplus.com/doc/tutorial/control/
@h3y4w
oh shoot i fixed that thanks
while (tries < 8)
{

if (guess == 0)
cout << "The Secret Number is " << secret << endl;
break;

if (guess < 1 || guess > 100)
cout << "Invalid output." << endl;
cout << "Please enter your guess? ";
cin >> guess;

if (guess < secret)
cout << "Guess is low..." << endl;

if (guess > secret)
cout << "Guess is high..." << endl;
tries++;

if (guess == secret)
cout << secret << " correct in " << tries << endl;
break;
}
like this?
when i run it, it only does one guess then quits the program? why?
It needs to be braced within a loop.
I don't know if it's too late, but here is what I came up with.

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
#include <iostream>
#include <ctime>
#include <cstdlib>

int main(){
	srand(static_cast<unsigned int>(time(NULL)));	// seed rand numb 
	
	int rndNum = rand() % 100 + 1;				// I hope you understand why. rand() % 100 + 1 = random number from 1 to 100
	int guess;
	int tries = 0;

	std::cout << "Welcome to Guess-my-Number version 0.1!" << std::endl;
	std::cout << "A secret number between 1-100 generated..." << std::endl << std::endl;

	// going to use a do while loop so that it executes at least once. i can use a while loop here but i just want to make sure.
	do{
		std::cout << "Please enter your guess: ";
		std::cin >> guess;
		++tries;			// we want to increment the number of tries every time the guess is incorrect.

		if (guess > rndNum)
			std::cout << "Too high of a guess! Please try lower." << std::endl;

		else if (guess < rndNum)
			std::cout << "Your guess is too low! Please try again." << std::endl;

		else
			std::cout << "Good job, you've figured out the number after " << tries << " tries!" << std::endl;
	} while (guess != rndNum && tries != 8);		// If our guess isn't the random number AND our tries aren't equal to 8..

	return 0;
}
Last edited on
Topic archived. No new replies allowed.