I am using the switch case below and this section is not allowing the user to be able to re-input the value they want to enter and repeatedly shows the same code in the program.
case 5: iMenu = 5;
{
cout << "Are you sure you would like to exit? Type 1 or 2." << endl << endl;
cout << "1. Yes" << endl;
cout << "2. No" << endl;
int iExit;
cin >> iExit;
if (iExit == 1)
{
return 0;
}
if (iExit == 2)
{
goto CalculatorMenu;
}
else
{
cout << "Incorrect choice was entered, please input 1 or 2." << endl << endl;
cin >> iExit;
}
}
It repeats the cout at the beginning of the case endlessly. Any help with this will be greatly appreciated and I can show more code of the entire program if required. Thanks a lot.
EDIT:: Thanks everyone for their input so far, unfortunately it hasn't managed to solve my problem so I'm thinking I messed up earlier in the coding. Here is my entire switch case.
CalculatorMenu: //name of menu
cout << "Which function of this calculator would you like to do?" << endl;
cout << "1. Add Two Numbers" << endl;
cout << "2. Subtract one number from another" << endl;
cout << "3. Multiply Two Numbers" << endl;
cout << "4. Divide one number by another" << endl;
cout << "5. Exit" << endl;
cin >> iMenu;
switch (iMenu)
{
case 1: iMenu = 1;
cout << "In this section you can add two numbers together. Please insert one number" << endl << endl;
cin >> iMenu;
int iAdd1;
iAdd1 = iMenu;
cout << "Please insert another number" << endl << endl;
cin >> iMenu;
int iAdd2;
iAdd2 = iMenu;
int iAddResult;
iAddResult = iAdd1 + iAdd2;
cout << "The addition of " << iAdd1 << " + " << iAdd2 << " = " << iAddResult << endl << endl;
goto CalculatorMenu;
case 2: iMenu = 2;
cout << "In this section you can subtract one number from another. Please insert one number" << endl << endl;
cin >> iMenu;
int iSub1;
iSub1 = iMenu;
cout << "Please insert another number" << endl << endl;
cin >> iMenu;
int iSub2;
iSub2 = iMenu;
int iSubResult;
iSubResult = iSub1 - iSub2;
cout << "The subtraction of " << iSub1 << " - " << iSub2 << " = " << iSubResult << endl << endl;
goto CalculatorMenu;
case 3: iMenu = 3;
cout << "In this section you can multiply two numbers together. Please enter a number" << endl << endl;
cin >> iMenu;
int iMult1;
iMult1 = iMenu;
cout << "Please enter another number" << endl << endl;
cin >> iMenu;
int iMult2;
iMult2 = iMenu;
int iMultResult;
iMultResult = iMult1 * iMult2;
cout << "The multiplication of " << iMult1 << " * " << iMult2 << " = " << iMultResult << endl << endl;
goto CalculatorMenu;
case 4: iMenu = 4;
cout << "In this section you can divide one number by another number. Please enter a number" << endl << endl;
float fDiv1;
cin >> fDiv1;
if (fDiv1 == 0)
do
{
cout << "Please do not divide by 0, please enter another number" << endl << endl;
cin >> fDiv1;
} while (fDiv1 == 0);
cout << "Please enter another number" << endl << endl;
float fDiv2;
cin >> fDiv2;
if (fDiv2 == 0)
do
{
cout << "Please do not divide by 0, please enter another number" << endl << endl;
cin >> fDiv2;
} while (fDiv2 == 0);
float fDivResult;
fDivResult = fDiv1 / fDiv2;
cout << "The division of " << fDiv1 << " / " << fDiv2 << " = " << fDivResult << endl << endl;
goto CalculatorMenu;
case 5: iMenu = 5;
{
cout << "Are you sure you would like to exit? Type 1 or 2." << endl << endl;
cout << "1. Yes" << endl;
cout << "2. No" << endl;
int iExit;
cin >> iExit;
while (iExit != 1 && iExit != 2)
{
cout << "Incorrect choice was entered, please input 1 or 2." << endl << endl;
cin >> iExit;
}
if (iExit == 2)
{
goto CalculatorMenu;
}
if (iExit == 1)
{
return 0;
}
}
default:
{
if (iMenu != 1 || 2 || 3 || 4 || 5)
cout << "Please enter a valid number" << endl << endl;
goto CalculatorMenu;
}
system("Pause");
}
}