RTTI - how to ?

Discussion in 'C++' started by Victor Bazarov, Jul 25, 2006.

  1. Bit Byte wrote:
    > I am passing data as a string arg to a function. I'd like to determine
    > whether the data is 'text' (i.e. alpha || alphanumeric), ineteger or
    > double).
    >
    > enum
    > {
    > TEXT,
    > INTEGER,
    > DOUBLE
    > }DataType ;
    >
    > DataType foo(const string& arg)
    > {
    > //determine data type
    > ...
    > };
    >
    > Any ideas how to do this ?


    Look up functions 'isalpha', 'isdigit', etc. The usual algorithm is: if
    you can convert it *entirely* into a number, it's a number. If you try
    converting and you have some symbols left over, it's not a number.

    Also, read FAQ 5.2.

    V
    --
    Please remove capital 'A's when replying by e-mail
    I do not respond to top-posted replies, please don't ask
     
    Victor Bazarov, Jul 25, 2006
    #1
    1. Advertising

  2. Victor Bazarov

    Bit Byte Guest

    I am passing data as a string arg to a function. I'd like to determine
    whether the data is 'text' (i.e. alpha || alphanumeric), ineteger or
    double).

    enum
    {
    TEXT,
    INTEGER,
    DOUBLE
    }DataType ;

    DataType foo(const string& arg)
    {
    //determine data type
    ...
    };

    Any ideas how to do this ?
     
    Bit Byte, Jul 25, 2006
    #2
    1. Advertising

  3. Bit Byte wrote:
    > I am passing data as a string arg to a function. I'd like to determine
    > whether the data is 'text' (i.e. alpha || alphanumeric), ineteger or
    > double).
    >
    > enum
    > {
    > TEXT,
    > INTEGER,
    > DOUBLE
    > }DataType ;
    >
    > DataType foo(const string& arg)
    > {
    > //determine data type
    > ...
    > };
    >
    > Any ideas how to do this ?
    >


    First you'd have to come up with a list of criteria that define your 'text',
    'integer' and 'double'. It is probably natural thing to do to arrange the
    criteria in hierarchical fashion: first test for 'double', then for 'integer'
    and the rest is 'text'. But what about the details of each format? Are you
    planning to use the C++ language definitions for integer and floating-point
    literals? Or maybe formats recognized by string conversion functions (like
    'scanf' and 'strtol')? Or you have something else in mind? For example, is '0x1'
    an 'integer' or a 'text'? Is '1e+5' an allowed format of your 'double'? What
    about ' 5 ' (with these extra spaces)? Is it an 'integer' or a 'text'?

    --
    Best regards,
    Andrey Tarasevich
     
    Andrey Tarasevich, Jul 25, 2006
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. John Harrison

    Re: RTTI

    John Harrison, Jul 14, 2003, in forum: C++
    Replies:
    2
    Views:
    474
    John Harrison
    Jul 14, 2003
  2. Alf P. Steinbach

    Re: RTTI

    Alf P. Steinbach, Jul 14, 2003, in forum: C++
    Replies:
    0
    Views:
    483
    Alf P. Steinbach
    Jul 14, 2003
  3. Steven Lien

    About RTTI

    Steven Lien, Aug 19, 2003, in forum: C++
    Replies:
    4
    Views:
    622
    Ivan Vecerina
    Aug 19, 2003
  4. BillyO
    Replies:
    2
    Views:
    502
    dslater
    Sep 30, 2003
  5. tirath
    Replies:
    3
    Views:
    736
    Ivan Vecerina
    Oct 12, 2003
Loading...

Share This Page