468,497 Members | 1,887 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,497 developers. It's quick & easy.

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

6
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.
Apr 6 '08 #1
3 7364
questionit
553 512MB
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:
Expand|Select|Wrap|Line Numbers
  1.   while(scanf("%f", &myFloatVal) == 0) 
  2.    printf("Invalid input, enter again");
  3.  
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
Apr 6 '08 #2
dewi44
1
<code snipped>
Nov 7 '08 #3
Banfa
9,055 Expert Mod 8TB
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
Nov 7 '08 #4

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

4 posts views Thread by JC | last post: by
18 posts views Thread by muser | last post: by
6 posts views Thread by Andrej Prsa | last post: by
6 posts views Thread by Peter Dunker | last post: by
9 posts views Thread by Gregory.A.Book | last post: by
3 posts views Thread by begum | last post: by
5 posts views Thread by Travis | last post: by
3 posts views Thread by gieforce | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.