0

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;
}
1
  • 2
    If the input doesn't match the expected type, then the input will not be extracted from the buffer, and will be left for your next attempt. I recommend you read whole lines into strings instead (use std::string and std::getline) and then use e.g. std::stoi to attempt numeric conversion. Commented May 16, 2022 at 8:47

3 Answers 3

1

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 );
Sign up to request clarification or add additional context in comments.

Comments

0

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.

Comments

0
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.".

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.