473,513 Members | 8,991 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

can anyone help me with this?

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
7 1293
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

"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
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
1262
by: asdf sdf | last post by:
i have an install issue with moinmoin on win2k. i've tried the mailing list, but it appears to be a very low traffic, low subscriber list. can i get a reality check? is anyone using moinmoin in...
1
2073
by: Simon Harvey | last post by:
Hi chaps, Does anyone know if the beast is going to release Visual Tools for office to work with Visual Studio 2002 and not just 2003, or are they quite happy to shaft those that supported them...
1
1473
by: Greg Steele | last post by:
Does anyone have a copy or know where I can get an installation executable for chiliReports 2.0 from chili! soft? The company I'm working for bought a copy several years ago, then moved their...
2
3679
by: Ken Shaw | last post by:
G'day, I'm trying to tack down any delta compression library that I could licence for use in a project. Does anyone know of a decent binary delta compression library? cheers, Ken Shaw
0
2263
by: james | last post by:
Hi guys! I´ve got a centerd table on my site with a black 1 pixel border. What i would like to do is to add a drop shadow to the table. Anyone know if it's possible to create a drop shadow for a...
162
7072
by: Isaac Grover | last post by:
Hi everyone, Just out of curiosity I recently pointed one of my hand-typed pages at the W3 Validator, and my hand-typed code was just ripped to shreds. Then I pointed some major sites...
0
1067
by: Jay1969 | last post by:
Hey all, I heard there was a new service offered to Microsoft partners called ISV Advisory Services (see link) where MS is giving free dev consulting to their Partners. ...
13
2203
by: penguin732901 | last post by:
Checking back for discussions, there was a lot of talk about 2000 being slower than 97, but not so much lately. What is the latest opinion? Anyone care to set up a poll for how many NG members...
2
3890
by: Brent Taylor via AccessMonster.com | last post by:
HELP----DOES ANYONE HAVE A SIMPLE .mdb for MLM structure? Does anyone have an example database for multi-level marketing for a 3 Tier setup? Thank you, brenttaylor@actionimports.net
2
2309
by: Bruno Alexandre | last post by:
Hi guys, does anyone know where is the Website used in the MSDN "Lear ASP.NET 2.0 with Jeff Prosise" (http://msdn.microsoft.com/asp.net/beta2/multimedia/default.aspx) events? The website used...
0
7254
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
7373
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7432
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...
1
7094
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
4743
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3230
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3218
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
796
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
452
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.