Advice needed for assignment

I have a homework assignment I was hoping to get some light shed on...

Write a program that generates a random number between 1 and 100 and asks the user to guess what the number is. If the user’s guess is higher than the random number, the program should display “Too high, try again.” If the user’s guess is lower than the random number, the program should display “Too low, try again.” The program must use a loop that repeats until the user correctly guesses the random number or has made 10 guesses. The program needs to keep track of the number of guesses the user makes. At the end the program will display one of the messages in the table below based on the number of guesses the user took. The program must validate the user’s guess by making sure the value entered is between 1 and 100. If the value is not between 1 and 100, the user should be told to make another guess. The invalid input should not count as one of the 10 guesses the user is allowed.

Number of Guesses by the User Output Message
Less than 5 guesses "Either you know the secret or you got lucky!"
5-7 guesses "You're pretty good at this!"
8-10 guesses "You'll do better next time."
If guess 10 is not correct "Sorry - You have taken too many guesses."


Following is what I have coded thus far:

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
/**********************************************************************************************
COMMENTS
**********************************************************************************************/
#include <iostream>
#include <iomanip>
#include <ctime>
using namespace std;

int main()
{
// Variables used 
const int MIN_NUM = 1;
const int MAX_NUM = 10;
int UserGuess; // Counter
int RightGuess = (rand() % 100) + 1;
int num = MIN_NUM;

do
{ 
cout << "Guess a number between 1 and 100: " << endl;
cin >> UserGuess; 
if (UserGuess < 1 || UserGuess > 100)
	cout << "The number is in the range 1 to 100. Guess again." << endl;
else if (UserGuess > RightGuess)
    cout << "Too high! Try again!" << endl;
else if (UserGuess < RightGuess)
    cout << "Too low! Try again!" << endl;
else
    cout << "That's it! Way to go." << endl;
num++;
} 
while (num <= MAX_NUM);

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


I can see two things wrong with the code (although I'm sure others will see many more):
1: The code will loop through 10 iterations regardless of if the correct answer is chosen or not
2: Obviously, the code does not calculate a response based upon if the user guesses the correct answer in under 5, 7 or 10 guesses... I cannot think of a good way to accomplish this.

Any advice would be greatly appreciated.
First, a few things.

1) You need to include the c standard library: #include <cstdlib>

2) You never included the srand function in the first place to even generate a random number, which is why your code will give you an error. Declare your srand before the variables: srand(time(0));

3) Your loop will actually iterate 11 times, not 10, since on the 10th loop it will pass the while test (being equal to 10) and go around once more. Change the test so it goes around exactly 10 times.

4) I would actually suggest you put line #20 BEFORE the loop. Its better that this message is only printed once.

5) The counter (num++) should actually be part of the "too high" and "too low" if-statements. As your assignment entails, only the valid inputs (1-100) should be counted as part of the 10 guesses. Your current code counts every guess (whether valid or invalid). Please fix this.

6) You need to write your code so that the program ends when the correct number is guessed. Right now, even if I guess the correct answer on my first try, it will print out the message and still allow the loop to continue. To see this for yourself, cout RighGuess and input it in your first try.

Do all this and we can move on from there.



Last edited on
Thanks for the reply Arslan.

1) Added

2) I excluded this on purpose; I found knowing the number allowed me greater control in testing the code. I was going to add it prior to actually submitting.

3) I changed the MAX_NUM to 9

4) When I add this before the loop, it simply prints the given cout 11 times... Unsure how to do this without modifying my code in a major way.

5) I tried doing this, but only made it worse I believe... Now it simply repeats the "Number too low/too high" statement 10 times in a row...

6) This was my first question from above... I'm not sure how to make this happen...

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
/**********************************************************************************************
COMMENTS
**********************************************************************************************/
#include <iostream>
#include <iomanip>
#include <ctime>
using namespace std;

int main()
{
// Variables used 
const int MIN_NUM = 1;
const int MAX_NUM = 9;
int UserGuess; // Counter
int RightGuess = (rand() % 100) + 1;
int num = MIN_NUM;

cout << "Guess a number between 1 and 100: " << endl;
cin >> UserGuess; 

do
{
if (UserGuess < 1 || UserGuess > 100)
	{
	cout << "The number is in the range 1 to 100. Guess again." << endl;
	num++;
	}
else if (UserGuess > RightGuess)
	{
    cout << "Too high! Try again!" << endl;
	num++;
	}
else if (UserGuess < RightGuess)
	{
    cout << "Too low! Try again!" << endl;
	num++;
	}
} 
while (num <= MAX_NUM);

cin.ignore().get();
	return 0;
}
Last edited on
Why did you delete your final else statement from your latest code?

Where is this:

1
2
3
4
else {
cout << "That's it! Way to go." << endl;
num++;
}


4) When I add this before the loop, it simply prints the given cout 11 times... Unsure how to do this without modifying my code in a major way.


simply cut and paste it to before the loop, like below:

1
2
3
4
5
6
7
cout << "Guess a number between 1 and 100: " << endl;
do
{
cin >> UserGuess;
if (UserGuess < 1 || UserGuess > 100)

// rest of your code.... 


5) I tried doing this, but only made it worse I believe... Now it simply repeats the "Number too low/too high" statement 10 times in a row...


Thats because you have your cin outside the loop. Put it back inside the loop like you had in your OP. What your code is doing is only seeing that one input and going around 10 times with it, hence the same message prints out 10 times.

Also, you DONT want to add to the counter if an invalid input is entered. So remove the num++ from your first if-statement.

6) This was my first question from above... I'm not sure how to make this happen...


Hint: Modify your while test. You need to keep the loop running up to 10 times unless the correct answer is given. Once the correct answer is given, it should fail this test and exit the loop.

I wont give you the answer but here is what it should look like:

while (num <= MAX_NUM && UserGuess.....)

Im sure you can figure it out.
Last edited on
Also, its best if you change for int MIN to 0, int MAX to 10, and only use a less than sign for your while test (as opposed to a less than-equal sign like you are doing now).

Your loop as of now goes around 9 times. Doing the above will make it go through 10 iterations.
OK, I believe I got point 6 with the following:
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
/**********************************************************************************************
COMMENTS
**********************************************************************************************/
#include <iostream>
#include <iomanip>
#include <ctime>
using namespace std;

int main()
{
// Variables used 
const int MIN_NUM = 1;
const int MAX_NUM = 10;
int UserGuess; // Counter
int RightGuess = (rand() % 100) + 1;
srand (time(NULL)); 
int num = MIN_NUM;
cout << "Guess a number between 1 and 100: " << endl;
do
{ 
cin >> UserGuess; 
if (UserGuess < 1 || UserGuess > 100)
	cout << "The number is in the range 1 to 100. Guess again." << endl;
else if (UserGuess > RightGuess)
    cout << "Too high! Try again!" << endl;
else if (UserGuess < RightGuess)
    cout << "Too low! Try again!" << endl;
else
    cout << "That's it! Way to go." << endl;
num++;
} 
while (num <= MAX_NUM && UserGuess != RightGuess);

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


Sorry, I believe I'm confusing myself with some of your questions (my fault).

I think now all I need is to figure out how to make the response vary depending on if the user guesses it in "x" amount of tries.
I think now all I need is to figure out how to make the response vary depending on if the user guesses it in "x" amount of tries.


Thats quite simple, actually. You already have your counter (the variable num), so all you need is a few if-statements to go with it, like so:

1
2
if(num<5)
cout << "You are one lucky person!";


These if-statements would be after the loop.

Last edited on
I think I've got it; had to put the if statements inside the loop...

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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
/**********************************************************************************************
COMMENTS
**********************************************************************************************/
#include <iostream>
#include <iomanip>
#include <ctime>
using namespace std;

int main()
{
// Variables used 
const int MIN_NUM = 1;
const int MAX_NUM = 10;
int UserGuess; // Counter
int RightGuess = (rand() % 100) + 1;
srand (time(NULL)); 
int num = MIN_NUM;

cout << "Guess a number between 1 and 100: " << endl;

do
{ 
cin >> UserGuess; 
if (UserGuess < 1 || UserGuess > 100)
	{
	cout << "The number is in the range 1 to 100. Guess again." << endl;
	}
if  (UserGuess > RightGuess && UserGuess <= 100)
    {
		if (num < 10)
		{
		cout << "Too high! Try again!" << endl;
		num++;
		}
		else 
		{
		cout << "Sorry - You have taken too many guesses." << endl; 	
		break;
		}
	}
if  (UserGuess < RightGuess && UserGuess >= 1)
    {
		if (num < 10)
		{
		cout << "Too low! Try again!" << endl;
		num++;
		}
		else
		{
		cout << "Sorry - You have taken too many guesses." << endl;
		break;
		}
	}
if ((UserGuess == RightGuess) && (num < 5 && UserGuess == RightGuess))
	{
	cout << "Either you know the secret or you got lucky!" << endl;
	break;
	}
if ((UserGuess == RightGuess) && (num >= 5 && num <= 7)) 
	{
	cout << "You're pretty good at this!" << endl; 
	break;
	}
if ((UserGuess == RightGuess) && (num >= 8 && num <= 10))
	{
	cout << "You'll do better next time." << endl;
	break;
	}
} 
while ((num <= MAX_NUM) && (UserGuess != RightGuess));


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


I hope this meets all the requirements... It seems to at first glance.
Works fine.

Just include the c standard library and move the srand(time(NULL)) function above line 15 so it generates a different random number every time it runs.
Last edited on
Topic archived. No new replies allowed.