我正在使用 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;
}
uj5u.com熱心網友回復:
您看到的問題是 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 將其拉入,因此我不需要添加該包含)。
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/480957.html
上一篇:如何使用我的條件創建回圈
