为什么程序会在循环中继续输入任何字符?

时间:2014-11-18 07:27:17

标签: c++ loops boolean

我退出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;
 }

3 个答案:

答案 0 :(得分:0)

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

现在没事了。

答案 1 :(得分:0)

尝试此操作(强制显示&#39; Y&#39;或&#39; N&#39;响应):

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

答案 2 :(得分:0)

    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; 

    }

}

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