C: How to check if the float is not a char?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Cheer
    New Member
    • Mar 2008
    • 6

    C: How to check if the float is not a char?

    Hello again,

    I would like to ask how could I check if the input is right:
    the program asks user to input a float from a keybord, but what if user inputs a char. Is there any funcion which can check that?

    Thanks in advance.
  • questionit
    Contributor
    • Feb 2007
    • 553

    #2
    Originally posted by Cheer
    Hello again,

    I would like to ask how could I check if the input is right:
    the program asks user to input a float from a keybord, but what if user inputs a char. Is there any funcion which can check that?

    Thanks in advance.
    Hi

    You can use library function ischar() if you are using C to check if the input is a character or a quoted string. ischar returns TRUE if input was char , FALSE otherwise.

    Manual method of doing it is this:
    [code=C]
    while(scanf("%f ", &myFloatVal) == 0)
    printf("Invalid input, enter again");
    [/code]

    On success, the scanf() function returns the number of items succesfully read.
    0 means no successful input (float in this example) is read.

    Regards
    Qi

    Comment

    • dewi44
      New Member
      • Nov 2008
      • 1

      #3
      input float and check if it is a real float

      <code snipped>
      Last edited by Banfa; Nov 7 '08, 06:00 PM. Reason: Removed full code solution

      Comment

      • Banfa
        Recognized Expert Expert
        • Feb 2006
        • 9067

        #4
        dewi44: I have removed you coded not only do we not allow posting of full code solutions and it didn't compile as either C or C++ and it was logically incorrect (after you fix the compilation errors try entering the strings "3.5,5" "3.55").

        Please read the posting guidelines, paying particular attention to Help on posting coursework questions and answers.. Please do not post full code solutions.

        Banfa
        Administrator

        Comment

        Working...