Results 1 to 3 of 3

Thread: Finding out if value is numeric

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Aug 2000
    Location
    Australia
    Posts
    149

    Finding out if value is numeric

    Hi,
    Just wondering, In c, if i use this code snippit:
    Code:
    int value;
    
    printf("Please enter a value: ");
    scanf("%d", &value);
    how can i then check that value is a numeric value, i know that by declaring it as int it should be numeric, but i have made a menu system that dispalys some options and the user has to enter a number that currosponds to the selection they want to use.

    This works great if the user enters a number, but if a user hits a letter key by mistake, then the menu is just displayed over and over again, without stopping.

    Maybe i need a way to allow the user to only enter numbers and no letters, is that a possibility, and if so, could you please post some sample code for me (the code needs to be in c, and will be used in a console app)..


    Thanks for your help.

    -|- Hurgh -|-
    Email: [email protected]
    Website: http://www.hurgh.org/

    Unix, Linux, FreeBSD, OpenBSD, Solaris, Windows

    C, C++, PHP, VB6, ASP, VBScript, JavaScript

  2. #2
    Hyperactive Member
    Join Date
    Sep 2001
    Posts
    396

    Re: Finding out if value is numeric

    Originally posted by hurgh
    Hi,
    Just wondering, In c, if i use this code snippit:
    Code:
    char value;
    
    printf("Please enter a value: ");
    scanf("%c", &value);
    
    if(value>=48 && value<=57)  //48 to 57 is ascii number for 1-9
    {
            value=value-48;
    }
    else
    {
           cout<<"a alphabet is entered, program exits."<<endl;
           return;
    }
    //do what you want hereafter
    The above is kedaman's method. I think is about time I collect some advertising fees from kedaman.

    hurgh, if you want to use 10 or above, try to figure out yourself, it is not difficult, ok?
    I'm a VB6 beginner.

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    Aug 2000
    Location
    Australia
    Posts
    149
    Thanks for the help, just one last question though

    -|- Hurgh -|-
    Email: [email protected]
    Website: http://www.hurgh.org/

    Unix, Linux, FreeBSD, OpenBSD, Solaris, Windows

    C, C++, PHP, VB6, ASP, VBScript, JavaScript

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