Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

std::cin infinite loop for wrong input

Tags:

c++

cin

In my following code I want to loop until the user provided the right input. But when I tried it's turning into a nonstop loop.
Please Enter Valid Input.
without the while loop it's also the same.

Here with while loop:

#include <iostream>
#include <fstream>
#include <string>
#include <ctime>
#include <sstream>
using namespace std;

class library {
public:
    library() {
        int mainOption;

        cout<<"Please choose the option you want to perform."<<endl;
        cout<<"1. Member Section"<<"\n"<<"2. Books, Lending & Donate Section"<<"\n"<<"3. Returning Section"<<endl;
        bool option=true;
        while (option==true) {
            cin>>mainOption;
            if (mainOption==1) {
                cout<<"section 1"<<endl;
                option=false;
            } else if (mainOption==2) {
                cout<<"section 1"<<endl;
                option=false;
            } else if (mainOption==3) {
                cout<<"section 1"<<endl;
                option=false;
            } else {
                cout<<"Please Enter Valid Input. "<<endl;
                //option still true. so it should ask user input again right?
            }
        }
    }
};

int main(int argc, const char * argv[])
{
    library l1;
    return 0;
}

Here without the while loop. But same thing happening.

#include <iostream>
#include <fstream>
#include <string>
#include <ctime>
#include <sstream>
using namespace std;

class library {
public:
    library() {
        int mainOption;

        cout<<"Please choose the option you want to perform."<<endl;
        cout<<"1. Member Section"<<"\n"<<"2. Books, Lending & Donate Section"<<"\n"<<"3. Returning Section"<<endl;

        cin>>mainOption;
        if (mainOption==1) {
            cout<<"section 1"<<endl;
        } else if (mainOption==2) {
            cout<<"section 1"<<endl;
        } else if (mainOption==3) {
            cout<<"section 1"<<endl;
        } else {
            cout<<"Please Enter Valid Input. "<<endl;
            library();//Calling library function again to input again.
        }
    }
};

int main(int argc, const char * argv[])
{
    library l1;
    return 0;
}
like image 749
IamBatman Avatar asked May 09 '26 06:05

IamBatman


1 Answers

The problem is that when you call

cin>>mainOption; // mainOption is an int

but the user does not enter an int, cin leaves the input buffer in the old state. Unless your code consumes the invalid portion of the input, the incorrect value entered by the end user would remain in the buffer, causing infinite repetitions.

Here is how you fix this:

} else {
    cout<<"Please Enter Valid Input. "<<endl;
    cin.clear(); // Clear the error state
    string discard;
    getline(cin, discard); // Read and discard the next line
    // option remains true, so the loop continues
}

Note that I also removed the recursion, because your while loop is good enough to handle the task at hand.

like image 159
Sergey Kalinichenko Avatar answered May 10 '26 19:05

Sergey Kalinichenko



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!