【问题标题】:Why does program continue in while loop for any character entered?为什么程序在while循环中继续输入任何字符?
【发布时间】:2015-01-15 07:35:44
【问题描述】:

我退出 while 循环的条件有效,而留在循环中并输入另一种数字的条件有效。用户可以输入 Y 以留在循环中,但用户也可以输入任何单个字符并留在循环中。我不明白为什么当 Y 是比较时它会将任何字符评估为真?

 int main()
 {

     string romNum;
     char nextRoman; 
     int decimal = 0; 

     bool done = false; 
     bool invalCharacter = false; 


     int I_counter = 0; 
     int X_counter = 0;
     int C_counter = 0;
     int M_counter = 0;

     int V_counter = 0; 
     int L_counter = 0;
     int D_counter = 0;

     while(done == false) 
     {

             cout << "Enter a Roman Number and I will tell you its integer equivalent : " << endl;
             cin >> romNum;

             decimal = 0;    

             I_counter = 0;
             X_counter = 0;
             C_counter = 0;
             M_counter = 0;

             V_counter = 0;
             L_counter = 0;
             D_counter = 0;

             invalCharacter = true; 

             for(int i = 0; i < romNum.length(); i++) 
             {

                 switch(romNum.at(i)) 
                 {
                     case 'M': decimal += 1000;   M_counter += 1; break;
                     case 'D': decimal += 500;    D_counter += 1; break;
                     case 'C': decimal += 100;    C_counter += 1; break;
                     case 'L': decimal += 50;     L_counter += 1; break;
                     case 'X': decimal += 10;     X_counter += 1; break;
                     case 'V': decimal += 5;      V_counter += 1; break;
                     case 'I': decimal += 1;      I_counter += 1; break;

                     default : invalCharacter = false; break;
                 }

             }

         if(I_counter > 4 || X_counter > 4 || C_counter > 4 || M_counter > 4 || V_counter > 1 || L_counter > 1 || D_counter > 1 || invalCharacter == false) 
         {
             cout << "Not a valid roman number. " << endl << endl;
         }

         else
         {
             cout << "The decimal value of the roman number is " << decimal << endl << endl; 
         }

         cout << "Would you like to enter another number? If yes enter Y, If not enter N. " << endl << endl;
         cin >> nextRoman; 

         if(nextRoman == 'Y')
         {
             done = false;
         }
         else if(nextRoman == 'N') 
         {
             done = true;
             cout << "Thanks for roman numeraling with me. " << endl;
         }

     }

     return 0;
 }

【问题讨论】:

  • 您能否在 == 'y' 子句中添加一些调试,并在末尾添加一个“else”并将调试也放入其中(即 printf("unknown answer...\n "); 并告诉我们结果? - 如果字母不是 Y 或 N,则 done 保持错误,因为您没有更改它。
  • 请先用一个体面的调试器进行检查,然后再问这里!
  • 酷就行。谢谢,求建议。我仍然忘记了通过打印给自己进行调试。
  • 关于风格的两个cmets:首先,应该是while ( !done ),而不是done == false。其次,您应该在循环中定义计数器变量,而不是在顶部。

标签: c++ loops boolean


【解决方案1】:
    cout << "Would you like to enter another number? If yes enter Y, If not enter N. " << endl;
    cin >> nextRoman;

    if (nextRoman == 'Y')
    {
        done = false;

    }
    else if (nextRoman == 'N')
    {

        cout << "Thanks for roman numeraling with me. " << endl;
        done = true;      //I rather have the cout execute first then done = true; 

    }

}

在某些编译器中,您的代码可以正常工作,这可能是一个错误。

【讨论】:

    【解决方案2】:

    试试这个(强制一个明确的“Y”或“N”响应):

    nextRoman = '';
    while (nextRoman != 'Y' && nextRoman != 'N') {
         cout << "Would you like to enter another number? If yes enter Y, If not enter N. " << endl << endl;
         cin >> nextRoman; 
    
         if(nextRoman == 'Y')
         {
             done = false;
         }
         else if(nextRoman == 'N') 
         {
             done = true;
             cout << "Thanks for roman numeraling with me. " << endl;
         }
         else
         {
             cout << "What?" << endl;
         }
    }
    

    【讨论】:

      【解决方案3】:

      在 if -else 部分添加一个条件:

      if(nextRoman == 'Y')
               {
                   done = false;
               }
               else if(nextRoman == 'N') 
               {
                   done = true;
                   cout << "Thanks for roman numeraling with me. " << endl;
               }
      
               else
               {
                   done=false;
                    cout<<"see you next time"//or anything you want
                }
      

      现在会好的。

      【讨论】:

        猜你喜欢
        • 2021-04-09
        • 2020-05-04
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-12-24
        • 1970-01-01
        • 2014-03-11
        相关资源
        最近更新 更多