3

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;
}
3
  • 2
    Please don't use constructors like they are normal functions Commented Apr 18, 2014 at 21:55
  • @clcto you mean, we should use contractors when they have parameters? Commented Apr 18, 2014 at 21:56
  • 1
    Constructors are used to set the initial state of the object. This all should really be in a void run() function. You don't even need an object for this. Commented Apr 18, 2014 at 21:59

1 Answer 1

5

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.