can anyone help me with this?

S

Skysword

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
 
W

Wiseguy

(e-mail address removed) (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.
 
S

Sharad Kala

Skysword said:
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
 
S

Sharad Kala

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.
 
J

John Harrison

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
 
J

John Harrison

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
 
R

Richard Herring

Skysword said:
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'.
 
G

Gianni Mariani

Richard said:
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... :)
 

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,484
Members
44,906
Latest member
SkinfixSkintag

Latest Threads

Top