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;
}
#include <iostream>
#include <cstdlib>
#include <cmath>
#include <ctime>
usingnamespace 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).