C++ 中的 do while 循环条件错误,条件是继续循环

error on do while loop condition in C++ with condition to continue the loop

我正在使用 do while 循环制作 C++ 程序,但在插入条件后 while ( x =='y'|| x == 'Y'); 我收到一个错误,循环继续而不允许我再次插入输入。

在停止程序之前我无法插入输入。

#include <iostream>
using namespace std;

int main()
{
    char str[30];
    char symptom,quest;
    char a,b,x,y,Y,n,N;
    
    do{

    cout<<" Enter your name: "<<endl;
    cin.get(str,30);
    
    cout<<"Hi " << str << ", Welcome to COVID test!"<<endl;
    cout<<"Let's start the self testing test!"<<endl<<endl<<endl;
    
    
    cout<<"@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"<<endl;
    cout<<"               COVID SELF TESTING CENTRE                "<<endl;
    cout<<"@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"<<endl<<endl;
    
    cout<<"Do you have any symptoms below: (1-Yes, 2-No)"<<endl;
    
    cout<<"Fever --> "<<endl;
    cin>>a;
    
    cout<<"Cough --> "<<endl;
    cin>>a;
    
    cout<<"Flu --> "<<endl;
    cin>>a;
    
    cout<<"Shortness of breath --> "<<endl;
    cin>>a;
    
    cout<<"Sore throat --> "<<endl;
    cin>>a;
    
    cout<<"Have you ever tested POSITIVE COVID-19 : "<<endl;
    cin>>b;
    
    cout<<"Do you had close contact with those who have been confirmed PORITIVE COVID-19 in the last 14 days?  : "<<endl;
    cin>>b;
    
    cout<<"Do you have a history of traveling abroad or outside the state of Perak in the last 14 days? : "<<endl;
    cin>>b;
    
    cout<<"Are you currently undergoing a home quarantine control order by the Ministry of Health Malaysia? : "<<endl;
    cin>>b;
    
    cout<<endl<<endl;
    
    cout<<"========================================================="<<endl;
    cout<<"          RESULT FOR COVID-19 SELF TESTING CENTRE        "<<endl;
    cout<<"========================================================="<<endl;
    
    symptom=a;
    quest=b;
    
    if (symptom=='2'&&quest=='2')
    {
    cout<<"GREEN ZONE. Your status are low risk and no symptoms. Please folllow the SOP and Stay Safe Thank You!"<<endl;
    }
    if (symptom=='1'&&quest=='1')
    {
    cout<<"RED ZONE. Please get a clinically COVID-19 checkup from nearby hospital. Please folllow the SOP and Stay Safe Thank You!"<<endl;
    }
    if (symptom=='1'&&quest=='2')
    {
    cout<<"YELLOW ZONE. Please stay at home or self quarantine. Please folllow the SOP and Stay Safe Thank You!"<<endl;
    }
    if (symptom=='2'&&quest=='1')
    {
    cout<<"YELLOW ZONE. Please stay at home or self quarantine. Please folllow the SOP and Stay Safe Thank You!"<<endl;
    }
    
    
    cout<<"Ingin teruskan? (Y-yes, N-No): "<<endl;
    cin>>x;
    cout<<" ";
    
    }while ( x =='y'|| x == 'Y');
    system("pause");
    return 0;
}

您看到的问题是 cin 缓冲区中有剩余字符,因此下一次通过循环它会读取这些字符并且不会要求用户输入。如果您在循环结束时清除缓冲区,则后续运行有效。


do {
    //...

    std::cout << "Ingin teruskan? (Y-yes, N-No): "<< std::endl;
    std::cin >> x;
    std::cout << " ";
    
    // add this to ignore and clear the current cin buffer
    std::cin.clear();
    std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');

} while (x == 'y' || x == 'Y');

查看 this question 了解更多相关详细信息。

您可能还需要添加 #include <limits> 才能使用 numeric_limits(尽管在我的情况下 iostream 将其拉入,因此我不需要添加该 include)。