math tutor program

Can't figure out the error. The program runs well when I select an option from the menu and answer the problem and it displays whether is correct or not, but then it shows me the menu again with the same problem(like addition for example) underneath it.

#include <iostream>
#include <cstdlib>
#include <cmath>
#include <ctime>
using namespace std;
int main ()
{
int choice,num1, num2, useranswer, result;
unsigned seed = time(0);
srand(seed);


num1= 1 +rand()%1000;
num2= 1 +rand()%1000;

do
{ // display the menu for the user to select a choice
cout<<"\n Math Tutor Menu:\n";
cout<<"1.Addition\n";
cout<<"2.Subtraction\n";
cout<<"3.Multiplication\n";
cout<<"4.Division\n";
cout<<"5.Quit\n";

//validate the menu choice
while ((choice<1) || (choice>5))
{ cout<<" Please select either 1,2,3,4, or 5.";
cin>> choice;
}

if (choice!=4)
{
switch (choice)
{
case 1:{ cout<<"You have chosen addition." <<endl;
cout<< num1 << "+" << num2 << "=?" <<endl;
cin>> useranswer;
result= num1 + num2;
if (useranswer== result)
cout<< "Congratulations! You got the answer right.";
else
cout<<"Your answer is incorrect. The correct answer is:" << result<< endl;
break; }

case 2:{ cout<< "You have chosen subtraction." <<endl;
cout<< num1 << "-" << num2 << "=?" <<endl;
cin>> useranswer;
result= num1 - num2;
if (useranswer== result)
cout<<"Congratualtions! You got the answer right.";
else
cout<<"Your answer is incorrect. The correct answer is:" << result<< endl;
break; }

case 3: {cout<<"You have chosen multiplication." <<endl;
cout<< num1 << "*" << num2 << "=?" <<endl;
cin>> useranswer;
result= num1 * num2;
if (useranswer==result)
cout<<"Congratualtions! You got the answer right.";
else
cout<<"Your answer is incorrect. The correct answer is:" <<result <<endl;
break; }

case 4:{ cout<<"You have chosen division." <<endl;
cout<< num1 << "/" << num2 << "=?" <<endl;
cin>> useranswer;
result= num1 / num2;
if(useranswer==result)
cout<<"Congratulations! You got the answer right.";
else
cout<<"Your answer is incorrect. The correct answer is:" <<result <<endl;
break; }
}
}
} while(choice!=5); //loops again if the user did not chose choice 5 to quit
return 0;
}
Next time please use the code tags and properly format your code. It make it a lot easier for use to decipher whats going on.

Here is the working code below:
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
#include <iostream>
#include <cstdlib>
#include <cmath>
#include <ctime>
using namespace std;
int main ()
{
    int choice = 0,num1, num2, useranswer, result;
    unsigned seed = time(0);
    srand(seed);

    do
    {
      // move inside loop to have different random numbers for each iteration
      num1= 1 +rand()%1000;
        num2= 1 +rand()%1000;

 // display the menu for the user to select a choice
        cout<<"\n Math Tutor Menu:\n";
        cout<<"1.Addition\n";
        cout<<"2.Subtraction\n";
        cout<<"3.Multiplication\n";
        cout<<"4.Division\n";
        cout<<"5.Quit\n";

        // changed to do-while loop so a difference choice can be 
        // entered for each iteration
        do
        {
            cout<<" Please select either 1,2,3,4, or 5.";
            cin>> choice;
        }while((choice<1) || (choice>5));
       
      // removed if(choice!=4), which didn't make sense, and caused infinite loop
        switch (choice)
        {
            case 1:
            {
                cout<<"You have chosen addition." <<endl;
                cout<< num1 << "+" << num2 << "=?" <<endl;
                cin>> useranswer;
                result= num1 + num2;
                if (useranswer== result)
                    cout<< "Congratulations! You got the answer right.";
                else
                    cout<<"Your answer is incorrect. The correct answer is:" << result<< endl;
                break;
            }

            case 2:
            {
                cout<< "You have chosen subtraction." <<endl;
                cout<< num1 << "-" << num2 << "=?" <<endl;
                cin>> useranswer;
                result= num1 - num2;
                if (useranswer== result)
                    cout<<"Congratualtions! You got the answer right.";
                else
                    cout<<"Your answer is incorrect. The correct answer is:" << result<< endl;
                break;
            }

            case 3:
            {
                cout<<"You have chosen multiplication." <<endl;
                cout<< num1 << "*" << num2 << "=?" <<endl;
                cin>> useranswer;
                result= num1 * num2;
                if (useranswer==result)
                    cout<<"Congratualtions! You got the answer right.";
                else
                    cout<<"Your answer is incorrect. The correct answer is:" <<result <<endl;
                break;
            }

            case 4:
            {
                cout<<"You have chosen division." <<endl;
                cout<< num1 << "/" << num2 << "=?" <<endl;
                cin>> useranswer;
                result= num1 / num2;
                if(useranswer==result)
                    cout<<"Congratulations! You got the answer right.";
                else
                    cout<<"Your answer is incorrect. The correct answer is:" <<result <<endl;
                break;
            }
        }

    } while(choice!=5); //loops again if the user did not chose choice 5 to quit
    return 0;
}


In your original code, after the break statement, the same choice would be used for the next iteration, hence the same switch case would be executed. The random numbers would also be the same as they were generated outside the loop.

This code shows menu again but allows the user to pick a different choice, then outputs a different problem (so if addition was picked again, a different addition problem would be given).
Last edited on
Topic archived. No new replies allowed.