isnumeric

K

Kenny

Hello,

I would like to know if the function IsNumeric requires a header like
#include <iostream> to be functionnal

thanks

ken
 
L

Leor Zolman

Hello,

I would like to know if the function IsNumeric requires a header like
#include <iostream> to be functionnal

There's no function by that name in either the C or C++ libraries.
Actually, I don't know of /any/ standard library functions whatsoever that
have capital letters in their names.

If you're asking about the character classification functions such as
isdigit(), isalpha(), et. al., then for C++ you should include <cctype>.
-leor
 
K

Kenny

that did not work ,
do you know the parameter setting of that calling function ?

K
 
K

Karl Heinz Buchegger

Kenny said:
that did not work ,
do you know the parameter setting of that calling function ?

Kenny. Please make it a habit of yours to *not* post:
'It doesn't work'

Tell us *what* the problem is: Do you get a compiler
error? Do you get a runtime error? Does the program not
behave the way you want it to?

Also: post a small code snippet such that we can see what
you have tried and what gave you troubles. Ideally you post
a small, *complete* program, such that we can cut&paste
it into our development environments and try the very
same as you did.

If you don't do this then all that is left to us is: guess
And guessing is never a good way to figure out technical
problems.
 
K

Kenny

Kenny. Please make it a habit of yours to *not* post:
'It doesn't work'

Tell us *what* the problem is: Do you get a compiler
error? Do you get a runtime error? Does the program not
behave the way you want it to?

Also: post a small code snippet such that we can see what
you have tried and what gave you troubles. Ideally you post
a small, *complete* program, such that we can cut&paste
it into our development environments and try the very
same as you did.

If you don't do this then all that is left to us is: guess
And guessing is never a good way to figure out technical
problems.
ok here is the code

cout << " Your choice please: ";
cin >> choice;
if (isnumeric(choice) == true){
 
T

Thomas Matthews

Kenny said:
that did not work ,
do you know the parameter setting of that calling function ?

K

#include <iostream> // for cout
#include <cstdlib> // for EXIT_SUCCESS
#include <ctypes> // for isdigit

using std::cout;
using std::endl;
using std::isdigit;

int main(void)
{
cout << "Is '5' a digit (numeric)?" << endl;
cout << isdigit('5') << endl;
cout << "\nIs 'Z' a digit?" << endl;
cout << isdigit('Z') << endl;
return EXIT_SUCCESS;
}

Much can be learned by reading a C++ reference book
and the FAQ.

--
Thomas Matthews

C++ newsgroup welcome message:
http://www.slack.net/~shiva/welcome.txt
C++ Faq: http://www.parashift.com/c++-faq-lite
C Faq: http://www.eskimo.com/~scs/c-faq/top.html
alt.comp.lang.learn.c-c++ faq:
http://www.raos.demon.uk/acllc-c++/faq.html
Other sites:
http://www.josuttis.com -- C++ STL Library book
 
K

Kenny

Thomas Matthews said:
#include <iostream> // for cout
#include <cstdlib> // for EXIT_SUCCESS
#include <ctypes> // for isdigit

using std::cout;
using std::endl;
using std::isdigit;

int main(void)
{
cout << "Is '5' a digit (numeric)?" << endl;
cout << isdigit('5') << endl;
cout << "\nIs 'Z' a digit?" << endl;
cout << isdigit('Z') << endl;
return EXIT_SUCCESS;
}

Much can be learned by reading a C++ reference book
and the FAQ.


I guess my book from Walter Savitch , absolute c++ is not so good.
 
K

Karl Heinz Buchegger

Kenny said:
ok here is the code

cout << " Your choice please: ";
cin >> choice;
if (isnumeric(choice) == true){

OK.
And what is the problem with this code?

Note: isnumeric() isn't one of the standard functions
So you must have written it by yourself. What this
function exactly has to look like is dependent on the
data type of choice.

So what is choice?
Where is the implementation of isnumeric()?
and last but not least: Which error message did you
get from your compiler.
Could it be that you wanted isdigit() instead of isnumeric()
(Note: isdigit() checks a *single* character if it represents
a numerical digit, not a whole string. But using this function
it is easy to write a function isNumeric which does the whole
thing on a complete string).


I know all of this may sound childish to you. But remember:
*You* sit in front of your computer. *You* write the program.
*You* see the error messages from your compiler. If we try
to help you, we can only use what *you* tell us. If you
call your doctor and cry: "Help" I am sure your doctor would
do the very same: Ask for details. If you are a good patient
you wouldn't let your doctor ask you for everything but provide
him with all you know about your problem in the first place.
We *want* to help you, that's why we are here and answer questions.
But we also expect people seeking for help to coorporate.
 
K

Karl Heinz Buchegger

Kenny said:
I guess you did not try the term 'isnumeric' in google I just did not
find a page where it was talking about how to call it in c++, just VB and
C#.

AS Leor has pointer out already, there is *no* function called
isnumeric() in standard C or standard C++.
Depending on what you actually want you have to
* use isdigit() out of the box
* write your own function isnumeric()
 
K

Kenny

Karl Heinz Buchegger said:
OK.
And what is the problem with this code?

Note: isnumeric() isn't one of the standard functions
So you must have written it by yourself. What this
function exactly has to look like is dependent on the
data type of choice.

So what is choice?
Where is the implementation of isnumeric()?
and last but not least: Which error message did you
get from your compiler.
Could it be that you wanted isdigit() instead of isnumeric()
(Note: isdigit() checks a *single* character if it represents
a numerical digit, not a whole string. But using this function
it is easy to write a function isNumeric which does the whole
thing on a complete string).


I know all of this may sound childish to you. But remember:
*You* sit in front of your computer. *You* write the program.
*You* see the error messages from your compiler. If we try
to help you, we can only use what *you* tell us. If you
call your doctor and cry: "Help" I am sure your doctor would
do the very same: Ask for details. If you are a good patient
you wouldn't let your doctor ask you for everything but provide
him with all you know about your problem in the first place.
We *want* to help you, that's why we are here and answer questions.
But we also expect people seeking for help to coorporate.


Choice is an int, I read somewhere that isnumeric is a built in function ,
just like strcpy . Is numeric should return true if it is a numeric value
and false other wise. so isnumeric is not in c++ , just in c ??

K
 
K

Karl Heinz Buchegger

Kenny said:
Choice is an int,

Then I am afraid: What you want to do cannot be done.
An int holds a number per definition! That's what it is for.
I read somewhere that isnumeric is a built in function ,
just like strcpy . Is numeric should return true if it is a numeric value
and false other wise. so isnumeric is not in c++ , just in c ??

Not even there.
An int always holds a number! That's the definition of int. You can't
store "Hello world" in an int.

What you *want* to do is something completely different:
You want to check the users input if the user entered a number.
Well if you use

int choice;

cin >> choice;

Then the user has no other possibility: He *has* to enter a number
or otherwise the input stream would simply refuse to accept the
users input and go into a fail state.

There are 2 possibilities you can cope with that problem:
* detect that the stream has gone into a fail state, clear
that fail state and discard all pending input

* Don't read into an int. Read into a string instead. This
way the user can enter what he wants, a string can accept
everything. Then your part begins. You have to check if
what the user has entered is what you expected him to enter
and react accordingly.
 
T

Thomas Matthews

Kenny wrote:

[snip]
Choice is an int, I read somewhere that isnumeric is a built in function ,
just like strcpy . Is numeric should return true if it is a numeric value
and false other wise. so isnumeric is not in c++ , just in c ??

K

Sorry Kenny, but isnumeric is not in C either.

Check your book carefully. Many authors write primitive
functions in other chapters and reference them throughout
the book.

The easiest method to test if an input value is numeric,
is to use the extraction operator, ">>" and read into
an integral (i.e. unsigned or signed integer) variable.
If the stream state is good, then the value is numeric.

Here is a common idiom:
unsigned int option_num;
do
{
// Display numeric options,
// such as 1. Do Something.
cout.flush(); // For synchronization.
cin >> option_num;
} while (cin.fail() || (option < 1) || ...);


--
Thomas Matthews

C++ newsgroup welcome message:
http://www.slack.net/~shiva/welcome.txt
C++ Faq: http://www.parashift.com/c++-faq-lite
C Faq: http://www.eskimo.com/~scs/c-faq/top.html
alt.comp.lang.learn.c-c++ faq:
http://www.raos.demon.uk/acllc-c++/faq.html
Other sites:
http://www.josuttis.com -- C++ STL Library book
 

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,743
Messages
2,569,478
Members
44,899
Latest member
RodneyMcAu

Latest Threads

Top