Home > OS >  Non integer input causes infinite loop
Non integer input causes infinite loop

Time:05-16

i created a code for my final project. where in the start the user is asked what calculator to use its either the average calculator or simple calculator. but if the user accidentally entered a non integer it causes in infinite error loop but if its an integer that is not in the choices it works because i created a while loop. i need help what do i need to do to prevent the infinite loop.

cout << 1. average calculator: << endl;
cout << 2. simple calculator: << endl;
cout << Enter the Number << endl;
cin >> choice;
while (choice > 2 || choice <= 1)
{
cout << "Error! Please choose a number between 1 and 2 only." << endl;
cout << "Enter the number again:";
cin >> choice;
}

CodePudding user response:

You need to clear the input buffer. Also the condition in this if statement is incorrect

while (choice > 2 || choice <= 1)

It seems you mean

while (choice > 2 || choice < 1)

The while loop can be rewritten as do-while loop the following way

#include <limits>

//...

bool input_error = false;
do
{
    input_error = false;

    if ( not ( std::cin >> choice ) )
    {
        std::cin.clear();
        std::cin.ignore( std::numeric_limits<std::streamsize>::max(), '\n' );
        input_error = true;
    }
    else if ( choice < 1 || choice > 2 )
    {
        input_error = true;
    }

    if ( input_error )
    {
        std::cout << "Error! Please choose a number between 1 and 2 only.\n";
        std::cout << "Enter the number again: ";
    }
} while ( input_error );

CodePudding user response:

Instead of using a while loop, you could use a switch like this

switch(choice //this is the input)
{
   case 1:
       //the stuff you want to do
       break; //always a break
   case 2:
       //more stuff
       break;
   default:
       //throwing a error
       std::cout << "Error, please pick a number between 1 and 2\n";
 }

and if you want to repeat until you pick the right number, you could put the switch inside a do while loop like this

do
{
    switch(choice)
    {
        //the stuff
    }
}while(choice > 2 || choice < 1);

let's hope this will work have a nice day.

CodePudding user response:

cout << "1. average calculator:" << endl;
cout << "2. simple calculator:" << endl;
cout << "Enter the Number" << endl;

string stringInput;
getline(cin, stringInput);
int choice = atoi(stringInput.c_str());
while(choice < 1 || choice > 2) {
    cout << "Error! Please choose a number between 1 and 2 only." << endl;
    cout << "Enter the number again:";
    getline(cin, stringInput);
    choice = atoi(stringInput.c_str());
}

You should read whole line, store it in string, and then convert that string to integer using atoi(). atoi() expects c-string to be passed so std::string should be converted to c-string. It is done by stringInput.c_str().

I changed while condition(choice > 2 || choice <= 1) to (choice < 1 || choice > 2) because it says to enter number between 1 and 2, but with your condition entering number 1 would print "Error! Please choose a number between 1 and 2 only.".

  • Related