469,642 Members | 1,271 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,642 developers. It's quick & easy.

check for blanks or negative numbers

I am writing a routine to check for bad or no input, How can I check for blanks or spaces and negative numers??

negative numbers and spaces just cause the program to lock up. They don't get caught in the if (!cin) statement like I anticipated.



if (!cin) // Checks for input failure
{
cin.clear();
cin.ignore(100, '\n');
if (ctr == 3)
{
a=0; b= 0;
cout << "Too many invalid entries - progam ended.";
}
else
{
cout << "Must be integers... Enter again. " <<endl ;
cout << "Enter two non-zero positive integers<0 to exit>:;
cin >> a >> b;
}
}
else
ctr = 3;
Mar 26 '07 #1
1 1645
Ganon11
3,652 Expert 2GB
You are checking for bad input (such as a string instead of an int), but you are not checking for negative numbers. You can simply add a condition to your while statement to keep executing while the numbers are negative.
Mar 26 '07 #2

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

5 posts views Thread by Subrahmanyam Arya | last post: by
15 posts views Thread by jaks.maths | last post: by
11 posts views Thread by drtimhill | last post: by
39 posts views Thread by Frederick Gotham | last post: by
4 posts views Thread by John Salerno | last post: by
20 posts views Thread by Casey | last post: by
16 posts views Thread by skanemupp | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.