|
-
Sep 6th, 2001, 07:47 PM
#1
Thread Starter
Hyperactive Member
Is it numeric?
Is their a way I can check if a variable contains a float or integer? I can't find a specific function which does so. Thanks.
Matt 
-
Sep 7th, 2001, 04:07 AM
#2
Frenzied Member
Do you mean a string representation of a number? I'll assume you do.
You can just try and apply atoi() and atof() to the strings, and if they can't be converted to int or float the return value of the functions will be 0. If the return value is any other number you know it successfully converted it, which means it must be the correct type.
Harry.
"From one thing, know ten thousand things."
-
Sep 7th, 2001, 06:12 AM
#3
You could also use isdigit, but this only accepts a single character as argument. You could test the first character of a string however...
isdigit(*str) or isdigit(str[0])
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
-
Sep 7th, 2001, 07:24 AM
#4
New Member
you can define a macro:
#define isinteger(x) ((x)==((int)(x)))
and use it like this:
int a=2;
float b=2.0, c=2.5;
if isinteger(a) cout << "a is integer\n";
if isinteger(b) cout << "b is integer\n";
if isinteger(c) cout << "c is integer\n";
I am the dominator of the dominator the Zirich nut of Qutan
My population calls me Sirius Zor'Z
-
Sep 7th, 2001, 02:38 PM
#5
Thread Starter
Hyperactive Member
All I want to do is say
Code:
cout<<"Enter a number";
cin >> x;
//do something here to check if its a float.
Matt 
-
Sep 7th, 2001, 02:58 PM
#6
Monday Morning Lunatic
Code:
float x;
cout << "Enter a number: ";
cin >> x;
Sorted
I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
-- Linus Torvalds
-
Sep 8th, 2001, 08:16 AM
#7
Lively Member
My FloatPart Function
You could use my FloatPart Function :-D
PHP Code:
inline double FloatPart(double f)
{
return(f-(int)f);
}
If its a float, it should return 0.0
I have been having some problems with this lately... So use it at your own risk
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|