Summary of number of times program runs

So, I got my program to work, finally. But I just noticed I did it wrong (kind of). Every time the user enters -1 to display the summary the number of addition problems played is off if the user got an addition problem wrong and the question had to be repeated.

Example:
1+1 = ?
2
2+2 = ?
5
2+2 = ?
4
1+3 = -1
Summary:
Number of addition problems played: 4

-It reads 4, when it should be 3. This also happens with 'How many times answered incorrectly'

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
#include "stdafx.h"
#include <iostream>
#include <math.h>
#include <ctime>

using namespace std;

int _tmain(int argc, _TCHAR* argv[])
{
	int iii, play;
	int correct = 0;
	play = 0;
	int played = 0;


	srand((unsigned)time(0));
	int aaa;
	int bbb;
	int total;
	int ans;
	int ccc = rand() % 99 + 10;
	int ddd = rand() % 99 + 10;

	do
	{
		cout << "*******Welcome to the Arithmetic quiz********\n";
		cout << "MENU:\n\nEnter 1 for Addition\n";
		cout << "Enter 2 for Subtraction\n";
		cout << "Enter 3 for Multiplication\n";
		cout << "Enter 4 for Division\n";
		cout << "Enter 5 to Exit\n\n";
		cin >> iii;


		if (iii == 1)
		{
			aaa = rand() % 99 + 10;
			bbb = rand() % 99 + 10;
			do
			{

				cout << "How much is " << aaa << " plus " << bbb << "?\n";
				cout << "Enter your answer (-1 to return to menu): ";
				cin >> ans;
				total = aaa + bbb;
				played++;
				if (total == ans)
				{
					cout << "Very Good!\n";
					aaa = rand() % 99 + 10;
					bbb = rand() % 99 + 10;
					correct++;
				}

				else if (ans == -1)
					break;

				else 
					cout << "No. Please try again.\n";
					play++;

			} while (true);
			cout << "Summary:\n";
			cout << "Addition Problems Played: " << played << endl;
			cout << "Number of times answered correctly: " << correct << endl;
			cout << "Number of times answered incorrectly: " << play << endl;


		}
		else if (iii == 2)
		{
			aaa = rand() % 99 + 10;
			bbb = rand() % 99 + 10;
			do
			{

				cout << "How much is " << aaa << " minus " << bbb << "?\n";
				cout << "Enter your answer (-1 to return to menu): ";
				cin >> ans;
				total = aaa - bbb;
				if (total == ans)
				{
					cout << "Very Good!\n";
					aaa = rand() % 99 + 10;
					bbb = rand() % 99 + 10;
				}

				else if (ans == -1)
					break;

				else
					cout << "No. Please try again.\n";

			} while (true);
			cout << "Calculations";
		}
		else if (iii == 3)
		{
			aaa = rand() % 99 + 10;
			bbb = rand() % 99 + 10;
			do
			{

				cout << "How much is " << aaa << " times " << bbb << "?\n";
				cout << "Enter your answer (-1 to return to menu): ";
				cin >> ans;
				total = aaa * bbb;
				if (total == ans)
				{
					cout << "Very Good!\n";
					aaa = rand() % 99 + 10;
					bbb = rand() % 99 + 10;
				}

				else if (ans == -1)
					break;

				else
					cout << "No. Please try again.\n";

			} while (true);
			cout << "Calculations";
		}

		else if (iii == 4)
		{
			aaa = rand() % 99 + 10;
			bbb = rand() % 99 + 10;
			do
			{

				cout << "How much is " << aaa << " divided by " << bbb << "?\n";
				cout << "Enter your answer (-1 to return to menu): ";
				cin >> ans;
				total = aaa / bbb;
				if (total == ans)
				{
					cout << "Very Good!\n";
					aaa = rand() % 99 + 10;
					bbb = rand() % 99 + 10;
				}

				else if (ans == -1)
					break;

				else
					cout << "No. Please try again.\n";

			} while (true);
			cout << "Calculations";
		}



	} while (iii != 5);
	return 0;
}
Essentially you want someone to debug your code. A major part of writing code is learning how to debug it yourself or even better writing code in a way that minimizes the chances of having too many bugs in the first place. If you know how your code works you can often work out where the error is from the results. For example the error total is being incremented somewhere along with the correct total.

By the way I would use meaningful names for your variables. This is a form of self commenting the code. Thus aaa and bbb should be something like num1 and num2. You ccc and ddd variables can be removed as you don't actually use them.

I will look over your code and reply later unless someone jumps in earlier with a solution.
I know why it is displaying the # of additions wrong, because played++ is in the do while loop, and the else statement runs the do loop every time the answer is wrong, but I don't know how to change the code to keep the program running and display played++ correctly.

I read the opposite, that you want variables that won't show up often in code like you are less likely to see, 'aaa' than 'num'?
ccc and ddd are from a previous attempt that did not work, I will delete it.

Thank you.
The compiler should flag unused variables.
The incorrect number is play - correct
cout << "Number of times answered incorrectly: " << play-correct << endl;
Also the total games played seems to be adding the -1 action as another play?
Also some code is common to the four options and this often means you can simplify it.
Nothing was flagged, the program ran without warnings or errors.

Yes, basically the only one printing correctly is 'correct' both play and played are displaying as you've noted.


Most likely it is, but I'm not sure how to do that. I'm just starting to get the hang of loops.
By the way I would use meaningful names for your variables.


Yea, the compilers today take long names. There's no excuse to have obscure variable names. It is bad form and should be eliminated from the beginning. The best code is clear code, not convoluted or obscure code. Add the else even when drop through is fine if it makes the code clearer.
Last edited on
@Elzcode,

Here I have used the while loop and only implemented plus and minus to keep it simple.
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
#include <stdlib.h>
#include <iostream>
#include <math.h>
#include <ctime>

using namespace std;

int main()
{
    int num1,num2,ans,correct,incorrect,selection;
    correct = 0;
    incorrect = 0;


    do
    {
        cout << "*******Welcome to the Arithmetic quiz********" << endl;
        cout << "MENU:" << endl;
        cout << "Enter 1 for Addition" << endl;
        cout << "Enter 2 for Subtraction" << endl;
        cout << "Enter 5 to Exit" << endl;
        cin  >> selection;

        num1 = rand() % 99 + 10;
        num2 = rand() % 99 + 10;


        if (selection == 1)
        {
            cout << "How much is " << num1 << " plus " << num2 << "?" << endl;
            cout << "Enter your answer (-1 to return to menu): " << endl;
            cin  >> ans;
            while (ans != -1)
            {
                if (ans == num1 + num2)
                {
                    cout << "CORRECT" << endl;
                    correct = correct + 1;
                    num1 = rand() % 99 + 10;
                    num2 = rand() % 99 + 10;
                }else{
                    cout << "WRONG TRY AGAIN" << endl;
                    incorrect = incorrect + 1;
                }
                cout << "How much is " << num1 << " plus " << num2 << "?" << endl;
                cout << "Enter your answer (-1 to return to menu): " << endl;
                cin  >> ans;
            }
        }

        if (selection == 2)
        {
            cout << "How much is " << num1 << " minus " << num2 << "?" << endl;
            cout << "Enter your answer (-1 to return to menu): " << endl;
            cin  >> ans;
            while (ans != -1)
            {
                if (ans == num1 - num2)
                {
                    cout << "CORRECT";
                    correct = correct + 1;
                    num1 = rand() % 99 + 10;
                    num2 = rand() % 99 + 10;
                }else{
                    cout << "WRONG TRY AGAIN" << endl;
                    incorrect = incorrect + 1;
                }
                cout << "How much is " << num1 << " plus " << num2 << "?" << endl;
                cout << "Enter your answer (-1 to return to menu): " << endl;
                cin  >> ans;
            }
        }

	    cout << "Summary: /n";
	    cout << "Addition Problems Played: " << correct + incorrect << endl;
	    cout << "Number of times answered correctly: " << correct << endl;
	    cout << "Number of times answered incorrectly: " << incorrect << endl;

    } while (selection != 5);


	return 0;
}

Topic archived. No new replies allowed.