Results 1 to 7 of 7

Thread: Is it numeric?

  1. #1

    Thread Starter
    Hyperactive Member MPrestonf12's Avatar
    Join Date
    Jun 1999
    Location
    NY
    Posts
    330

    Question 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

  2. #2
    Frenzied Member HarryW's Avatar
    Join Date
    Jan 2000
    Location
    Heiho no michi
    Posts
    1,827
    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."

  3. #3
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    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.

  4. #4
    New Member Sirius Zor'Z's Avatar
    Join Date
    Sep 2001
    Location
    Bagburhaistan
    Posts
    3
    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

  5. #5

    Thread Starter
    Hyperactive Member MPrestonf12's Avatar
    Join Date
    Jun 1999
    Location
    NY
    Posts
    330
    All I want to do is say

    Code:
    cout<<"Enter a number";
    cin >> x;
    
    //do something here to check if its a float.
    Matt

  6. #6
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    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

  7. #7
    Lively Member
    Join Date
    Dec 2000
    Location
    Indiana
    Posts
    73

    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
  •  



Click Here to Expand Forum to Full Width