Help with math tutor

hey guys I'm writing a math tutor program and i need it to keep track of the number of correct scores as well as number tries and i also want to have 5 answer options,and also give the user up to 3 tries to get a question right. i have the basis of the code but the others elude me.
I'm also trying to write this without any global variables. thanks.
here's what i have so far
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
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
#include <iostream>
#include <iomanip>
#include <cstdlib>
#include <ctime>
#include <string>

using namespace std;

char Menu();                              //prototypes
void difficulty(int &);
void generate(double &, double &, int );
void addition(double &, double, double);
void subtraction(double &, double, double);
void multiplication(double &, double, double);
void division(double &, double, double, char);
void Ans(double &);



int main()
{
	double correctans, num1, num2;
	char choice;
	char line[80];
	int diff; 

	time_t now = time(0);
	string name;
	srand(now);  // Seed the random number generator.

	//cout << ctime(&now) << endl;

	cout << "what is your first and last name " << endl;
	getline(cin, name);

	for (unsigned i = 0; i < name.length(); i++)
		if (name[i] == ' ') name[i] = '_';

	name += ".txt";
	cout << endl << endl << endl;

	cout << "Welcome to the Math Skills Program " << endl;
	cout << "This program allows you to practice your math skills" << endl;
	cout << "Choose what to practice in the menu shown below." << endl;
	cout << endl << endl << endl;

	difficulty( diff);



	bool test = true; 
	while (test)
	
	{
		
		now = time(0);
//		cout << endl << "Problem start time:" << ctime(&now) << endl;

		generate(num1, num2, diff);
		choice = Menu();

		switch (choice)
		{
		case 'a': 
			addition(correctans, num1, num2);
			break;

		case 'b': 
			subtraction(correctans, num1, num2);
			break;

		case 'c': 
			multiplication(correctans, num1, num2);
			break;

		case 'd': 
			division(correctans, num1, num2, choice);
			break;

		case 'q': test = false;// The user chose to quit the program.
			cout << "Thank you for using Math Tutor program.\n\n";
			return 0;
			break;
		}

	}
	if (choice != 'q')
	{
		cout << "incorect input please try again" << endl;
		cin >> choice; // Loop again
	}
	 return 0;

}

char Menu()
{
	char choice;

	{
		cout << "Arithmetic Practice program\n" << endl;


		// Display the menu and get a choice.

	
		//cout << "------------------------------\n";
		cout << "---------------------------------------------------------" << endl;
		cout << "ARITHMETIC PRACTICE PROGRAM " << endl;
		cout << "MAIN MENU" << endl;
		cout << "-------------------------------------------------------- - " << endl;
		cout << "a. Addition \n";
		cout << "b. Subtraction\n";
		cout << "c. Multiplication\n";
		cout << "d. Division\n";
		cout << "q. Quit the program\n";
		cout << "------------------------------\n";
		cout << "Enter your choice (a b c d q): ";
		cin >> choice;
	}

	// Validate the choice.
	choice = tolower(choice);
	if (choice >= 'a' && choice <= 'd' || choice == 'q')
		cout << "You entered: '" << choice << "', your input is valid " << endl;
	else

	{
		cout << "The valid choices are a, b, c, "
			<< "d, and q. Please choose: ";
		cin >> choice;
	}
	cout << endl;
	return choice;
}

void difficulty(int & diff)
{
	//double studentans;
	char line[80];

	cout << "Select diffculty. Please choose" << endl;
	cout << " 1 for 1 digit\n"
		<< " 2 for 2 digits\n"
		<< " 3 for 3 digits\n"
		<< " 4 for 4 digits\n"
		<< " 5 for 5 digits\n"
		<< endl;
	cout << endl << "> ";

	cin >> line;
	diff = atoi(line);

	while (diff < 1 || diff >5)
	{
		cout << "Enter 1-5 only " << endl;
		cin >> line;
		diff = atoi(line);
		cin.clear();
		cin.ignore(1000, '\n');
	}

}

void generate( double & num1, double & num2, int diff)
{
	int min, max;
	min = pow(10., diff - 1);
	max = pow(10., diff);

	num1 = min + rand() % (max - min);
	num2 = min + rand() % (max - min);

}


void Ans(double & correctans)
{
	double studentans;
	// char choice;
	// if (choice >= 'a' && choice <= 'd')
	// {
		cin >> studentans;
		if (studentans == correctans)
			cout << "\n\nCongratulations! That's right.\n\n";
		else
			cout << "\n\nSorry, the correct answer is " << correctans << ".\n\n";
	// }
}

void addition(double & correctans, double num1, double num2)
{
	double studentans; //Generate two random numbers in the range 1 - 500.

	cout << "Prepare to add" << endl;
	
	//difficulty(num1, num2);

	// num1 and num2 haven't been randomly generated yet

	correctans = num1 + num2;

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

	Ans(correctans);
}


void subtraction(double & correctans, double num1, double num2)
{
	double studentans;
	//difficulty(num1, num2);

	//if ('h' : num2 > num1) //num2 = 1 + rand() % 999;
	correctans = num1 - num2;

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

	Ans(correctans);
}

void multiplication(double & correctans, double num1, double num2)
{
	double  studentans;

	//difficulty(num1, num2);

	correctans = num1 * num2;

	cout << "\n\n";
	cout << " " << setw(4) << num1 << endl;
	cout << " *" << setw(4) << num2 << endl;
	cout << " " << "----" << endl;
	cout << " ";
	
	Ans(correctans);

}

void division(double & correctans, double num1, double num2, char choice)
{
	double studentans;

	num1 = num2 * num1;
	//difficulty(num1, num2);
	/*if (choice == 'b')
	{
		num2 = 1 + rand() % 9;
		num1 = num2 * (rand() % 50 + 1);
	}
	if (choice == 'm')
	{

		num2 = 1 + rand() % 20;
		num1 = num2 * (rand() % 100 + 1); 
	}
	else if (choice == 'h')
	{

		num2 = 1 + rand() % 15;
		num1 = num2 * (rand() % 110 + 1);
	}
	*/

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

	cout << "\n\n";
	cout << " " << num1 << " / " << num2 << " = ";

	Ans(correctans);

}
but the others elude me.
What do you mean? What is the problem? Please elaborate.
this is the part i cant do : i need it to keep track of the number of correct scores as well as number tries and i also want to have 5 answer options,and also give the user up to 3 tries to get a question right
Well, I'm not doing your homework. You need to be more specific what your problems are.
i'm not asking you to do my homework, i've done it as you can see. i'm having problems figuring out how to get it to loop the question up to 3 times if the user gets it wrong and also how to get it to display multiple choice options. I may be a beginner, but i'm not lazy or anything. i"m here because i'm stuck, you can run my code it works fine.

Here's an example of looping a question up to 3 times. Maybe it'll help:

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
#include <iostream>
#include <string>

int main()
{
    using namespace std;

    std::string correct_answer = "dog";
    std::string your_answer;
    
    int num_loops = 3;
    bool correct = false;
    for (int i = 0; i < num_loops; i++)
    {
        cout << "Question: What is a grown puppy called?" << endl;
        cout << "Answer: ";
        cin >> your_answer;
    
        if (your_answer == correct_answer)
        {
            correct = true;
            break;
        }
        else
        {
            cout << "Sorry, incorrect." << endl;
        }
    }
    if (correct)
    {
        cout << "Correct!" << endl;
    }
    else
    {
        cout << "You ran out of tries. =(" << endl;
    }
}
Last edited on
thanks, if you look at my code, you'll see that i have a "void ans" function, that takes care of displaying the answers, is that where i'll put this loop? or will i have to make another function?
Last edited on
yes you could certainly put the loop logic inside of answer.

You could do something like
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
void Ans(double correctans, char op int num1, int num2)
{
    // ...

   for (int i = 0; i < num_loops; i++)
    {
        display_problem(op, num1, num2);
        cout << "Answer: ";
        cin >> your_answer;
    
        if (your_answer == correct_answer)
        {
            correct = true;
            break;
        }
        else
        {
            cout << "Sorry, incorrect." << endl;
        }
    }
}


Also, you have some repeated code inside each +,-,*,/ function

1
2
3
4
5
6
	// Display the problem.
	cout << "\n\n";
	cout << " " << setw(4) << num1 << endl;
	cout << " +" << setw(4) << num2 << endl;
	cout << " " << "----" << endl;
	cout << " ";

1
2
3
4
5
6
	// Display the problem.
	cout << "\n\n";
	cout << " " << setw(8) << num1 << endl;
	cout << " -" << setw(8) << num2 << endl;
	cout << " " << "----" << endl;
	cout << " ";

1
2
3
4
5
	cout << "\n\n";
	cout << " " << setw(4) << num1 << endl;
	cout << " *" << setw(4) << num2 << endl;
	cout << " " << "----" << endl;
	cout << " ";


I would factor those into a function:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
void display_problem(char op, int num1, int num2)
{
	cout << "\n\n";
    if (operator != '/')
    {
	cout << " " << setw(4) << num1 << endl;
	cout << " " << op << setw(4) << num2 << endl;
	cout << " " << "----" << endl;
	cout << " ";
    }
    else
    {
	cout << " " << num1 << " / " << num2 << " = ";
    }

}


called like:
1
2
    display_problem('+'); // for addition
    display_problem('/'); // for division 
Last edited on
Topic archived. No new replies allowed.