Need help asap with programming homework with random number guessing game and math tutor program..

Pages: 12
Hi there I have 2 programs I need help with for my class. One is a random number guessing game with a range between 1 and 100 with the range changing each time and never widening. The problem I'm having is that each time I restart the game it generates the same random number.

The other one is a math tutor program and I think I got it except I need help with the end where its telling the user how many questions they answered correct out of whatever many they answered and the percent correct. I think I need to use an accumulator like correct++ or something but I get lost how to do it after that..

First code..

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

using namespace std;

int main() {



	int random;
	int num;
	int max;
	int min;
	int outofrange;
	int rightguess;


	srand(time(NULL));
	random = rand() %100;

	rightguess = 1;
	outofrange = 1;
	max = 100;
	min = 1;

	bool repeat = true;
	while (repeat) {
		cout << "Welcome to the game Hi-Lo..\n";
		cout << "Enter an integer that is between 1 and 100: " << endl;
		cin >> num;

		while (num != random) {
			if (num > random) {
				cout << num << " is too high" << endl;
				max = num - 1;
			} else {
				cout << num << " is too low" << endl;
				min = num + 1;
			}
			cout << "Enter an integer that is between " << min+num << " and " << max << endl;

			cin >> num;

			if (num > max || num < min) {
				cout
						<< "the integer you enter is not within range, enter a number between "
						<< min << " and " << max << endl;
				cin >> num;
				outofrange = outofrange + 1;
			} else
				rightguess = rightguess + 1;
		}
		cout << " It took you " << rightguess
				<< " valid guesses to find the number " << endl;
		cout << " You had " << outofrange << " out of range guesses " << endl;

		cout << "Do you want to play again? [y/n]" << endl;
		char answer;
		cin >> answer;
		repeat = answer == 'y';
	}
	cout << "Thank you for playing Hi-Lo. Bye!" << endl;

	return 0;
}


2nd..
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
#include <iostream>
#include <iomanip>
#include <cstdlib>
#include <ctime>
using namespace std;

int main() {
	unsigned seed = time(0);

	srand(seed);

	int choice = 0;
	int total = 0;
	int num1, num2, result, useranswer;
	int correct = 1;
	int wrong = 1;


	do { // Display the menu and get a choice.

		cout << "\tMath Tutor Menu\n";
		cout << "------------------------------\n";
		cout << "1. Addition problem\n";
		cout << "2. Subtraction problem\n";
		cout << "3. Multiplication problem\n";
		cout << "4. Division problem\n";
		cout << "5. Quit this program\n";
		cout << "------------------------------\n";
		cout << "Enter your choice (1-5): ";
		cin >> choice;

		// Validate the choice.
		while (choice < 1 || choice > 5) {
			cout << "The valid choices are 1, 2, 3, "
					<< "4, and 5. Please choose: ";
			cin >> choice;
		}

		// Produce a problem.
		switch (choice) {
		case 1: // Addition problem
			// Generate two random numbers in
			// the range 1 - 500.
			num1 = 1 + rand() % 500;
			num2 = 1 + rand() % 500;

			// Calculate the correct answer.
			result = num1 + num2;

			// Display the problem.
			cout << "\n\n";
			cout << " " << setw(4) << num1 << endl;
			cout << " +" << setw(4) << num2 << endl;
			cout << " " << "----" << endl;
			cout << " ";
			break;

		case 2: // Subtraction problem
			// Generate two random numbers in
			// the range 1 - 999.
			num1 = 1 + rand() % 999;
			num2 = 1 + rand() % 999;

			// Make sure num2 <= num1...
			while (num2 > num1)
				num2 = 1 + rand() % 999;

			// Get the correct answer.
			result = num1 - num2;

			// Display the problem.
			cout << "\n\n";
			cout << " " << setw(4) << num1 << endl;
			cout << " -" << setw(4) << num2 << endl;
			cout << " " << "----" << endl;
			cout << " ";
			break;

		case 3: // Multiplication problem
			// Generate two random numbers. The first in
			// the range 1 - 100, the second in the
			// range 1 - 9.
			num1 = 1 + rand() % 100;
			num2 = 1 + rand() % 9;

			// Calculate the correct answer.
			result = num1 * num2;

			// Display the problem.
			cout << "\n\n";
			cout << " " << setw(4) << num1 << endl;
			cout << " *" << setw(4) << num2 << endl;
			cout << " " << "----" << endl;
			cout << " ";
			break;

		case 4: // Division problem with no remainder
			// Generate a single digit divisor.
			num2 = 1 + rand() % 9;

			// Generate a number that is a multiple
			// of num2...
			num1 = num2 * (rand() % 50 + 1);

			// Calculate the correct answer.
			result = num1 / num2;

			// Display the problem.
			cout << "\n\n";
			cout << " " << num1 << " / " << num2 << " = ";
			break;

		case 5: // The user chose to quit the program.

			cout << "Thank you for using Math Tutor";
			break;
		}

		// If student selected a problem, get and evaluate the answer.
		if (choice >= 1 && choice <= 4) {
			cin >> useranswer;
			if (useranswer == result)



				cout << "\n\nCongratulations! That's right.\n\n";

			else

				cout << "\n\nSorry, the correct answer is " << result
						<< ".\n\n";

		}
	}



	while(choice != 5);	return 0;
}


TIA!
Last edited on
weird...it appears like you did put srand(time(0))
also...the code must be under [code]
and the closing must use / rather than \
Last edited on
Yeah Im not sure..I think it might be a loop problem but I'm not seeing what to do next. I'll edit my post!
I see the problem now...
so
1
2
3
4
5
while(repeat)
{
     srand(time(0)) //put this
    //your code
}


that should fix it

the second one just use counter each time they correct add up counter
and output it
Last edited on
I'm not sure where to put that code in my code? I already have a while loop do I need another one?

Also what would be a good format for a counter?

Edit: Tried to enter that and it's still giving me the same random number..
Last edited on
No...put the srand(time(0)) in your while loop
Are you asking about type ??
Int is the best type
I tried putting srand in the while loop like you said and its still giving me the same error. I would run it once, get a random number. Run twice get the same random number from the 1st time..

I mean like.. I'm stuck here..

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
case 5: // The user chose to quit the program.
			cout << "Thank you for using Math Tutor.\n\n";
			break;
		}

		// If student selected a problem, get and evaluate the answer.
		if (choice >= 1 && choice <= 4) {
			cin >> useranswer;
			if (useranswer == result)
				cout << "\n\nCongratulations! That's right.\n\n";

			else
				cout << "\n\nSorry, the correct answer is " << result
						<< ".\n\n";

		}
	} while (choice != 5); // Loop again if student did not choose to quit.

	return 0;
}


Nothing displays after the user enters 5 except the thank you message. I need an output that will tell the user how many questions they answered, how many they got right and the percentage correct..except when I try to use a counter it doesnt do anything..
I tried putting srand in the while loop like you said and its still giving me the same error. I would run it once, get a random number. Run twice get the same random number from the 1st time..

The srand() function should only be called once, usually early in main() outside of any loop.

Nothing displays after the user enters 5 except the thank you message. I need an output that will tell the user how many questions they answered, how many they got right and the percentage correct..except when I try to use a counter it doesnt do anything..

Where are you trying to display this information?
Flaze07 wrote:
No...put the srand(time(0)) in your while loop

Bad advice. As jlb said, srand() should be called ONCE at the beginning of main. OP has the call to srand() in the correct place.

@OP - You're getting the same number every time because you call rand() at line 20 before the loop. Move the call to rand() inside the loop.

Last edited on
@OP - You're getting the same number every time because you call rand() at line 20 before the loop. Move the call to rand() inside the loop.


So srand outside any loop, rand() inside the while loop? like at line 29?
Line 2 is what I edited
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
	while (repeat) {
		random = rand() % 100;
		cout << "Welcome to the game Hi-Lo..\n";
		cout << "Enter an integer that is between 1 and 100: " << endl;
		cin >> num;

		while (num != random) {
			if (num > random) {
				cout << num << " is too high" << endl;
				max = num - 1;
			} else {
				cout << num << " is too low" << endl;
				min = num + 1;
			}
			cout << "Enter an integer that is between " << min + num << " and "
					<< max << endl;

			cin >> num;

			if (num > max || num < min) {
				cout
						<< "the integer you enter is not within range, enter a number between "
						<< min << " and " << max << endl;
				cin >> num;
				outofrange = outofrange + 1;
			} else
				rightguess = rightguess + 1;
		}
		cout << " It took you " << rightguess
				<< " valid guesses to find the number " << endl;
		cout << " You had " << outofrange << " out of range guesses " << endl;

		cout << "Do you want to play again? [y/n]" << endl;
		char answer;
		cin >> answer;
		repeat = answer == 'y';
	}
	cout << "Thank you for playing Hi-Lo. Bye!" << endl;

	return 0;
}



Where are you trying to display this information?


At the end of the program, when the user enters 5 as an input. Something like this should be displayed:

Terminating Math Tutor...

__________________________________
You worked on 6 problems.
1 addition problems
2 subtraction problems
1 multiplication problems
2 division problems
You got 4 problems correct!
Your percent correct is: 66.67%


Thank you for using Math Tutor.

But it only displays the Thank you message...I don't know how to use counters or where to put them in my code..
Last edited on
You need to update your counters, for example:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
case 1: // Addition problem
			// Generate two random numbers in
			// the range 1 - 500.
			num1 = 1 + rand() % 500;
			num2 = 1 + rand() % 500;

			// Calculate the correct answer.
			result = num1 + num2;

			// Display the problem.
			cout << "\n\n";
			cout << " " << setw(4) << num1 << endl;
			cout << " +" << setw(4) << num2 << endl;
			cout << " " << "----" << endl;
			cout << " ";
			break;

Update your addition counter inside the case statement.

On line 138 before you return you need to display the results.

Update your addition counter inside the case statement.

On line 138 before you return you need to display the results.


Ok so I have something like this now in each case..
1
2
3
4
5
6
7
8
9
10
11
12
13
14
// Produce a problem.
		switch (choice) {
		case 1: // Addition problem
			// Generate two random numbers in
			// the range 1 - 500.
			num1 = 1 + rand() % 500;
			num2 = 1 + rand() % 500;

			// Calculate the correct answer.
			result = num1 + num2;
			if(result==useranswer)
			{
				correct+=1;
			}

and defined correct as an int and set it to 1. How do I add up the total correct at the end? It just displays 1 problem correct
Can you post now the current code.
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156

			#include <iostream>
#include <iomanip>
#include <cstdlib>
#include <ctime>
using namespace std;

int main() {
	unsigned seed = time(0);

	srand(seed);

	int choice = 0;
	int num1, num2, result, useranswer;
	int correct=0;

	do { // Display the menu and get a choice.

		cout << "\tMath Tutor Menu\n";
		cout << "------------------------------\n";
		cout << "1. Addition problem\n";
		cout << "2. Subtraction problem\n";
		cout << "3. Multiplication problem\n";
		cout << "4. Division problem\n";
		cout << "5. Quit this program\n";
		cout << "------------------------------\n";
		cout << "Enter your choice (1-5): ";
		cin >> choice;

		// Validate the choice.
		while (choice < 1 || choice > 5) {
			cout << "The valid choices are 1, 2, 3, "
					<< "4, and 5. Please choose: ";
			cin >> choice;
		}

		// Produce a problem.
		switch (choice) {
		case 1: // Addition problem
			// Generate two random numbers in
			// the range 1 - 500.
			num1 = 1 + rand() % 500;
			num2 = 1 + rand() % 500;

			// Calculate the correct answer.
			result = num1 + num2;
		if(result==useranswer)
		{
			correct+=1;
		}


			// Display the problem.
			cout << "\n\n";
			cout << " " << setw(4) << num1 << endl;
			cout << " +" << setw(4) << num2 << endl;
			cout << " " << "----" << endl;
			cout << " ";
			break;

		case 2: // Subtraction problem
			// Generate two random numbers in
			// the range 1 - 999.
			num1 = 1 + rand() % 999;
			num2 = 1 + rand() % 999;

			// Make sure num2 <= num1...
			while (num2 > num1)
				num2 = 1 + rand() % 999;

			// Get the correct answer.
			result = num1 - num2;
			if(result==useranswer)
						{

							correct++;
						}

			// Display the problem.
			cout << "\n\n";
			cout << " " << setw(4) << num1 << endl;
			cout << " -" << setw(4) << num2 << endl;
			cout << " " << "----" << endl;
			cout << " ";
			break;

		case 3: // Multiplication problem
			// Generate two random numbers. The first in
			// the range 1 - 100, the second in the
			// range 1 - 9.
			num1 = 1 + rand() % 100;
			num2 = 1 + rand() % 9;

			// Calculate the correct answer.
			result = num1 * num2;
			if(result==useranswer)
						{

							correct++;
						}

			// Display the problem.
			cout << "\n\n";
			cout << " " << setw(4) << num1 << endl;
			cout << " *" << setw(4) << num2 << endl;
			cout << " " << "----" << endl;
			cout << " ";
			break;

		case 4: // Division problem with no remainder
			// Generate a single digit divisor.
			num2 = 1 + rand() % 9;

			// Generate a number that is a multiple
			// of num2...
			num1 = num2 * (rand() % 50 + 1);

			// Calculate the correct answer.
			result = num1 / num2;
			if(result==useranswer)
						{

							correct++;
						}

			// Display the problem.
			cout << "\n\n";
			cout << " " << num1 << " / " << num2 << " = ";
			break;

		case 5: // The user chose to quit the program.
			cout << "Thank you for using Math Tutor.\n\n";
			break;
		}

		// If student selected a problem, get and evaluate the answer.
		if (choice >= 1 && choice <= 4) {
			cin >> useranswer;
			if (useranswer == result)
				cout << "\n\nCongratulations! That's right.\n\n";

			else
				cout << "\n\nSorry, the correct answer is " << result
						<< ".\n\n";

		}
	} while (choice != 5); // Loop again if student did not choose to quit.
	cout<< "You worked on 5 problems\n";
	cout << "1 addition problem\n";
	cout << "2 subtraction problems\n";
	cout << "1 multiplication problem\n";
	cout << "2 division problems\n";
	cout << "You got" <<correct<< "problems correct!\n";
	cout << "Your percent correct is";
	return 0;
}


Obviously the bottom needs worked on to count how many of each problem I worked on but I cant get it to display how many correct problems there were
Do you realize that C++ executes code from the top down?

Look at where you're incrementing the "counter". Is this done before or after you have the user enter the answer to the "test"?

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
...
  	        // Produce a problem.
		switch (choice) {
		case 1: // Addition problem
			// Generate two random numbers in
			// the range 1 - 500.
			num1 = 1 + rand() % 500;
			num2 = 1 + rand() % 500;

			// Calculate the correct answer.
			result = num1 + num2;
		if(result==useranswer)
		{
			correct+=1;
		}


			// Display the problem.
			cout << "\n\n";
			cout << " " << setw(4) << num1 << endl;
			cout << " +" << setw(4) << num2 << endl;
			cout << " " << "----" << endl;
			cout << " ";
			break;
...


		// If student selected a problem, get and evaluate the answer.
		if (choice >= 1 && choice <= 4) {
			cin >> useranswer;
			if (useranswer == result)
				cout << "\n\nCongratulations! That's right.\n\n";

			else
				cout << "\n\nSorry, the correct answer is " << result
						<< ".\n\n";

		}
...


Wouldn't it make more sense to increment the "counter" when you tell the user the answer is correct?

Last edited on
Wouldn't it make more sense to increment the "counter" when you tell the user the answer is correct?


So are you saying I need an if statement in line 33 for the counter?

Also when I am running my number guess game for the 2nd time the range is off..

Do you want to play again? [y/n]
y
Welcome to the game Hi-Lo..
Enter an integer that is between 1 and 100:
56
56 is too low
Enter an integer that is between 57 and 63

64 being the upper bound of the previous game
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
bool repeat = true;
	while (repeat) {
		cout << "Welcome to the game Hi-Lo..\n";
		cout << "Enter an integer that is between 1 and 100: " << endl;
		cin >> num;

		while (num != random) {
			if (num > random) {
				cout << num << " is too high" << endl;
				max = num - 1;
			} else {
				cout << num << " is too low" << endl;
				min = num + 1;
			}
			cout << "Enter an integer that is between " << min+num << " and " << max << endl;

			cin >> num;

			if (num > max || num < min) {
				cout
						<< "the integer you enter is not within range, enter a number between "
						<< min << " and " << max << endl;
				cin >> num;
				outofrange = outofrange + 1;
			} else
				rightguess = rightguess + 1;
		}
		cout << " It took you " << rightguess
				<< " valid guesses to find the number " << endl;
		cout << " You had " << outofrange << " out of range guesses " << endl;

		cout << "Do you want to play again? [y/n]" << endl;
		char answer;
		cin >> answer;
		repeat = answer == 'y';
	}
	cout << "Thank you for playing Hi-Lo. Bye!" << endl;

	return 0;
}

Last edited on

So are you saying I need an if statement in line 33 for the counter?

You already have an if() statement, why not just use that statement? By the way the "counter" I'm talking about is your variable "correct".

Also when I am running my number guess game for the 2nd time the range is off..

One program at a time please, it'll get too confusing if you keep jumping between two different programs.


You already have an if() statement, why not just use that statement? By the way the "counter" I'm talking about is your variable "correct".

I'm confused...

1
2
3
4
5
6
7
8
9
// If student selected a problem, get and evaluate the answer.
		if (choice >= 1 && choice <= 4) {
			cin >> useranswer;
			if (useranswer == result)
				cout << "\n\nCongratulations! That's right.\n\n";

			else
				cout << "\n\nSorry, the correct answer is " << result
						<< ".\n\n";

Are you saying put correct++ After the congrats message? If I do that I get a syntax error.. Or do I put it with the if statement that has useranswer and result? Do I need to initialize correct to 1 or 0?
1
2
3
4
5
6
7
8

			if (useranswer == result) //right here?
				cout << "\n\nCongratulations! That's right.\n\n";
                              //or right here? I get a syntax error here?

			else
				cout << "\n\nSorry, the correct answer is " << result
						<< ".\n\n";

I get a syntax error here?

Do realize that without the "optional" braces a control statement will only have a one line body, any other lines are outside the if() statement, and in this case that means that the "else" has no if() statement?

This is why it is usually recommend to always use braces with all control statements, even when not technically required, at least until you are much more familiar with the language.




Last edited on
Do realize that without the "optional" braces a control statement will only have a one line body, any other lines are outside the if() statement, and in this case that means that the "else" has no if() statement?

This is why it is usually recommend to always use braces with all control statements, even when not technically required, at least until you are much more familiar with the language.


1
2
3
4
5
6
7
8
if (useranswer == result) //right here I put a {
				cout << "\n\nCongratulations! That's right.\n\n";
                             //and another one here }

			else //but what about this? Do I need a { here?
				cout << "\n\nSorry, the correct answer is " << result
						<< ".\n\n";



edit: tried this, didnt do anything..
1
2
3
4
5
6
7
8
9
10
11
12
13
	// If student selected a problem, get and evaluate the answer.
		if (choice >= 1 && choice <= 4) {
			cin >> useranswer;
			if (useranswer == result) {
				if (useranswer == result && result == correct)
					correct++;
				cout << "\n\nCongratulations! That's right.\n\n";

			}

			else
				cout << "\n\nSorry, the correct answer is " << result
						<< ".\n\n";
Last edited on
Pages: 12