-2

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");
        }
    }
D. Power
  • 1
  • 1

4 Answers4

2

Edit: I looked into your code and I've found several bugs:

  • When you input data you should convert the data from it's original format to int... Else a string containing no numbers will hang your program.
  • In the default case you're not comparing correctly on the if...
  • I don't understand what your system("Pause") function does... Perhaps sleep() will do to delay showing the menu again?
  • Also, as I've stated on my original response you should not use gotos in C++, as it is considered a poor programming practice (there are lots of posts on the subject, such as: GOTO still considered harmful?)

Here is a corrected version of the code:

enum STR2INT_ERROR { SUCCESS, OVERFLOW, UNDERFLOW, INCONVERTIBLE };

STR2INT_ERROR str2int (int &i, std::string &s, int base = 0)
{
    char *end;
    const char *c= s.c_str();

    long  l;
    errno = 0;
    l = strtol(c, &end, base);
    if ((errno == ERANGE && l == LONG_MAX) || l > INT_MAX) {
    return OVERFLOW;
    }
    if ((errno == ERANGE && l == LONG_MIN) || l < INT_MIN) {
    return UNDERFLOW;
    }
    if (*c == '\0' || *end != '\0') {
    return INCONVERTIBLE;
    }
    i = l;
    return SUCCESS;
}

int main(){
std::string menuOpt;
int iMenu=-1;
bool done=false;


while(!done){
    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 >> menuOpt;

    if( str2int(iMenu,menuOpt) != SUCCESS){
    cout << "Please enter a valid number" << endl;
    continue;  
    }

    switch (iMenu)
    {
    case 1: 
        iMenu = 1;
        cout << "In this section you can add two numbers together. Please insert one number" << endl << endl;
        cin >> menuOpt;
        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;
        break;

    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;
        break;

    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;
        break;

    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;
        break;

    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 == 1)
          {
          done=true;
          }
          break;
    } 
        default:
        {
        if (iMenu != 1 && iMenu !=2 && iMenu !=3 && iMenu !=4 && iMenu !=5)
        cout << "Please enter a valid number" << endl << endl;
        }
        break;
        //system("Pause");
    }
  }
}

Note: I've used a modified version of the str2int() method obtained from here: Convert string to int C++

Also, please notice that I've only converted the first input to int, you need to do the same every time you ask the user for a numerical input...

Community
  • 1
  • 1
Alex Mantaut
  • 3,657
  • 3
  • 35
  • 45
  • Thanks for your input, unfortunately this didn't work for me in this situation but I will be sure to use this technique in the future. – D. Power Oct 24 '15 at 19:12
  • Mmm, I've looked into your code and it was unclear to me what was the bug in your code... So I've addressed several bugs I saw running it... Please check the updated answer, if your problem persists please explain the expected behaviour and how your code is failing to match that behaviour. – Alex Mantaut Oct 25 '15 at 14:06
  • The errors I have been having are that when an input that is not a number is entered into the menu, it keeps repeating the cout in the default and doesn't allow the user to enter a new number. Thank you for your input but the code here is a bit too complicated for me so if there is an easier way for this to be solved I would appreciate it. – D. Power Oct 27 '15 at 11:13
  • Mmmm, too complicated how? Regarding the str2int() function, I needed a function to validate string to int conversion (to avoid problems related to inputing non numeric strings) For alternatives to this implementation of str2int() please take a look to this post: http://stackoverflow.com/questions/7663709/convert-string-to-int-c – Alex Mantaut Oct 27 '15 at 13:11
0

If you want to check more than one condition separately, you need to use an 'else-if' after the 'if'.

int iExit;
  cin >> iExit;
  if (iExit == 1)
  {
     return 0;
  }
  else if (iExit == 2)
  {
     goto CalculatorMenu;
  }

 else
 {
  cout << "Incorrect choice was entered, please input 1 or 2." << endl << endl;
   cin >> iExit;
 }
0

You didn't post the whole switch block so it is possible that you might forgot to terminate the case block with a break.

Bulent Vural
  • 2,630
  • 1
  • 13
  • 18
0

Maybe EOF input will solve ur problem:

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;
while(cin>>iExit){
   if(iExit == 1){
       return 0;
   }else if(iExit == 2){
       //goto ...
   }
   else{
        cout << "Incorrect choice was entered, please input 1 or 2." << endl << endl;
        continue;
   }
}
M.Sun
  • 1
  • 4