Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

Cheaking if value is char or number (in c++)

Options
  • 02-04-2005 5:55pm
    #1
    Registered Users Posts: 221 ✭✭


    can anyone tell me how i can i test if a value is string, char, or number

    all help much appriciated (i'm using visual c++ 6.0)


Comments

  • Registered Users Posts: 15,258 ✭✭✭✭Rabies


    Could you check if its ascii value is within a certain range then you should know


  • Registered Users Posts: 221 ✭✭Elfman


    But of course

    cheers dude that really good any idea where i could find said ranges,

    em... how would i out that as an if test ??

    thanks again , is there an easier way like in VB ??


  • Moderators, Recreation & Hobbies Moderators, Science, Health & Environment Moderators, Technology & Internet Moderators Posts: 91,687 Mod ✭✭✭✭Capt'n Midnight


    is c++ the language where data types have to be defined before use by the programmer or is it the language where you can preform just about any operation on a variable and it's up to the programmer to remember what the variable is ??

    A way to rule out an ascii char would be to check the first few bits.
    0011 - will be the start of a number character - 010 and 011 are possible letter characters - then you can rule out the other alphanumerics.
    course this is no use in foreign langagues but you could tweak it using XOR so that it would ignore non-alphanumerics very quickly

    [php]if a and 128 goto end
    if (a and 64) or (a and 64) check ifletter
    if (a and 32) and (a and 16) check ifnumber

    'Alternative'
    b=a or 32 'if b is a lower case letter , a will be also be a letter irrespective of case'[/php]


  • Registered Users Posts: 221 ✭✭Elfman


    Yeah in c++ you must define your varibles before u use them e.g.

    int Myint;
    char my character

    can u tell me how to phrase that as a test ??(the bit abour ascii i mean


  • Registered Users Posts: 4,287 ✭✭✭NotMe


    How come you don't know what type the value is? What is the value stored as?


  • Advertisement
  • Registered Users Posts: 221 ✭✭Elfman


    Sorry i'm prob not explaining this well,

    I'm taking in a value from an edit box which has to be a string and i want to test to make sure it's only letters


  • Registered Users Posts: 4,287 ✭✭✭NotMe


    Here's an ASCII table: http://www.lookuptables.com/
    You can see that A-Z goes from 65 to 90 and a-z goes from 97 to 122. So to check if your string is all letters just change each char in the string to an int and compare to those values
    ie.
    [PHP]char *str = new char[5];
    str = "abcd";

    for(int i = 0; i < 4; i++)
    {
    if(((int)s >= 65 && (int)s <= 90) // uppercase
    || ((int)s >= 97 && (int)s <= 122)) // lowercase
    {
    // character is a letter
    }
    }
    [/PHP]


  • Registered Users Posts: 4,003 ✭✭✭rsynnott


    Beware of ASCII, lest you must one day run your program on an IBM mainframe ;)


  • Moderators, Recreation & Hobbies Moderators, Science, Health & Environment Moderators, Technology & Internet Moderators Posts: 91,687 Mod ✭✭✭✭Capt'n Midnight


    Forget VB, IBM code sounds like a bad case of VD ;)

    "eb-sih-dik" / ĕb'sē-dĭk

    ASCII stupid question, get a stupid ANSI.


  • Moderators, Recreation & Hobbies Moderators, Science, Health & Environment Moderators, Technology & Internet Moderators Posts: 91,687 Mod ✭✭✭✭Capt'n Midnight


    Elfman wrote:
    to make sure it's only letters
    [php]'You want letters'
    c--
    c=((c and 31) mod 25 ) or 64
    c++ 'you got letters'[/php]


  • Advertisement
  • Registered Users Posts: 221 ✭✭Elfman


    Your al stars what ye ____ ??

    thanks all u were loads of help (unlike microsoft)


  • Registered Users Posts: 1,391 ✭✭✭fatherdougalmag


    And you don't need to specify the ASCII character constant (65 for 'e', etc.). Just enclose the character in single quotes ('a' ... 'z', etc.) and the compiler will convert it for you.

    Alternatively, use the 'is...' family of routines (isalnum, isdigit, etc.) on each character in the string.


  • Closed Accounts Posts: 7 MDMA


    Elfman wrote:
    can anyone tell me how i can i test if a value is string, char, or number

    all help much appriciated (i'm using visual c++ 6.0)

    #ok i agree with the others the data in a datatype is defined by u .ie an int in memory is really stored the same as a char so its how u look at this value that tells u the difference so fo example

    int imanumber=1; //cout this and you will get the number 1;

    but the ascii value of 1 is some funny bell or something i think....
    but when u said INT you told the compiler this is an int any value it holds
    is a number and nothing else..

    i think there is a fn worth mentioning/// in String.h
    isNumeric() bool fn pass an int or char as args//

    now u could have said
    char imaletter = 3; and indeed this is a character when cout this u will get the ascii value for 3; this is because u told the compiler that imaletter will store a character and nothing else.

    now when u say
    imanumber = imaletter;

    imanumber will now be 3..but its still a number...

    as for strings... well this is again the same principle

    String blah = "funny"; //i understand what u want to do u want to see if this is a string pretending u dont "know if its a string char or int" well to be honest u can treat it anyway u like because it will hole a number character etc..if u want you could debug the code in a dissasembler and change a few instructions to get a better understanding of how they are all bunched together...ok im tired now so im going to stop ..if this helped good stuff if not ....?

    also u could do isalpha(char)

    Cheers
    MDMA


Advertisement