Thread: Numbers detection in input

  1. #1
    Registered User
    Join Date
    Oct 2014
    Posts
    8

    Numbers detection in input

    Hello,

    I need to distinguish between number (float) and other non number characters.

    I was trying to use this:

    Code:
    #include <stdio.h>
    #include <ctype.h>
    
    float Ax;
    ...
    scanf("%f\n",&Ax);
    if (isdigit(Ax)) {....}
    and whatever I type I receive "Segmentation fault".

    How can I detect that the float input is correct? Is there any built in function?

    Thank you...

  2. #2
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    Check the return value of "scanf()". It returns the number of items successfully read. You can use this to determine if the read was successful, and handle the situation if it was not.

    Also, you don't need the \n in your "scanf()" format string.

  3. #3
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    The isdigit() function only works with single characters, not with numbers.

    How can I detect that the float input is correct?
    This depends on how you're retrieving this information and what you consider to be a "correct" entry. You need to show more content for a more detailed answer.


    Jim

  4. #4
    Registered User
    Join Date
    Oct 2014
    Posts
    8
    Quote Originally Posted by Matticus View Post
    Check the return value of "scanf()". It returns the number of items successfully read. You can use this to determine if the read was successful, and handle the situation if it was not.

    Also, you don't need the \n in your "scanf()" format string.
    I fully understand what you mean but since I am new to C I do not know how to do it (I know in Linux it is echo $? ).

  5. #5
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    Finding information is a valuable skill for programming. Rather than give you the straight solution, I invite you to search for an answer yourself.

    Perhaps a search for something like "return value of scanf" will yield helpful results. Give it a shot, see what you get, and let us know how you make out.

  6. #6
    Registered User
    Join Date
    Oct 2014
    Posts
    8
    the value of scanf gives me the amout of successfull inputs. Therefore if I do this:

    Code:
    int v=scanf ("%f,%f", &A,&B);
    if(v==2) { printf ("ok"); }
    not only I check correct the amount of inputs (must be 2 inputs separated by ",") I also detect from the same if (v==2) the amount of correct float numbers. In other words, if there are two inputs (both or at least one of them is a character) it will never give me 2.
    Do I understand it correctly?

  7. #7
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    Yes, you seem to understand it - well done.

    Whenever you're in doubt, I'd suggest creating a small "test" project where you can implement this stuff in code and test it yourself.

  8. #8
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    Remember an input of "2,34A" should produce a value of 2 for v but there will still be something left in your input buffer, and if the user doesn't enter that separator (comma) your input will also be incorrect. If you want to insure that only numeric entries are entered you should consider using fgets() and then validate that there are only the desired characters before converting the entries to their numeric values.

    Jim

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. User Input Error detection
    By aquilina in forum C Programming
    Replies: 2
    Last Post: 12-03-2012, 12:28 PM
  2. Help on restricting input to a numbers only.
    By Siaw Ys in forum C Programming
    Replies: 7
    Last Post: 11-11-2011, 06:44 AM
  3. Input Numbers
    By bummielove in forum C Programming
    Replies: 2
    Last Post: 07-23-2011, 07:20 PM
  4. No input detection
    By oboy in forum C Programming
    Replies: 3
    Last Post: 02-12-2011, 02:06 PM
  5. count numbers input
    By Unregistered in forum C Programming
    Replies: 5
    Last Post: 05-10-2002, 09:48 AM

Tags for this Thread