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

Why does program continue in while loop for any character entered?

本文关键字:继续 输入 任何 字符 循环 程序 while 为什么      更新时间:2023-10-16

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

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

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

现在会没事的。

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

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