Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Integer input

Reply
Thread Tools

Integer input

 
 
Pycco
Guest
Posts: n/a
 
      10-23-2004
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!
 
Reply With Quote
 
 
 
 
Victor Bazarov
Guest
Posts: n/a
 
      10-24-2004
"Pycco" <(E-Mail Removed)> wrote...
>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


 
Reply With Quote
 
 
 
 
=?ISO-8859-15?Q?Juli=E1n?= Albo
Guest
Posts: n/a
 
      10-24-2004
Pycco wrote:

> 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);

--
Salu2
 
Reply With Quote
 
Catalin Pitis
Guest
Posts: n/a
 
      10-25-2004

"Pycco" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) m...
>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


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How do I add an Integer to another Integer? Sebastian Stelzer Java 6 04-07-2010 07:03 PM
CType(x,Integer) vs. Integer.Parse(x) =?Utf-8?B?Sm9l?= ASP .Net 7 02-07-2006 02:30 AM
how do I make Class.forName("Integer") returning java.lang.Integer? Johannes Zellner Java 22 12-19-2005 11:22 AM
How do I add an Integer to another Integer? Sebastian Stelzer Java 2 10-15-2004 01:17 PM
No Math.min(Integer, Integer)? =?ISO-8859-1?Q?Thomas_Gagn=E9?= Java 0 07-29-2003 07:46 PM



Advertisments