Connecting Tech Pros Worldwide Forums | Help | Site Map

can anyone help me with this?

Skysword
Guest
 
Posts: n/a
#1: Jul 22 '05
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

Wiseguy
Guest
 
Posts: n/a
#2: Jul 22 '05

re: can anyone help me with this?


josephcm@163.com (Skysword) tried to express:[color=blue]
> 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[/color]

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.

Sharad Kala
Guest
 
Posts: n/a
#3: Jul 22 '05

re: can anyone help me with this?



"Skysword" <josephcm@163.com> wrote in message
news:d86e683a.0408062224.9743025@posting.google.co m...[color=blue]
> 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;[/color]

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)
{
// ...
}[color=blue]
> cout<<valid(a,b);[/color]

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

[color=blue]
> system("pause");
> return 0;
> }
>
> when I input "b 3", the program crashes, it's suppose to print "0",[/color]

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

-Sharad


Sharad Kala
Guest
 
Posts: n/a
#4: Jul 22 '05

re: can anyone help me with this?


[color=blue]
> 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);[/color]

in the isdigit function.



John Harrison
Guest
 
Posts: n/a
#5: Jul 22 '05

re: can anyone help me with this?


On 7 Aug 2004 00:01:30 -0700, Skysword <josephcm@163.com> wrote:
[color=blue]
> 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[/color]

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
John Harrison
Guest
 
Posts: n/a
#6: Jul 22 '05

re: can anyone help me with this?


On Sat, 07 Aug 2004 09:15:59 +0100, John Harrison
<john_andronicus@hotmail.com> wrote:
[color=blue]
> 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";
> }
>[/color]

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
Richard Herring
Guest
 
Posts: n/a
#7: Jul 22 '05

re: can anyone help me with this?


In message <d86e683a.0408062224.9743025@posting.google.com> , Skysword
<josephcm@163.com> writes[color=blue]
>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;
>}
>[/color]

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
Gianni Mariani
Guest
 
Posts: n/a
#8: Jul 22 '05

re: can anyone help me with this?


Richard Herring wrote:[color=blue]
> In message <d86e683a.0408062224.9743025@posting.google.com> , Skysword
> <josephcm@163.com> writes
>[color=green]
>> 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;
>> }
>>[/color]
>
> 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'.[/color]

Oh man - you just reduced his line count by 3 and made him 43% less
productive... :-)
Closed Thread