Integer input

P

Pycco

I need the user to input an integer, and do some calculation with that
integer. Should the user input an invalid integer, I must return a
warning, saying it's an invalid input.

E.g.

int a;
cin >> a;
if (cin.fail())
cout << "Invalid input.";
else (...)

If the user inputs "four" instead of "4", it'll say "Invalid input".
But if the user inputs "4 + 0", the program won't enter in fail state,
and accept "4" as the value of "a", when it should say "Invalid
input."

What should I do in order to warn the user that "4 + 0" instead of "4"
is also an invalid input?

Thx!
 
V

Victor Bazarov

Pycco said:
I need the user to input an integer, and do some calculation with that
integer. Should the user input an invalid integer, I must return a
warning, saying it's an invalid input.

E.g.

int a;
cin >> a;
if (cin.fail())
cout << "Invalid input.";
else (...)

If the user inputs "four" instead of "4", it'll say "Invalid input".
But if the user inputs "4 + 0", the program won't enter in fail state,
and accept "4" as the value of "a", when it should say "Invalid
input."

What should I do in order to warn the user that "4 + 0" instead of "4"
is also an invalid input?

You should read the whole string, then try converting and see if you
have any left-overs in the string after conversion. If you do, it's
invalid. If you use up the whole string, it's valid.

Victor
 
?

=?ISO-8859-15?Q?Juli=E1n?= Albo

Pycco said:
What should I do in order to warn the user that "4 + 0" instead of "4"
is also an invalid input?

I have this tiny template that can be useful:

template <typename T>
std::istream & lineread (std::istream & is, T & value)
{
std::string line;
getline (is, line);
if (is)
{
std::istringstream iss (line);
iss >> value;
if (! iss)
is.clear (std::ios::badbit);
else
{
char c;
iss >> c;
if (! iss.eof () )
is.clear (std::ios::badbit);
}
}
return is;
}

To use it:


int i;
lineread (std::cin, i);
 
C

Catalin Pitis

Pycco said:
I need the user to input an integer, and do some calculation with that
integer. Should the user input an invalid integer, I must return a
warning, saying it's an invalid input.

E.g.

int a;
cin >> a;
if (cin.fail())
cout << "Invalid input.";
else (...)

If the user inputs "four" instead of "4", it'll say "Invalid input".
But if the user inputs "4 + 0", the program won't enter in fail state,
and accept "4" as the value of "a", when it should say "Invalid
input."

It is a normal behavior. The operator>> will read a string until the first
whitespace is reached. In your example it is the string "4" and it converts
it successfully to int.
What should I do in order to warn the user that "4 + 0" instead of "4"
is also an invalid input?

I didn't try it to see it working, but you might read the whole line (use
std::getline function) and then parse it to implement additional rules that
you need.

Catalin
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top