0

I have a while true loop to force the user to input the right type of input and make sure that input is between 1-8. When I try to use !cin or cin.fail to check if the input is integer or not, my output goes on a loop forever.

       // Taking amount of courses user is enrolled in
int courses = 0;
while (true)
{
    cout << "How many courses do you want to take?\n If this is not the first time you are seeing this prompt,\n that means you entered a faulty input. Enter a number \n between 1 and 8.";
    cin >> courses;
    if (cin.fail())
    {
        cout << "You entered a non integer value such as Z or 0.2\n. Please enter an integer value\n";
    }
    if (courses > 0 && courses<= 8)
    {
        cout << "You have entered " << courses << " courses\n";
        break;
    }
}

Could the while true loop be causing the issue and if so, how could I fix it while having the same desired effect of running until user inputs a number between 1-8.

How the output looks:

 If this is not the first time you are seeing this prompt,
 that means you entered a faulty input. Enter a number
 between 1 and 8.You entered a non integer value such as Z or 0.2
. Please enter an integer value
How many courses do you want to take?
 If this is not the first time you are seeing this prompt,
 that means you entered a faulty input. Enter a number
 between 1 and 8.You entered a non integer value such as Z or 0.2
. Please enter an integer value
How many courses do you want to take?
 If this is not the first time you are seeing this prompt,
 that means you entered a faulty input. Enter a number
 between 1 and 8.You entered a non integer value such as Z or 0.2
. Please enter an integer value
How many courses do you want to take?
 If this is not the first time you are seeing this prompt,
 that means you entered a faulty input. Enter a number
 between 1 and 8.You entered a non integer value such as Z or 0.2
. Please enter an integer value
How many courses do you want to take?
 If this is not the first time you are seeing this prompt,
 that means you entered a faulty input. Enter a number
 between 1 and 8.You entered a non integer value such as Z or 0.2
. Please enter an integer value
How many courses do you want to take?
 If this is not the first time you are seeing this prompt,
 that means you entered a faulty input. Enter a number
 between 1 and 8.You entered a non integer value such as Z or 0.2
. Please enter an integer value
How many courses do you want to take?
 If this is not the first time you are seeing this prompt,
 that means you entered a faulty input. Enter a number
 between 1 and 8.You entered a non integer value such as Z or 0.2
. Please enter an integer value
How many courses do you want to take?
 If this is not the first time you are seeing this prompt,
 that means you entered a faulty input. Enter a number
 between 1 and 8.You entered a non integer value such as Z or 0.2
. Please enter an integer value
How many courses do you want to take?
 If this is not the first time you are seeing this prompt,
 that means you entered a faulty input. Enter a number
 between 1 and 8.You entered a non integer value such as Z or 0.2
. Please enter an integer value
How many courses do you want to take?
 If this is not the first time you are seeing this prompt,
 that means you entered a faulty input. Enter a number
 between 1 and 8.You entered a non integer value such as Z or 0.2
. Please enter an integer value
How many courses do you want to take?
 If this is not the first time you are seeing this prompt,
 that means you entered a faulty input. Enter a number
 between 1 and 8.You entered a non integer value such as Z or 0.2

I had to force stop it with the stop button in the ide console.

Edit: My program works if cin.fail is not true. If user enters an integer from the beginning, the output does not loop.

  • 1
    Right. Once `cin` has failed, it is in a bad state and no further reads will succeed, so you will get stuck in a loop of failed reads. See https://stackoverflow.com/a/39282970/1171191 – BoBTFish Feb 05 '21 at 15:24
  • @BoBTFish Thank you for that. Sorry about the duplicate question. – Rohan Parikh Feb 05 '21 at 15:28
  • No worries - I already knew what to search for, you probably did not :) – BoBTFish Feb 05 '21 at 15:29

0 Answers0