Home > Enterprise >  error on do while loop condition in C with condition to continue the loop
error on do while loop condition in C with condition to continue the loop

Time:05-23

i make c program using do while loop but after insert the condition which is y or Y i get the error where the loop is continue without let me to insert the input again

after while ( x =='y'|| x == 'Y'); it continue to repeat and i cant insert he input untill i stop the program

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

CodePudding user response:

The problem you are seeing is that there is are leftover characters in the cin buffer, so the next time through the loop it reads those and doesn't ask the user for input. If you clear the buffer out at the end of the loop then the subsequent runs work.


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

Have a look at this question for some more related details.

You may also need to add #include <limits> to use numeric_limits (although in my case iostream pulls it in so I do not need to add that include).

  • Related