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

Pages: 12
Jun 21, 2017 at 5:55pm
//right here I put a {

Well did you try the two braces (without the comments of course)? Did they work?

//but what about this? Do I need a { here?


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 Jun 21, 2017 at 5:55pm
Jun 21, 2017 at 6:13pm
I added braces, no error message (thank god) but nothing is being counted or accumulated..
1
2
3
4
5
6
7
8
9
10
11
12
13
14
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";
			}
		}


At the beginning of my code I have int correct=0; Is this correct?
Jun 21, 2017 at 6:20pm
Why do you have that second if statement inside the first one?

1
2
3
4
5
6
7
8
9
			cin >> useranswer;
			if (useranswer == result) {
				correct++;
				cout << "\n\nCongratulations! That's right.\n\n";

			}
			else{
				cout << "\n\nSorry, the correct answer is " << result << ".\n\n";
			}
Jun 21, 2017 at 6:26pm
I fixed it..and it's still saying I answered 0 correct in the end
Jun 21, 2017 at 6:34pm
Post the complete "new" code.


Jun 21, 2017 at 6:36pm
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
#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;
	int wrong=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;

			// 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.\n\n";
			break;
		}

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

			}

			else{
				cout << "\n\nSorry, the correct answer is " << result
						<< ".\n\n";
				wrong++;
			}
		}
	} while (choice != 5); // Loop again if student did not choose to quit.
	cout << "You worked on blank problems\n";
	cout << " addition problem\n";
	cout << " subtraction problems\n";
	cout << " multiplication problem\n";
	cout << " division problems\n";
	cout << "You got" << correct << "problems correct!\n";
	cout << "Your percent correct is";
	return 0;
}

I got it to count now (yay! Thank you!)
BUT How do I count how many of each problem I worked on??
Jun 21, 2017 at 6:50pm
For that you'll need to either have an array big enough to hold all of your choices, or individual variables for each choice. Then you need to keep track of the correct answers for each choice.

Jun 22, 2017 at 6:19pm
closed account (1vf9z8AR)
add an srand(time(null))
Jun 22, 2017 at 8:35pm
suyashsing234 wrote:
add an srand(time(null))

Did you not see line 10?
Jun 23, 2017 at 2:19am
I believe he means putting the srand(time(0)) in line 18
Jun 23, 2017 at 2:29am
I believe he means putting the srand(time(0)) in line 18

There's already a srand() call on line 10, so why do you want to add it to line 18? The srand() function only needs to be called once.

Jun 23, 2017 at 9:46am
Well...we wants the seed to be different each time the loop runs..(Because the random wont be random with same seed, yes ?)
Correct me if i an wrong
Jun 23, 2017 at 11:49am
Correct me if i an wrong

Okay, consider yourself corrected.

The srand() function only needs to be called once to seed the random number generator. Calling srand() multiple times in a loop using time(0) as the seed will likely lead to reseeding the random number generator with the same seed multiple times which will usually cause the random numbers to "restart" with the same sequence. Remember that time(0) only changes once every second, and using the same seed with srand() will cause the random number generator to restart at the same point in the sequence, causing repeats of the random numbers.



Topic archived. No new replies allowed.
Pages: 12