By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,300 Members | 1,387 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,300 IT Pros & Developers. It's quick & easy.

can anyone help me with this?

P: n/a
Hi there,
I got a problem with this. If I have a fuction like the following
bool valid(int m, int n)
{ if(!isdigit(m)||!isdigit(n))
return false;
else
return true;
}

int main()
{ int a,b;
cin>>a>>b;
cout<<valid(a,b);
system("pause");
return 0;
}

when I input "b 3", the program crashes, it's suppose to print "0",
right? how could this happen? how can I fix it? I want foo to ensure
input for a and b are in a certain range and do not accept letters, of
course

thanks!~

Joseph
Jul 22 '05 #1
Share this Question
Share on Google+
7 Replies


P: n/a
jo******@163.com (Skysword) tried to express:
Hi there,
I got a problem with this. If I have a fuction like the following
bool valid(int m, int n)
{ if(!isdigit(m)||!isdigit(n))
return false;
else
return true;
}

int main()
{ int a,b;
cin>>a>>b;
cout<<valid(a,b);
system("pause");
return 0;
}

when I input "b 3", the program crashes, it's suppose to print "0",
right? how could this happen? how can I fix it? I want foo to ensure
input for a and b are in a certain range and do not accept letters, of
course


the most likely culprit is that you are entering a (char) "b" when an
(int) is expected for (int a).

You need to read in a (string) of characters and parse the arguments by
one of any number of different methods.

Jul 22 '05 #2

P: n/a

"Skysword" <jo******@163.com> wrote in message
news:d8*************************@posting.google.co m...
Hi there,
I got a problem with this. If I have a fuction like the following
bool valid(int m, int n)
{ if(!isdigit(m)||!isdigit(n))
return false;
else
return true;
}

int main()
{ int a,b;
cin>>a>>b;
When you input "b 3" cin goes into a bad state. No operations on a stream
work once it goes into a bad state. So a and b hold garbage values.
The way to check if the user has given right input here is to simply check
state of cin.
if (cin)
{
// ...
} cout<<valid(a,b);
This will fail because a library could make some kind of checks on input
given to it. On MS VC 7 this check is made -

_ASSERTE((unsigned)(c + 1) <= 256);

system("pause");
return 0;
}

when I input "b 3", the program crashes, it's suppose to print "0",


I would use isdigit for character inputs. If I am using ints then checking
stream state is good enough.

-Sharad
Jul 22 '05 #3

P: n/a
This will fail because a library could make some kind of checks on input
given to it. On MS VC 7 this check is made -

_ASSERTE((unsigned)(c + 1) <= 256);


in the isdigit function.

Jul 22 '05 #4

P: n/a
On 7 Aug 2004 00:01:30 -0700, Skysword <jo******@163.com> wrote:
Hi there,
I got a problem with this. If I have a fuction like the following
bool valid(int m, int n)
{ if(!isdigit(m)||!isdigit(n))
return false;
else
return true;
}

int main()
{ int a,b;
cin>>a>>b;
cout<<valid(a,b);
system("pause");
return 0;
}

when I input "b 3", the program crashes, it's suppose to print "0",
right? how could this happen? how can I fix it? I want foo to ensure
input for a and b are in a certain range and do not accept letters, of
course

thanks!~

Joseph


You are inputing a character 'b' when your program is expecting an integer
because you said 'int a'. It the usual situation in programming, the
program is doing exactly what you asked it to, not what you thought you
asked it to.

Here is one way to detect bad input.

#include <iostream>
#include <limits.h> // for INT_MAX
using namespace std;

int a;
for (;;)
{
// prompt the user
cout << "Enter a number from 0 to 999 ";
// read integer and check if entered correctly and in correct range
if (cin >> a && a >= 0 && a <= 999)
break;
// cin might be bad, so clear it
cin.clear();
// disard any extraneous input from cin
cin.ignore(INT_MAX);
// ask the user to input again
cout << "Please try again\n";
}

Look up clear() and ignore() in you favourite C++ book.

This code would be a good candidate to put into a function if you have
several numbers to read.

john
Jul 22 '05 #5

P: n/a
On Sat, 07 Aug 2004 09:15:59 +0100, John Harrison
<jo*************@hotmail.com> wrote:
Here is one way to detect bad input.

#include <iostream>
#include <limits.h> // for INT_MAX
using namespace std;

int a;
for (;;)
{
// prompt the user
cout << "Enter a number from 0 to 999 ";
// read integer and check if entered correctly and in correct range
if (cin >> a && a >= 0 && a <= 999)
break;
// cin might be bad, so clear it
cin.clear();
// disard any extraneous input from cin
cin.ignore(INT_MAX);
// ask the user to input again
cout << "Please try again\n";
}


This is actually pretty cheap and nasty code, it shouldn't really have
suggested it. Suppose your user type in '1b23', I would imagine that you
would want to report this as an error. But this code will not report an
error, instead it will read '1' into variable a and then leave 'b23'
unread. Next time you start to read from cin, you will start by reading
'b23'.

If oyu want to do this properly you should take Wiseguy's suggestion, read
the input into a string, check that the string is exactly what you want
(using isdigit for instance) and then convert the string into an integer.

john
Jul 22 '05 #6

P: n/a
In message <d8*************************@posting.google.com> , Skysword
<jo******@163.com> writes
Hi there,
I got a problem with this. If I have a fuction like the following
bool valid(int m, int n)
{ if(!isdigit(m)||!isdigit(n))
return false;
else
return true;
}


Others have explained what's wrong with the rest of the program, so I'll
just remark that that's an unnecessarily brain-bendingly convoluted way
of writing

bool valid(int m, int n)
{
return isdigit(m) && isdigit(n);
}

Just because the return type is 'bool', you don't have to return a
literal 'true' or 'false'.
--
Richard Herring
Jul 22 '05 #7

P: n/a
Richard Herring wrote:
In message <d8*************************@posting.google.com> , Skysword
<jo******@163.com> writes
Hi there,
I got a problem with this. If I have a fuction like the following
bool valid(int m, int n)
{ if(!isdigit(m)||!isdigit(n))
return false;
else
return true;
}


Others have explained what's wrong with the rest of the program, so I'll
just remark that that's an unnecessarily brain-bendingly convoluted way
of writing

bool valid(int m, int n)
{
return isdigit(m) && isdigit(n);
}

Just because the return type is 'bool', you don't have to return a
literal 'true' or 'false'.


Oh man - you just reduced his line count by 3 and made him 43% less
productive... :-)
Jul 22 '05 #8

This discussion thread is closed

Replies have been disabled for this discussion.