Thread: Function to check whether user has entered a number or not?

  1. #1
    Registered User
    Join Date
    Apr 2009
    Posts
    18

    Question Function to check whether user has entered a number or not?

    I want to be able to check if the user has entered "two" or even gibberish as one of the number inputs.

    What i have so far checks whether each input = 0. If it does it confirms that with the user before continuing. Is there a function that can check whether a string has anything else but a number?

    Thanks in advance,

    Jez
    Last edited by JezW; 04-12-2009 at 09:02 AM.

  2. #2
    DESTINY BEN10's Avatar
    Join Date
    Jul 2008
    Location
    in front of my computer
    Posts
    804
    you can run this function in a loop.
    Code:
    for(i=0;s[i]!='\0';i++)
    if(!isdigit(s[i]))
    {
    ........do anything
    }
    isdigit(s[i]) checks whether s[i] is a digit or not.if s[i] is a digit it returns true else returns zero(false).this function is independent of the type of encoding.it is declared in <ctype.h>

  3. #3
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    a lot easier I think will be reading string with fgets
    then parsing it with strtol or strtod
    and check that the end pointer is pointing to \n character
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  4. #4
    Registered User
    Join Date
    Apr 2009
    Posts
    18
    Thanks for the replies.

    Ben10, the isdigit function sounds like what i'm looking for, i'll give it a go

    vart please can you elaborate on fgets, and parsing with strtol or strtod?

    Sorry i'm still pretty new to this.

    Jez

  5. #5
    Registered User
    Join Date
    Apr 2009
    Posts
    18
    could i have something like this? can't get it work properly...

    Code:
    int functiona(void)
    {
    
        system("cls");                          /*This clears the screen and displays the data*/
        displaydata();
        printf("\n                          Please enter a new value for A:\n\n                                         ");         /*Gets the new value of a*/
    
    
    while(1)
        {
            gets(a);
            numbera = atof(a);
    
            if(isdigit(numbera)==1)
            {
            // do some stuff
            break;
            }
    
            if(isdigit(numbera)==0)
            {
            system("cls")
            displaydata()
            }
        }
    }
    Thanks,

    Jez

  6. #6
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    1. Do NOT use gets - it is one of the most dangerous functions because it will continue inputting until it sees a newline - so if someone falls asleep on the A key and then hits enter half a minute later, it will accept all those A's as input. The gets function can therefore quite easily cause a crash, and has been the source of MANY security holes in various applications.

    2. atof(), if the input is a valid floating point number, will return a floating point value. isdigit checks if a character input is a digit. It does not expect a floating point value - most likely it will translate to an integer if you have included the right header files, in which case numbers that translate to 48 .. 57 will be "ok", and others not ok. If you haven't included the right header file, no one knows what will happen. Nothing, nuclear launch, or program crash is all possible scenarios - do not do that!

    3. atof() is not very good at telling you if the translation was complete or not. The function strtof() or strtod() will do this better.

    3. I don't think it's guaranteed that isdigit returns 1 for "is a digit" - it is guaranteed that it returns zero for "is NOT a digit". So do not do "isdigit(x) == 1".

    4. You are missing a curly brace at the end of functiona(). (And petty detail: You are clearing the screen before functiona, then clearing the screen again inside functiona).

    5. You have two if-statements - they are (aside from point 3) mutually exclusive - if one is true, then the other is false guarnteed [and if the isdigit doesn't return exactly 1 and 0 neither is true - but you were going to fix point 3, right?]. In this case, you do not need two if-statements, just one if with an else to for the other alternative.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  7. #7
    Registered User
    Join Date
    Apr 2009
    Posts
    18
    thanks for the reply matsp. couple of Qs:

    1.Can you elaborate on the difference between scanf and gets? What's the difference between entering aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa when prompted by a gets or when prompted by a scanf?

    3.Any chance of an example of strtof() or strtod() in action?

    4. Are you sure that's missing a brace....? I closed the "if", closed the "while" then closed functiona. Point taken about the clear screen.

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,412
    Quote Originally Posted by JezW
    1.Can you elaborate on the difference between scanf and gets? What's the difference between entering aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa when prompted by a gets or when prompted by a scanf?
    gets() will read in the whole line regardless of the size of the array that you are storing the input in. scanf with the %s format specifier will do the same, but you can specify the maximum width, i.e., the maximum number of characters to read in, and that allows you to limit the amount of input read to the size of your destination array.

    Quote Originally Posted by JezW
    4. Are you sure that's missing a brace....? I closed the "if", closed the "while" then closed functiona. Point taken about the clear screen.
    I think matsp is mistaken, but I must say that you could format your code a little better.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 08:18 PM
  2. xor linked list
    By adramalech in forum C Programming
    Replies: 23
    Last Post: 10-14-2008, 10:13 AM
  3. Replies: 28
    Last Post: 07-16-2006, 11:35 PM
  4. writing/reading from file produces double results.
    By stumon in forum C Programming
    Replies: 4
    Last Post: 03-20-2003, 04:01 PM
  5. qt help
    By Unregistered in forum Linux Programming
    Replies: 1
    Last Post: 04-20-2002, 09:51 AM