finding the type of a input variable

This is a discussion on finding the type of a input variable within the C++ Programming forums, part of the General Programming Boards category; Hi, I have a quick question. I am designing a program that lets a user either input an int or ...

  1. #1
    Unregistered
    Guest

    Unhappy finding the type of a input variable

    Hi,

    I have a quick question.

    I am designing a program that lets a user either input an int or a string . Is there a method I can use that will return the type of the input value as I need to do different things if they have typed in a int rather than a string.


    thank you

  2. #2
    Registered User
    Join Date
    Feb 2002
    Posts
    114
    Code:
    <ctype.h>
    
    int isalnum( int c ); // returns true if c is a particular representation of an alphanumeric character.
    
    int isalpha( int c ); // returns true if c is a particular representation of an alphabetic character.
    
    __isascii( int c ); // returns true if c is a particular representation of an ASCII character.
    Sorry... got no funktion to test yer whole string

  3. #3
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    accept input only as the string. Pass each char of the input strings to isnum(). If isnum() returns 0 at any char then inut must be considered a string of char and not an int. If no char in the input string fails the test, then convert the string to an int with atoi(), assuming the value of the integer is small enough to fit. Otherwise use atol() if the value is above 35000 (give or take a little). If the only non numerical value is a single period/dot/decimal point, then you can use atof() to convert string to a float.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. sorting number
    By Leslie in forum C Programming
    Replies: 8
    Last Post: 05-20-2009, 04:23 AM
  2. Choosing a variable based on user text input.
    By Compiling... in forum C++ Programming
    Replies: 7
    Last Post: 11-01-2005, 12:21 AM
  3. Question on l-values.
    By Hulag in forum C++ Programming
    Replies: 6
    Last Post: 10-13-2005, 04:33 PM
  4. header file bringing errors?
    By bluehead in forum Windows Programming
    Replies: 4
    Last Post: 08-19-2003, 12:51 PM
  5. variable type char to variable type int
    By Unregistered in forum C Programming
    Replies: 1
    Last Post: 07-15-2002, 08:52 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21