c++ If/Else语句切换错误

C++ If/Else statement in switch case error

本文关键字:错误 语句 Else If c++      更新时间:2023-10-16

我使用下面的开关情况,这一节不允许用户能够重新输入他们想要输入的值,并在程序中重复显示相同的代码。

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

它无休止地重复案件开头的判决。对此的任何帮助将不胜感激,如果需要,我可以显示整个程序的更多代码。非常感谢。

感谢大家的输入到目前为止,不幸的是它还没有设法解决我的问题,所以我认为我搞砸了早期的编码。这是我的整个开关箱。
    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");
        }
    }

Edit:我看了你的代码,我发现了几个错误:

  • 当你输入数据时,你应该将数据从原始格式转换为int…否则,不包含数字的字符串将挂起您的程序。
  • 在默认情况下,你没有正确地比较if…
  • 我不明白你的系统("暂停")功能是什么…也许sleep()可以延迟再次显示菜单?
  • 另外,正如我在我最初的回复中所说的,你不应该在c++中使用GOTO,因为它被认为是一种糟糕的编程实践(有很多关于这个主题的帖子,例如:GOTO仍然被认为是有害的?)

下面是更正后的代码:

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 == '' || *end != '') {
    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");
    }
  }
}

注意:我使用了从这里获得的str2int()方法的修改版本:

另外,请注意,我只将第一个输入转换为int,您需要在每次向用户请求数字输入时都这样做…

如果要分别检查多个条件,则需要在' If '后面加上'else-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;
 }

也许EOF输入可以解决你的问题:

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

您没有发布整个开关块,因此您可能会忘记使用break终止case块。