Repeating a menu option until user quits

I'm having a bunch of issues with this program.
Right now I am working on the Addition portion so hopefully it will transfer over to the other menu options. So assume the user chooses 1 for now.

I want the program to continually ask the user random number addition questions, until they input -1, which will take them to the main menu. So, upon entering the correct answer, another addition question will be asked. If they enter the wrong answer the same question will be asked (until they enter -1 to quit). If they do quit it will display how many questions they answered.

I'm just not getting how to repeat the addition statement once the user answers it, and then all the choices are messed up. >.<

So the output might be:
"1 +1 is?
2.
correct!

2+3 is?
8.
wrong!
2+3 is?
-1
You tried 2 problems
****welcome**** (Menu)


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

using namespace std;

int _tmain(int argc, _TCHAR* argv[])
{
	int iii;



	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;
			cout << "How much is " << aaa << " plus " << bbb << "?\n";
			cout << "Enter your answer (-1 to return to menu): ";
			cin >> ans;
			total = aaa + bbb;
			for (total == ans; total < ans; total++)
				cout << "Very Good!\n";
				total++;
			if (ans == -1)
				cout << "Total times";
				
			if (ans != total)
				cout << "No. Please try again.\n";
				cout << "How much is " << aaa << " plus " << bbb << "?\n";
				cout << "Enter your answer (-1 to return to menu): ";
				cin >> ans;
				total = aaa + bbb;
		}

		else if (iii == 2)
		{
			aaa = rand() % 99 + 10;
			bbb = rand() % 99 + 10;
			cout << "How much is " << aaa << " minus " << bbb << "?\n";
			cout << "Enter your answer (-1 to return to menu): ";
			total = aaa - bbb;
		}
		else if (iii == 3)
		{
			aaa = rand() % 99 + 10;
			bbb = rand() % 99 + 10;
			cout << "How much is " << aaa << " times " << bbb << "?\n";
			cout << "Enter your answer (-1 to return to menu): ";
		}

		else if (iii == 4)
		{
			aaa = rand() % 99 + 10;
			bbb = rand() % 99 + 10;
			cout << "How much is " << aaa << " divided " << bbb << "?\n";
			cout << "Enter your answer (-1 to return to menu): ";
		}

	} while (iii != 5);
	return 0;


}
So I put it in another do while loop.
It kind of works kind of does not. For example it will now loop the problem but when the answer is correct it loops the same problem, where as when it is wrong two different random numbers are added. I would like the opposite.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
			do
			{
				aaa = rand() % 99 + 10;
				bbb = rand() % 99 + 10;
				cout << "How much is " << aaa << " plus " << bbb << "?\n";
				cout << "Enter your answer (-1 to return to menu): ";
				cin >> ans;
				total = aaa + bbb;
				if (total == ans)
					cout << "Very Good!\n";

				else if (total != ans)
					cout << "No. Please try again.\n";
					cout << "How much is " << aaa << " plus " << bbb << "?\n";
					cout << "Enter your answer (-1 to return to menu): ";
					cin >> ans;
					total = aaa + bbb;

			} while (ans != -1);
				cout << "Calculations";
Can anyone help me understand why it is doing this?
I modified a little bit.
It works well for me:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
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;
				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";



I moved the random number generation into the "if" statement. So it generates random number, if the previous answer was good. Otherwise we wont need it. (So I have to put the random generating before the "for loop" too, because we also need numbers for the first time.

I modified the "do while" loop into an infinite loop and I used the "break" to exit from the loop if the input is "-1".

Instead of
1
2
3
4
5
if(){

}else if(){

}


I changed it to
1
2
3
4
5
6
7
8
if(){

}else if(){

}else{

}


So whit that i is possible to check all of the tree type of input:
-right answer
-exit "-1"
-wrong answer
-Edit-
OMG I figured it out! Thank you so much Won! I have been working on this and other assignments for weeks!
Sometimes you just want someone else to look at it and see if they see anything, thank you Won.
Last edited on
I'm happy, that I could help you. :)
I think you were having trouble because you're trying to do this in 1 loop where conceptually there are really two:
- an outer loop that displays the menu and get's their choice
- an inner loop that repeatedly asks one question until they get it right or enter -1.

Here is what I mean:
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
#include <iostream>
#include <cstdlib>
#include <ctime>

using namespace std;


// Ask the user for the answer to x+y, x-y, x*y or x/y, depending on
// whether operation is 1, 2, 3, or 4.  Return when they get it
// right, or quit (by entering -1 for their answer
void askQuestion(int x, int y, char operation)
{
    int answer;                 // answer to problem
    int guess;                  // user's guess
    const char *prompts[4] = {"plus",
                              "minus",
                              "times",
                              "divided by"};


    // Determine the answer
    switch (operation) {
    case 1: answer = x+y; break;
    case 2: answer = x-y; break;
    case 3: answer = x*y; break;
    case 4: answer = x/y; break;
    }


    // Now ask them until they get it right or enter -1
    while (true) {
        cout << "How much is " << x << ' ' << prompts[operation-1]
             << ' ' << y << "?\n";
        cout << "Enter your answer (-1 to return to menu): ";
        cin >> guess;
        if (guess == -1) {
            return;     // user quit
        } else if (guess == answer) {
            cout << "Very Good!\n";
            return;
        } else {
            cout << "No. Please try again.\n";
        }
    }
}



int main(int argc, char *argv[])
{
    int operation;
    int aaa;
    int bbb;

    srand((unsigned)time(0));

    while (true) {
        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 >> operation;

        if (operation == 5) {
            break;              // exit
        }
        aaa = rand() % 99 + 10;
        bbb = rand() % 99 + 10;
        askQuestion(aaa, bbb, operation);
    }

    return 0;
}

@dhayden,
Neat readable clean code solution. I like it.
Topic archived. No new replies allowed.