How would you check this?

This is a discussion on How would you check this? within the C Programming forums, part of the General Programming Boards category; I have a float that must be entered from the keyboard and I want to check to make sure it ...

  1. #1
    Registered User
    Join Date
    Aug 2008
    Posts
    55

    How would you check this?

    I have a float that must be entered from the keyboard and I want to check to make sure it is a number and not something else. How would you do that? Here is my function so far:
    Code:
    float getfloat(char str[], float min){
    	float num ;
    	
    	printf("Please enter the %s of raffel tickets.\n", str);
    	scanf("%f%*c", &num);
            
           while(num < min){
    		printf("Please enter a number greater than %.2f\n", min);
    		scanf("%f%*c", &num);
    	}
    	
    	return num;
    }
    Thanks for any help.

  2. #2
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    You could do a for loop checking the ASCII value of each character.
    Last edited by MK27; 10-09-2008 at 09:05 PM.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  3. #3
    Registered User
    Join Date
    Aug 2008
    Posts
    55
    How would you check the ascii value of a number instead of the numerical value?

  4. #4
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    This is the ASCII table:

    http://www.asciitable.com/

    Notice that the decimal value of the ten digits (0-9) is >47 and <58. If your participant entered a legit float like 3.41, the only thing besides a digit that's valid would be ASCII 46 (.)
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  5. #5
    Registered User
    Join Date
    Aug 2008
    Posts
    55
    Thanks. I'm just not quite sure how to get the ascii number of the string of characters the user inputs. Like after I get input into a float variable, how can I check it from there? If a non-numeric character gets scanf()ed into a float, does the float just put it back into the buffer?

  6. #6
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    If you read input into a float variable, you're toast as far as trying to check later if it was valid (scanf will just stop at illegal input).

    If you store it as characters, then by definition you'll have the character values.

  7. #7
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,831
    You might want to check the returned value of scanf(). It will tell you whether the input was a number. And clean up the format string to be just "&#37;f".

    Also, you're checking if the user entered something less than the minimum. Yet the error says it was erroneously entered exceeding it. What if it's equal to the minimum? Change either the condition, or the message.

  8. #8
    Registered User
    Join Date
    Aug 2008
    Posts
    55
    so if I scanf() some input into a char array with %s, it will be stored sequentially by character with a terminating null. I can then check the ascii values. If it turns out to be an number, how can I the number back out of the array as a float? also if I scanf() a string into a char array that already holds values, what happens? Do the values get over-written starting from the first index of the array?

  9. #9
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    1. strtod/strtof
    2. Yes

  10. #10
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,831
    I think checking for ASCII validity yourself is not a good idea unless you are prepared to be able to scan over leading '+', '-', a decimal point (and only ONE), followed by scientific notation if any. That's a lot of logic.

    How about
    Code:
    float ff;
    char crap;
    ...
    if (2 > scanf("&#37;f%c", &num, &crap) || '\n' != crap) {
        printf("You didn't enter a number.\n");
        ....
        }
    The crap variable only captures one trailing character. In case the user types in garbage after a legitimate number. I just found out you get at least a newline. So that's an acceptable trailing character, but no other.
    Last edited by nonoob; 10-10-2008 at 09:51 PM.

  11. #11
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,169
    Avoid scanf for reading strings, preferably. Better to use fgets instead.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.
    For information on how to enable C++11 on your compiler, look here.
    よく聞くがいい!私は天才だからね! ^_^

  12. #12
    Registered User
    Join Date
    Aug 2008
    Posts
    55
    Here's what I ended up coming up with if anyone is interested.

    Code:
    printf("Enter a number: ");
    while(scanf("%f", &num) != 1 || isalpha(getchar())) {
       printf("Just a number, no characters please.");
       while(getchar() != '\n');
    }
    It was for an exam program, but unfortunately I came up with it after it was due(actually we weren't instructed to validate but I wanted to do it anyway). It was more important that I understand how to do it anyway.

  13. #13
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,335
    > Here's what I ended up coming up with if anyone is interested.
    It would still go into an infinite loop if you pressed ctrl-z (dos/win) or crtl-d (unix/linux)
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  14. #14
    Registered User
    Join Date
    Aug 2008
    Posts
    55
    Quote Originally Posted by Salem View Post
    > Here's what I ended up coming up with if anyone is interested.
    It would still go into an infinite loop if you pressed ctrl-z (dos/win) or crtl-d (unix/linux)
    Good point. I didn't consider that. How would one check for those?

  15. #15
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    scanf returns EOF in those cases (since that's what it is).

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. BN_CLICKED, change button style
    By bennyandthejets in forum Windows Programming
    Replies: 13
    Last Post: 07-05-2010, 11:42 PM
  2. how to check input is decimal or not?
    By kalamram in forum C Programming
    Replies: 3
    Last Post: 08-31-2007, 07:07 PM
  3. Please check this loop
    By Daesom in forum C++ Programming
    Replies: 13
    Last Post: 11-02-2006, 12:52 AM
  4. A way to check for Win98 or WinXP
    By Shadow in forum A Brief History of Cprogramming.com
    Replies: 5
    Last Post: 10-31-2002, 10:06 AM
  5. how to check for end of line in a text file
    By anooj123 in forum C++ Programming
    Replies: 6
    Last Post: 10-24-2002, 11:21 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21