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

扎希德

我使用 do while 循环制作 c++ 程序,但在插入 y 或 Y 条件后,我得到循环继续的错误,而没有让我再次插入输入

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');

请查看此问题以了解更多相关细节。

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

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章