0

I have this snippets of code from my original long program, and as much as it looks simple, it doesn't work correctly! I am brand-new to c++ language, but I know in Java that would be the way to do it (Regardless of the syntax).

Simply put, this should ask the user for an input to answer the following multiplication (5*5), however, it should also check if the user entered a wrong input (not number), keep asking the user again and again... Somehow, it keeps running forever without taking a new input!!

I hope to get, not only an answer, but also a reason for such an error!

int main() { int userAnswer; bool isValidAnswer = true; cout << 5 << " * " << 5 << " = "; cin >> userAnswer; cin.ignore(); do { if (cin.fail()) { //user input is not an integer cout << "Your answer is not valid! Please enter only a natural number: "; cin >> userAnswer; cin.ignore(); } else { isValidAnswer = false; } } while (isValidAnswer); return 0; } 

2 Answers 2

2

Well you need to clear the error state before accepting new input. Call cin.clear() then cin.ignore() before trying to read input again.

I would do something like.

cout << "Enter a number: "; cin >> number; while(cin.fail()) { cin.clear(); cin.ignore(1000, '\n'); //some large number of character will stop at new line cout << "Bad Number Try Again: "; cin >> number; } 
Sign up to request clarification or add additional context in comments.

10 Comments

I tried to use cin.clear() but nothing changed. Your code is having the same problem too!! It never excepts the new input and I keep pressing enter without ending the loop!
Oops. It actually needs to be cin.ignore(1000, '\n'); You need to tell it to ignore up to the new line.
what fail() and clear() exactly do, so that it caused such a problem?
fail is a way of checking if cin failed. It returns a boolean. Clear clears the error state so that you can continue to use the stream. When cin fails that bad input is left in the stream so after you clear the error state you need to ignore the bad input.
What if I did something like that 123B, then what is going to happen? Is it going to fail or not?
|
0

First, cin.fail() is not going to adequately check if your answer is a natural number or not with the type set to int (could also be negative).

Second, your boolean isValidAnswer is really checking if it's is an invalid answer.

Third (and most importantly), as another answer suggests, you should put in cin.clear() to clear the failure state, and then followed by cin.ignore(), which will remove the failed string from cin.

Fourth, cin will only check if an int exists somewhere in the string. You'll need to perform your own string comparison to determine if the entire input is a int (see answer below, based on this answer).

Updated:

#include <iostream> #include <string> #include <cstdlib> using namespace std; bool isNum(string line) { char* p; strtol(line.c_str(), &p, 10); return *p == 0; } int main() { int userAnswer; string input; bool isInvalidAnswer = true; cout << 5 << " * " << 5 << " = "; while (isInvalidAnswer) { if (!(cin >> input) || !isNum(input)) { cout << "Answer is not a number! Please try again:\n"; cin.clear(); cin.ignore(); } else { userAnswer = atoi(input.c_str()); if (userAnswer < 0) { //user input is not an integer cout << "Answer is not a natural number! Please try again:\n"; } else { isInvalidAnswer = false; } } } cout << "Question answered!\n"; return 0; } 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.