473,386 Members | 1,748 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Integer input

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!
Jul 22 '05 #1
3 2079
"Pycco" <or*************@ntlworld.com> 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
Jul 22 '05 #2
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
Jul 22 '05 #3

"Pycco" <or*************@ntlworld.com> wrote in message
news:67*************************@posting.google.co 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
Jul 22 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Paul E Collins | last post by:
Given a string variable (form input), how can I determine whether it represents a valid integer? is_numeric is true for floats as well as integers, and is_int always fails on a string. P.
10
by: dave | last post by:
I am reading input from a form. I want to validate the input by making sure that the string is actually an integer. How would I do this? Do i need to convert it to a character array and break down...
8
by: Radith | last post by:
Hi All, I have a number guessing game in which users try to guess a random number. Obviously, input is required of type int. BUT, when a user inputs a string the program will result in an...
12
by: obdict | last post by:
Hello, I have a simple C question regarding scanf() /* What happens if user inputs a char when he/she is supposed * to input an integer? */ #include<stdio.h> int main(void){ int n;
8
by: Candace | last post by:
I am using the following code to pick off each digit of a number, from right to left. The number I am working with is 84357. So for the first iteration it should return the number 7 and for the...
1
by: redpayne | last post by:
Okay, I finally got this program to run according to what the book had us build it as. Now prof wants case 2 and case 3 to prompt again for input, check input to see if it is the correct type, then...
2
by: rsk | last post by:
Friends, The following logic just converts the hexadecimal values to the binay values taken into the array. for(i=31;i>=0;i--){ if ((1<<i)&var1) array_var1=1; else array_var1=0;
1
by: haelly | last post by:
write a program that prompts the user to enter three different positive integer values.If the values are not different, the program prints a message"equal value" and terminates(hint:use the return...
2
by: robtyketto | last post by:
Greetings, Within my jsp I have HTML code (see below) which accepts input, one of these fields sequence unlike the others is an Integer. <FORM ACTION="wk465682AddFAQ.jsp" METHOD="POST"> Id:...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.