std::cin infinite loop for wrong input

IamBatman

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;
}
Sergey Kalinichenko

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.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

std::cin infinite loop for wrong input

From Dev

std::cin infinite loop from invalid input

From Dev

std::cin resulting in infinite loop

From Dev

Stopping while-loop with std::cin input

From Dev

Program goes into infinite loop in case of wrong input

From Dev

While/if statement causing infinite loop, correct input reads wrong

From Dev

C : infinite loop in the input

From Dev

Limiting input size with std::setw in std::cin

From Dev

std::thread and input with std::cin in an opengl application

From Dev

std::cin: empty the input buffer without blocking

From Dev

C++ std::cin producing extra input

From Dev

std::cin: empty the input buffer without blocking

From Dev

How is "std::cin>>value" evaluated in a while loop?

From Dev

Infinite loop without accepting input

From Dev

Infinite loop without accepting input

From Dev

cin infinite loop when reading in a non-numeric value

From Dev

Circular References in PHPExcel - infinite loop or wrong result

From Dev

Circular References in PHPExcel - infinite loop or wrong result

From Dev

Infinite while loop with a std::ofstream C++

From Dev

std::getline skipping input from std::cin after last occurrence of delimiter, but not with input from std::istringstream

From Dev

My while loop goes into infinite loop, what's wrong?

From Dev

My while loop goes into infinite loop, what's wrong?

From Dev

Stopping and resuming infinite loop on key input

From Dev

Python - Infinite while loop, break on user input

From Dev

Disabling user input during an infinite loop in bash

From Dev

invalid int input gets stuck in an infinite loop

From Dev

While input is not a number, goes into Infinite loop

From Dev

Sudden infinite loop above certain input argument?

From Dev

Input goes in an infinite loop in a C program

Related Related

  1. 1

    std::cin infinite loop for wrong input

  2. 2

    std::cin infinite loop from invalid input

  3. 3

    std::cin resulting in infinite loop

  4. 4

    Stopping while-loop with std::cin input

  5. 5

    Program goes into infinite loop in case of wrong input

  6. 6

    While/if statement causing infinite loop, correct input reads wrong

  7. 7

    C : infinite loop in the input

  8. 8

    Limiting input size with std::setw in std::cin

  9. 9

    std::thread and input with std::cin in an opengl application

  10. 10

    std::cin: empty the input buffer without blocking

  11. 11

    C++ std::cin producing extra input

  12. 12

    std::cin: empty the input buffer without blocking

  13. 13

    How is "std::cin>>value" evaluated in a while loop?

  14. 14

    Infinite loop without accepting input

  15. 15

    Infinite loop without accepting input

  16. 16

    cin infinite loop when reading in a non-numeric value

  17. 17

    Circular References in PHPExcel - infinite loop or wrong result

  18. 18

    Circular References in PHPExcel - infinite loop or wrong result

  19. 19

    Infinite while loop with a std::ofstream C++

  20. 20

    std::getline skipping input from std::cin after last occurrence of delimiter, but not with input from std::istringstream

  21. 21

    My while loop goes into infinite loop, what's wrong?

  22. 22

    My while loop goes into infinite loop, what's wrong?

  23. 23

    Stopping and resuming infinite loop on key input

  24. 24

    Python - Infinite while loop, break on user input

  25. 25

    Disabling user input during an infinite loop in bash

  26. 26

    invalid int input gets stuck in an infinite loop

  27. 27

    While input is not a number, goes into Infinite loop

  28. 28

    Sudden infinite loop above certain input argument?

  29. 29

    Input goes in an infinite loop in a C program

HotTag

Archive