C++ Function Help

I need this code to work with DoOneSet having only 3 arguments, basically this code needs to be simplified, would appreciate any and all help. Theres a way I can use one function to figure out the amount of correct answers, thats the part I'm stuck on. Thanks.
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
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
#include <iostream>
#include <iomanip>
using namespace std;

void doOneset(char symbol, int probsPerSet, int &numCorrect1, int &numCorrect2,
              int &numCorrect3);
void getProbsPerSet (int &probsPerSet);
void checkAnswer(int &answer1, int &sum);
void printReport(int probsPerSet, int numCorrect1, int numCorrect2, int numCorrect3);


int main ()
{
    int probsPerSet;
    int numCorrect1 = 0;
    int numCorrect2 = 0;
    int numCorrect3 = 0;
   
    
    
    srand(static_cast<unsigned>(time(0)));
    getProbsPerSet(probsPerSet);
    doOneset('+', probsPerSet, numCorrect1, numCorrect2, numCorrect3);
    doOneset('-', probsPerSet, numCorrect1, numCorrect2, numCorrect3);
    doOneset('*', probsPerSet, numCorrect1, numCorrect2, numCorrect3);
    printReport(probsPerSet, numCorrect1, numCorrect2, numCorrect3);
    
    
    
}


void generateOperands(int &num1, int &num2, int &maxNum)
{
    
    
    num1 = rand() % (maxNum + 1);
    num2 = rand() % (maxNum + 1);
    
    
}





void checkAnswer(int &answer1, int &sum, int &numCorrect1)
{
    
    
    
    
    
    if (answer1 == sum){
        cout << "correct" << endl;
        numCorrect1++;
        
        
    } else {
        cout << "incorrect" << endl;
    }
    
    
}






void checkAnswer2(int &answer1, int &sum, int &numCorrect2)
{
    
    
    
    
    
    if (answer1 == sum){
        cout << "correct" << endl;
        numCorrect2++;
        
        
    } else {
        cout << "incorrect" << endl;
    }
    
    
}






void checkAnswer3(int &answer1, int &sum, int &numCorrect3)
{
    
    
    
    
    
    if (answer1 == sum){
        cout << "correct" << endl;
        numCorrect3++;
        
        
    } else {
        cout << "incorrect" << endl;
    }
    
    
}





void calculateMultiplication (int &sum, int &num1, int &num2)
{
    sum = num1 * num2;
}





void calculateSubtraction(int &sum, int &num1, int &num2)
{
    sum = num1 - num2;
}





void calculateCorrectAnswer(int &sum, int &num1, int &num2)
{
    
    
    sum = num1 + num2;
    
    
}





void getMaxNum(int &maxNum)
{
   cout << "What is the maximum number for this set? ";
    cin >> maxNum;
}




void doOneProblem(int maxNum, int &numCorrect1)
{
    int num1;
    int num2;
    int answer1;
    int sum;
    
    
    
    
    
    
    
    generateOperands(num1, num2, maxNum);
    calculateCorrectAnswer(sum, num1, num2);
    
    cout << num1 << "+" << num2 << "= ";
    cin >> answer1;
    checkAnswer(answer1, sum, numCorrect1);
   
}





void doOneSubtraction(int maxNum, int &numCorrect2)
{
    int num1;
    int num2;
    int sum;
    int answer1;
    
    
    
    
    
    generateOperands(num1, num2, maxNum);
    calculateSubtraction(sum, num1, num2);
    
    cout << num1 << " - " << num2 << " = ";
    cin >> answer1;
    
    checkAnswer2(answer1, sum, numCorrect2);
    
    
}





void doOneMultiplication(int maxNum, int &numCorrect3)
{
    int num1;
    int num2;
    int sum;
    int answer1;
    
    
    
    
    generateOperands(num1, num2, maxNum);
    calculateMultiplication(sum, num1, num2);
    
    cout << num1 << " * " << num2 << " = ";
    cin >> answer1;
    
    checkAnswer3(answer1, sum, numCorrect3);
    
}





void getProbsPerSet(int &probsPerSet)
{
    
    
    
    cout << "Enter problems per set: ";
    cin >> probsPerSet;
    
    }


void printHeader(char symbol)
{
    if (symbol == '+')
        cout << "Set #1" << endl;
    
    if (symbol == '-')
        cout << "Set #2" <<endl;
   
    if (symbol == '*')
        cout << "Set #3" <<endl;
    
    
}







void doOneset(char symbol, int probsPerSet, int &numCorrect1, int &numCorrect2,
              int &numCorrect3)
{
    
    int count;
    int maxNum;
    
  
    
    
    if (symbol == '+'){
        printHeader('+');
        getMaxNum(maxNum);
        for (count = 0; count < probsPerSet; count++)
        doOneProblem(maxNum, numCorrect1);
        
        
        
        
        
    }
    if (symbol == '-'){
        printHeader('-');
        getMaxNum(maxNum);
        for (count = 0; count < probsPerSet; count++)
            doOneSubtraction(maxNum, numCorrect2);
        
        
        
        
    }
    if (symbol == '*'){
        printHeader('*');
        getMaxNum(maxNum);
        for (count = 0; count < probsPerSet; count++)
            doOneMultiplication(maxNum, numCorrect3);
       
        
        
    }
    
}

void printReport(int probsPerSet, int numCorrect1, int numCorrect2, int numCorrect3)
{
    int percCorrect1, percCorrect2, percCorrect3, TotalPercCorr, TotalCorrect, TotalProbs;
    
    percCorrect1 = numCorrect1/static_cast<double>(probsPerSet) * 100;
    
    percCorrect2 = numCorrect2/static_cast<double>(probsPerSet) * 100;
    
    percCorrect3 = numCorrect3/static_cast<double>(probsPerSet) * 100;
    
    TotalCorrect = numCorrect1 + numCorrect2 + numCorrect3;
    
    TotalProbs = 3*probsPerSet;
    
    TotalPercCorr = TotalCorrect/static_cast<double>(TotalProbs) * 100;
    
    cout <<"Set#1: You got " <<numCorrect1<<" correct out of " <<probsPerSet<< " for "
    << fixed << setprecision(1) << percCorrect1 << "%" << endl;
    
    cout <<"Set#2: You got " <<numCorrect2<< " correct out of " <<probsPerSet << " for "
    << fixed << setprecision(1) << percCorrect2 << "%" << endl;
    
    cout << "Set#3: You got " << numCorrect3 << " correct out of "<< probsPerSet<<" for "
    << fixed << setprecision(1) << percCorrect3 << "%" << endl;
    
    cout << "Overall you got " << TotalCorrect << " correct out of "<<TotalProbs <<" for "
    << fixed << setprecision(1) << TotalPercCorr << "%" << endl;
    
    
   
}
Last edited on
Why do you have checkanswer, checkanswer2 and checkanswer3? All three functions are identical. The third argument name is different, but that is irrelevant. Argument names are local to the function and have no bearing on what they are called when called.
Because I am checking 3 different correct answers, I know theres a way to make one function call all three, thats why I'm posting on here, I don't know how to make it work with just one function, if it had the same variable name it wouldn't work when I'm adding up all the scores in the printReport function.
That's not how argument names work. As I said,
Argument names are local to the function and have no bearing on what they are called in the calling function.
47
48
49
50
51
52
53
54
55
void checkAnswer (int & answer, int &sum, int &numCorrect)
//  Note than answer and numCorrect names do NOT need to be the same as in calling function
{   if (answer == sum)
    {   cout << "correct" << endl;
        numCorrect++;        
    } else 
    {   cout << "incorrect" << endl;
    }        
}


Line 8: Your function prototype for checkAnswer is incorrect. The implementation takes 3 arguments.

doOneSet can be refactored as follows:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
void doOneset(char symbol, int probsPerSet, int &numCorrect1, int &numCorrect2,
              int &numCorrect3)
{   int maxNum;
     
    printHeader (symbol);  
    getMaxNum(maxNum); 
    for (int count = 0; count < probsPerSet; count++)
    {   if (symbol == '+')
            doOneProblem(maxNum, numCorrect1);              
        if (symbol == '-')
            doOneSubtraction(maxNum, numCorrect2);                 
        if (symbol == '*')
            doOneMultiplication(maxNum, numCorrect3);                       
    }
}
Last edited on
Topic archived. No new replies allowed.