Computer-aided-instruction environment questions

I'm writing a CAI program to help kids learn math and I am running into a few logic errors. Other resources I have tried to use to debug the program have been unhelpful thusfar. The program works at lower difficulty levels most of the time, but at higher maximum number counts and random operations the program shows a number of bugs. For debugging purposes, I've told the program to output the correct answer after the user inputs their own.

- At a difficulty level of 5, where the operation applied to the two numbers is random, the display function does not correctly choose which operation to perform and leaves the string as "default". Why is this the case?

- When numbers are large, the correct answer is an overflow value instead of an actual number despite being within the data value limit of a double.

- The program loops infinitely when anything other than a number is inputted by the user. This can be solved if I allow the answer to be a string, but that would make things very messy. Is there a way around this?

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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
#include <iostream>
#include <cmath>
#include <string>
#include <ctime>
#include <cstdlib>

using namespace std;

void generate(double  &a, double &b, int diff, int mathtype)
{
	
	switch (diff)
	{
	case 1:
		a = (rand() % 10);
		b = (rand() % 10);
		break;
	case 2:
		a = (1 + rand() % 99);
		b = (1 + rand() % 99);
		break;
	case 3:
		a = (1+rand() % 999);
		b = (1+rand() % 999);
		break;
	}
	
}


void display(double a, double b, int mathtype, int diff, int opp)
{
	string(operation) = "default";

	if (diff <= 5)
	{
		opp = (rand() % 4 + 1);
		switch (opp)
		{
		case 1:
			operation = "plus";
			break;
		case 2:
			operation = "minus";
			break;
		case 3:
			operation = "divided by";
			break;
		case 4:
			operation = "times";
			break;
		}
	}

	switch (mathtype)
	{
	case 1:
		cout << "What is " << a << " plus " << b << "?" << endl;
		break;
	case 2:
		cout << "What is " << a << " minus " << b << "?" << endl;
		break;
	case 3:
		cout << "What is " << a << " divided by " << b << "?" << endl;
		break;
	case 4:
		cout << "What is " << a << " times " << b << "?" << endl;
		break;
	case 5:
		cout << "What is " << a << " " << operation << " " << b << "?" << endl;
	
	}

}

void percent(int &c, int &total, char choice, bool choices)
{
	double score = 0;
	score = static_cast <double> (c) / static_cast<double>(total);
	if (score <= .75)
	{
		cout << "This is too hard for you, just give up" << endl;
		cout << "Your average is " << score*100 << "%" << endl;
		total = 0;
		c = 0;
		
	}
	else
	{
		cout << "You passed, go away and let someone else use the program" << endl;
		cout << "Your average is " << score*100 << "%" << endl;
		total = 0;
		c = 0;
	}
}


void correct(int i, int &total, int &c, char choice, bool choices)
{
	int q = rand() % 4 + 1;
	total++;
	c++;
	switch (q)
	{
		
	case 1:
		cout << "Cool, you can math, do it " << 10 - i << " more times" << endl;
		
		break;
	case 2:
		cout << "Riveting, do it " << 10 - i << " more times" << endl;
		break;
	case 3: 
		cout << "Wow you totally smashed that one, do it " << 10 - i << " more times" << endl;
		break;
	case 4: 
		cout << "This answer isn't incorrect, do it " << 10 - i << " more times" << endl;
		break;
	}
	if (total >= 10)
	{
		percent(c, total, choice, choices);
	}

}

void incorrect(int usr, double ans, int &total, int &c, char choice, bool choices)
{


	cout << "Looks like you can't do math, try again" << endl;

	do
	{
		
		cin >> usr;
		
		int q = rand() % 4 + 1;

		switch (q)
		{
	
		case 1:
			cout << "Wow, you're pretty stupid" << endl;
		total++;
			
			break;
		case 2:
			cout << "You have no future" << endl;
			total++;
			
			break;
		case 3:
			cout << "That is easy, you are not trying" << endl;
			total++;
			
			break;
		case 4:
			cout << "Your IQ is even lower than projected" << endl;
			total++;
			
			break;
		}
		if (total == 10)
		{
			percent(c, total, choice, choices);
		}

	} while (usr != ans);

}

void compare(int usr, double &ans, int i, double a, double b, int &c, int &total, char choice, bool choices, int opp, int diff, int mathtype)
{ 

	if (diff == 5)
	{
		switch (opp)
		{
		case 1:
			ans = a + b;
			break;
		case 2:
			ans = a - b;
			break;
		case 3:
			ans = a / b;
			break;
		case 4:
			ans = a * b;
			break;
		}
	}
	else
	{
		switch (mathtype)
		{
		case 1:
			ans = a + b;
			break;
		case 2:
			ans = a - b;
			break;
		case 3:
			ans = a / b;
			break;
		case 4:
			ans = a * b;
			break;
		}
	}
	
	cout << ans << endl;

	if (usr != ans)
	{
		incorrect(usr, ans, total, c, choice, choices);
	}
	else if (usr == ans)
	{
		correct(i, total, c, choice, choices);
	}
}

void clear()
{
	for (int i = 0; i <= 10; i++)
	{
		cout << endl;
	}
}

int main()
{
	srand(time(0));
	double a, b, ans;
	int usr, diff, mathtype, opp=0;
	int c=0, total=0;
	char choice = 'y';
	bool choices = true;
	
	while (choices != false)
	{ 
		cout << "Enter the maximum count of digits from 1 to 3" << endl;
		cin >> diff;
		cout << "Enter the operation you want to practice: 1 is addition, 2 is subtrction, 3 is division, 4 is multiplication, and 5 is random" << endl;
		cin >> mathtype;
		for (int i=0; i < 10; i++ )
		{
			generate(a, b, diff, mathtype);
			display(a, b, mathtype, diff, opp);
			cin >> usr;
			compare(usr, ans, i, a, b, c, total, choice, choices, opp, diff, mathtype);

		}

		clear();
		choices = true;
		
	}
	

	return 0;
}
Line 33 looks strange. Better remove the parentheses.

Line 35: Why do you use diff here? Isn't it the number of digits?

Line 37: opp will not be changed outside display(...) because it is not passed by reference. I'd suggest that you omit opp and modify mathtype instead (you would need to pass it by reference as well).
Topic archived. No new replies allowed.