C++ Simple Math Caclulator Help!

I need some help with me C++ assignment. Here are the requirements:

Choices should be as follows:

1.Addition
2.Subtraction
3.Multiplication
4.Quit Tutor

/*

My name

Date

Title

C++

*/

?

?

?

#include <iostream>

#include <cmath>

#include <ctime>

#include <cstdlib>

using namespace std;

?

int num1,

num2,

num3,

response,

answer1,

wrong,

correct,

ran1,

y,

z,

x;

int main()

{

for (x = 0; x < 1000; x++){

system("cls");

cout << "my name and title" << endl << endl;

cout << "Please enter what Method of Arithmetic you would like to do" << endl << endl;

cout << "To Select Addition Please Press (1)" << endl << endl;

cout << "To Select Subtraction Please Press (2)" << endl << endl;

cout << "To Select Multiplication Please Press (3)" << endl << endl;

cout << "To Quit the Math Tutor Please Press (4)" << endl << endl;

cin >> response;

cout << endl << endl;

y = 0;

z = 0;

?

for (y = 0; y < 10; y++){

if (response == 1)

{

unsigned seed = time(0);

srand(seed);

num1 = rand() % 999;

num2 = rand() % 999;

num3 = rand() % 999;

system("cls");

cout << "Attempt 1/10" << endl << endl;

cout << "Compute the following" << endl << endl;

cout << num1 << " + " << num2 << " = ?" << endl << endl;

cin >> answer1;

cout << endl << endl;

if (answer1 != num1 + num2)

{

cout << "Wrong answer" << endl << endl;

wrong += 1;

system("pause");

}

if (answer1 == num1 + num2)

{

cout << "Right answer" << endl << endl;

correct += 1;

system("pause");

}

}

}

for (z = 0; z < 10; z++){

if (response == 2)

{

unsigned seed = time(0);

srand(seed);

num1 = rand() % 999;

num2 = rand() % 999;

num3 = rand() % 999;

system("cls");

cout << "Compute the following" << endl << endl;

cout << num1 << " - " << num2 << " = ?" << endl << endl;

cin >> answer1;

cout << endl << endl;

if (answer1 != num1 - num2)

{

cout << "Wrong answer" << endl << endl;

wrong += 1;

system("pause");

}

if (answer1 == num1 - num2)

{

cout << "Right answer" << endl << endl;

correct += 1;

system("pause");

}

}

}

?

for (z = 0; z < 10; z++){

if (response == 3)

{

unsigned seed = time(0);

srand(seed);

num1 = rand() % 999;

num2 = rand() % 999;

num3 = rand() % 999;

system("cls");

cout << "Compute the following" << endl << endl;

cout << num1 << " * " << num2 << " = ?" << endl << endl;

cin >> answer1;

cout << endl << endl;

if (answer1 != num1 * num2)

{

cout << "Wrong answer" << endl << endl;

wrong += 1;

system("pause");

}

if (answer1 == num1 * num2)

{

cout << "Right answer"<< endl << endl;

correct += 1;

system("pause");

}

}

}

if (response == 4)

{

exit(1);

}

system("cls");

cout << "You answered " << correct << " correctly out of 10" << endl << endl;

cout << "The percentage score is " << (correct * 10) << "%" << endl << endl;

system("pause");

}

?

return 0;

}

Thank you in advance!!!
Last edited on
Here is the program with the correct spacing...

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
157
158
159
160
161
162
163
/*

my name
date
Better Math Tutor Calculator
C++

*/




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


int num1,
num2,
num3,
response,
answer1,
wrong,
correct,
ran1,
y,
z,
x;

int main()
{
	for (x = 0; x < 1000; x++){

		system("cls");

		cout << "title"<< endl << endl;
		cout << "Please enter what Method of Arithmetic you would like to do" << endl << endl;
		cout << "To Select Addition Please Press    (1)" << endl << endl;
		cout << "To Select Subtraction Please Press    (2)" << endl << endl;
		cout << "To Select Multiplication Please Press     (3)" << endl << endl;
		cout << "To Quit the Math Tutor Please Press    (4)" << endl << endl;

		cin >> response;

		cout << endl << endl;

		y = 0;
		z = 0;


		for (y = 0; y < 10; y++){

			if (response == 1)
			{

				unsigned seed = time(0);
				srand(seed);
				num1 = rand() % 999;
				num2 = rand() % 999;
				num3 = rand() % 999;

				system("cls");
				cout << "Attempt 1/10" << endl << endl;
				cout << "Compute the following" << endl << endl;
				cout << num1 << " + " << num2 << " = ?" << endl << endl;
				cin >> answer1;
				cout << endl << endl;
				if (answer1 != num1 + num2)
				{
					cout << "wrong'<< endl << endl;
					wrong += 1;
					system("pause");
				}
				if (answer1 == num1 + num2)
				{
					cout << "right'<< endl << endl;
					correct += 1;
					system("pause");
				}

			}
		}

		for (z = 0; z < 10; z++){

			if (response == 2)
			{

				unsigned seed = time(0);
				srand(seed);
				num1 = rand() % 999;
				num2 = rand() % 999;
				num3 = rand() % 999;

				system("cls");
				cout << "Compute the following" << endl << endl;
				cout << num1 << " - " << num2 << " = ?" << endl << endl;
				cin >> answer1;
				cout << endl << endl;
				if (answer1 != num1 - num2)
				{
					cout << "wrong"<< endl << endl;
					wrong += 1;
					system("pause");
				}
				if (answer1 == num1 - num2)
				{
					cout << "right"<< endl << endl;
					correct += 1;
					system("pause");
				}

			}
		}


		for (z = 0; z < 10; z++){

			if (response == 3)
			{

				unsigned seed = time(0);
				srand(seed);
				num1 = rand() % 999;
				num2 = rand() % 999;
				num3 = rand() % 999;

				system("cls");
				cout << "Compute the following" << endl << endl;
				cout << num1 << " * " << num2 << " = ?" << endl << endl;
				cin >> answer1;
				cout << endl << endl;
				if (answer1 != num1 * num2)
				{
					cout << "wrong"<< endl << endl;
					wrong += 1;
					system("pause");
				}
				if (answer1 == num1 * num2)
				{
					cout << "right" << endl << endl;
					correct += 1;
					system("pause");
				}

			}
		}
		if (response == 4)
		{
			exit(1);
		}
		system("cls");
		cout << "You answered " << correct << " correctly out of 10" << endl << endl;
		cout << "The percentage score is " << (correct * 10) << "%" << endl << endl;
		system("pause");
	}


	return 0;

} 
Last edited on
Thanks!
I desperately need someone's help please! Thank you!!!
I think I would try to break this into separate functions instead of having one huge main function.

You only need to seed the random function once at the beginning of the program, not every time you go into a set of problems.

At all times the student should know what question in the set he/she is working on out of 10 questions.


If you have a for loop, you know the number of the current problem and the total number of problems, so you'll have this info to print to the screen.

The user should also know how many attempts he/she has taken per question. If the user answers a problem incorrectly, the user should be notified that the response is incorrect and be given multiple chances to answer the question correctly. With each incorrect answer, the attempt count should increase.


Looks like the assignment allows you to choose how many guesses the person gets per question. You need to keep a counter of how many attempts are made
for that particular question and for the overall set of questions. I'd probably use a while loop to let them enter an answer until they get it correct or until they run out of attempts, whichever comes first.

The user's grade should be calculated as (correctAnswers / NumberOfAttempts * 100).


You should be keeping track of the total number of attempts, and then you just need to track the number of correct answers. If there are a fixed number of questions, you can track either the number of wrong or correct and easily be able to calculate the other.

The initial menu should be re-displayed and the user given the opportunity to attempt another set of random problems from the chosen operation.


I'd probably put the menu display into its own function and validate that they enter a valid option. Then call the appropriate function for the problem set they chose, or exit the program if they want to exit.


There's a section on file input/output here.
http://www.cplusplus.com/doc/tutorial/files/

Someone also linked this pdf on files in another post which might be helpful.
http://www.umich.edu/~eecs381/handouts/filestreams.pdf
Thank you very much for your input. Could you possibly give me an example of how this stuff should look in my program? I'm still pretty new to C++ (started last week).
Here's one example.

If you have a for loop, you know the number of the current problem and the total number of problems, so you'll have this info to print to the screen.


1
2
3
4
5
6
7
8
9
10
//using a const int that can easily be updated to adjust the number of problems
const int numSet = 10; 
	
for (int i = 1; i <= numSet; i++)
{
	std::cout << "Now on problem " << i << " of " << numSet << "\n";
	//code for problem details
}
Now on problem 1 of 10
Now on problem 2 of 10
Now on problem 3 of 10
Now on problem 4 of 10
Now on problem 5 of 10
Now on problem 6 of 10
Now on problem 7 of 10
Now on problem 8 of 10
Now on problem 9 of 10
Now on problem 10 of 10
Different ways to do this, but here's a quick example of breaking out into functions.

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
//function prototypes
int displayMenu();
void arithmeticSet();
void subtractionSet();
void multiplicationSet();

int main() 
{
	int choice = 0;
	
	do{
		choice = menuSelect();
		switch (choice)
		{
		case 1:	arithmeticSet();//call Arithmetic function
			break;
		case 2:	subtractionSet();//call Subtraction function
			break;
		case 3: multiplicationSet();//call Multiplication function
			break;
		case 4: std::cout << "Goodbye!" << endl;
			break;
		default: std::cout << "Unrecognized selection" << endl;
			break;
		}
	}while (choice != 4)
	
	return 0;
}

int displayMenu()
{
	int menuSelect = 0;
	std::cout << "1. Addition\n"; 
	std::cout << "2. Subtraction\n";
	std::cout << "3. Multiplication\n";
	std::cout << "4. Quit Tutor\n\n";
	std::cout << "Enter Selection (1-4): ";

	cin >> menuSelect;
	//while the menu selection is not within the range of 1 to 4
	while (!(menuSelect >= 1 && menuSelect <= 4))
	{
		cout << "Invalid Selection - Please Re-enter (1-4): ";
		cin >> menuSelect;
	}
	return menuSelect;
}

//additional function definitions here 
Last edited on
I tried to insert the attempts (1/10 etc...) and it works great but after question 10, it keeps on repeating 1-10 over and over.

Last edited on
We can't do your homework for you. We'll try to help out if there are specific things you don't understand, but you need to do your own work. :)
Could you show me how to make the user keep on entering values if they get it incorrect up until they get it right? Thanks
lines 40-46 in the code I posted above have a while loop that validates input. Instead of validating the menu selection, you'd be checking if the answer the user entered was equal to the correct solution.
I finally figured it out, Thanks for yalls help!
Topic archived. No new replies allowed.