I am trying to get this math program to work and I'm struggling with the final part.

Hi everyone!I am new to c++ and I am working on this math program. The program essentially gives the user 5 options to work with. Addition, subtraction, multiplication,division and mixture of all the previous. I am stuck at the part where I have to return the choice with mixture of all the above 4(+,-,*,/). I do have some idea how to go about doing it but I would need some help.

I would like to elaborate a little more about the program. The program allows the user to work with 2 difficulty levels and it gives the user 10 attempts for any problem in any category and after that it calculates the responses. If the correct responses have a percentage less than 75 the program prints out a specific statement and if it's higher then it tells the user to go on the next level. Haven said all that I would post my code and I am open to any scrutiny as I am eager to learn. I appreciate any help. cheers

PS I did'nt post my whole program as it is 500 lines of code.

for option 5 which is mixture of all programs I had this in my mind.

void mixLvl1(int add, int sub, int mult, double div)
{
srand(time(0));
int n1 = rand() % 10;
int n2 = rand() % 10;

cout << "what is " << n1 << " + " << n2 << " ?" << endl;
add = n1 + n2;

srand(time(0));
int n1 = rand() % 10;
int n2 = rand() % 10;

cout << "what is " << n1 << " - " << n2 << " ?" << endl;
sub = n1 - n2;

srand(time(0));
int n1 = rand() % 10;
int n2 = rand() % 10;

cout << "what is " << n1 << " * " << n2 << " ?" << endl;

srand(time(0));
double n1 = 2 + (2 * rand()) % 10;
double n2 = 2 + (2 * rand()) % 10;

cout << "what is " << n1 << " / " << n2 << " ?" << endl;
div = n1 / n2;

}

and then for the main function. I know I can't equal my void function = int answers as this is illegal but how do I call this function so that I can check the number of correct and incorrect responses and have mixture of questions as well.

int main()

int input;
cin >> input;

if (input == 5)
{
int diff;
cout << "What level of difficulty would you like? 1 or 2" << endl;
cin >> diff;
if (diff == 1)
{
bool flag = false;
double cans = 0;
while (!flag)


//
{
int useranswer;
int add,sub,div,mult;

for (int i = 0; i < 10; i++)
{
mixLvl1(add,sub,div,multi)?????
cin >> useranswer;
while (answer != useranswer && i < 9)

{
wrongAns();
cin >> useranswer;
i++;
}

if (answer == useranswer)
{
cans++;
CorrectAns();
}

}
results(cans);
main();
}
}





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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
#include<iostream>
#include<cstdlib>
#include<ctime>

using namespace std;

void CorrectAns();
void wrongAns();
void results(int cans);
  
double DivLvl1()
{
	srand(time(0));
	double n1 = 2 + (2 * rand()) % 10;
	double n2 = 2 + (2 * rand()) % 10;

	cout << "what is " << n1 << " / " << n2 << " ?" << endl;

	return n1/n2;
}

double DivLvl2()
{
	srand(time(0));
	double n1 = 2 + (2 * rand()) % 100;
	double n2 = 2 + (2 * rand()) % 100;

	cout << "what is " << n1 << " / " << n2 << " ?" << endl;

	return n1/n2;
}


void CorrectAns()//correct answer responses

	{
		srand(time(0));
		int n1 = (rand() % 4) + 1;

		switch (n1)
		{
		case 1:
			cout << "Very Good! " << endl;
			break;
		case 2:
			cout << "Excellent!" << endl;
			break;
		case 3:
			cout << "Nice Work!" << endl;
			break;
		case 4:
			cout << "Keep up the good work!" << endl;
			break;
		}
	}


void wrongAns()//incorrect answer responses

	{
		srand(time(0));
		int n1 = (rand() % 4) + 1;

		switch (n1)
		{
		case 1:
			cout << "No. Please try again. " << endl;
			break;
		case 2:
			cout << "Wrong. Try once more" << endl;
			break;
		case 3:
			cout << "Don't give up!" << endl;
			break;
		case 4:
			cout << "No. Keep trying." << endl;
			break;
		}
	}


void results(double cans)//calculates the percentage of correct and incorrect responses
{
	double perc=0;
	
	perc = (cans / 10) * 100;

	if (perc < 75)
		cout << "Please ask your teacher for extra help." << endl;
	else
		cout << "Congratulations, you are ready to go to the next level!" << endl;
		
}

int main()
{
	cout << "Welcome to Math Tutor. This program will help you learn some basic math." << endl;
	cout << "Select the type of problems you would like to work with. " << endl;
	cout << "Enter '1' for Addition problems. " << endl;
	cout << "Enter '2' for Subtraction problems. " << endl;
	cout << "Enter '3' for Multiplication problems. " << endl;
	cout << "Enter '4' for Division problems. " << endl;
	cout << "Enter '5' for a mixture of all the above problems. " << endl;

	int input;
	cin >> input;

if (input == 4)//Division Problems
	{
		int diff;
		cout << "What level of difficulty would you like? 1 or 2" << endl;
		cin >> diff;
		if (diff == 1)
		{
			bool flag = false;
			double cans = 0;
			while (!flag)


				//Division level 1 difficulty questions
			{
				double useranswer;

				for (int i = 0; i < 10; i++)
				{
					double answer = DivLvl1();
					cin >> useranswer;
					while (answer != useranswer && i < 9)

					{
						wrongAns();
						cin >> useranswer;
						i++;
					}

					if (answer == useranswer)
					{
						cans++;
						CorrectAns();
					}

				}
				results(cans);
				main();
			}
		}
		else
		{
			bool flag = false;
			double cans = 0;
			while (!flag)


				//Division level 2 difficulty questions
			{
				double useranswer;

				for (int i = 0; i < 10; i++)
				{
					double answer = DivLvl2();
					cin >> useranswer;
					while (answer != useranswer && i < 9)

					{
						wrongAns();
						cin >> useranswer;
						i++;
					}

					if (answer == useranswer)
					{
						cans++;
						CorrectAns();
					}

				}
				results(cans);
				main();

			}

		}
	}


	return 0;
}
I am still stuck at this issue. Although I came up with this but now it's telling me that case is skipping my 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
void mixLvl1()

{
	
	srand(time(0));
	int n1 = (rand() % 4) + 1;
	int ans;
	double cans=0;
	

	switch (n1)
	{
	case 1:	
		 AddLvl1();
		cin >> ans;
		int answer1 = AddLvl1();
                if (ans == answer1)
                 cans++;
		break;
	case 2:
		SubLvl1();
		cin >> ans;
		int answer2 = SubLvl1();
                if(ans==answer2)
                cans++;
		break;
	case 3:
		DivLvl1();
		cin >> ans;
		int answer3 = DivLvl1();
                if (ans == answer3)
                cans++;
		break;
	case 4:
		multiLvl1();
		cin >> ans;
		int answer4 = multiLvl1();
                if(ans==answer4)
                cans++;
		break;
	}
}
Last edited on
helPPp?
closed account (48T7M4Gy)
Just write a main program, include the switch for testing it, print the results ( via selected cout statements inserted in the code).

Better still run a test rig on the random generation couple of lines and test those as a unit.

Run the switch as a separate unit and hard code an option at a time with suitable cout's.

Trying to debug 180 lines of code is often a waste of time when you can narrow down by unit testing - it never fails.

Of course, if you need help on a particular unit show us the output, code and line number etc.
Hi Kemort!

Thanks for you input with regards to my query. Alright so as I mentioned I'm very new to c++ and I struggle a lot.

I tried the swtich statement it gives me an error saying case is going to skip label.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
void mixLvl1()

{
	
	srand(time(0));
	int n1 = (rand() % 4) + 1;
	int ans;
	double cans=0;
	

	switch (n1)
	{
	case 1:	
		int answer1 = AddLvl1();//.......Its giving me an error over here 
		cin >> ans;
		
                if (ans == answer1)
                 cans++;
		break;

closed account (48T7M4Gy)
The first thing I notice with that line is I can't find where the function AddLvl1() is defined in the rest of your program.
thanks for the help I fixed the issues the program works!
closed account (48T7M4Gy)
Excellent
Topic archived. No new replies allowed.