C ++ If / Else语句中的switch case错误

时间:2015-10-24 14:07:29

标签: c++ if-statement

我正在使用下面的开关案例,此部分不允许用户重新输入他们想要输入的值,并在程序中重复显示相同的代码。

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

它在案件开始时无休止地重复cout。如果需要,我将非常感谢任何帮助,我可以显示整个程序的更多代码。非常感谢。

编辑::感谢大家到目前为止他们的输入,遗憾的是它还没有设法解决我的问题所以我以为我在编码中早先搞砸了。这是我的整个开关案例。

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

4 个答案:

答案 0 :(得分:2)

修改:我查看了您的代码,我发现了一些错误:

  • 当您输入数据时,您应该将数据从原始格式转换为int ...否则,不包含数字的字符串将挂起您的程序。
  • 在默认情况下,您无法正确比较if ...
  • 我不明白你的系统(&#34;暂停&#34;)功能...也许sleep()会延迟再次显示菜单?
  • 另外,正如我在原始回复中所述,你不应该在C ++中使用gotos,因为它被认为是一种糟糕的编程习惯(关于这个主题有很多帖子,例如:GOTO still considered harmful?

以下是代码的更正版本:

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

注意:我使用了从这里获得的str2int()方法的修改版本:Convert string to int C++

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

答案 1 :(得分:0)

如果您想分别检查多个条件,则需要使用&else; if-if&#39;在&#39; if&#39;之后。

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

答案 2 :(得分:0)

您没有发布整个交换机块,因此您可能忘记使用break终止案例块。

答案 3 :(得分:0)

也许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;
   }
}
相关问题