Is there a way to validate floating value?

This is a discussion on Is there a way to validate floating value? within the C Programming forums, part of the General Programming Boards category; Is there anyway to validate floating value? For example, i have a code of Code: float a; printf("Please key in ...

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

    Is there a way to validate floating value?

    Is there anyway to validate floating value? For example, i have a code of

    Code:
    float a;
    printf("Please key in a floating value : ");
    scanf("%f",&a);
    But what if user key in 'A' or "ABC" to it, it will show errors. Anyway to validate that?

  2. #2
    Registered User
    Join Date
    Oct 2009
    Location
    While(1)
    Posts
    377
    Quote Originally Posted by kekewong View Post
    Is there anyway to validate floating value? For example, i have a code of

    Code:
    float a;
    printf("Please key in a floating value : ");
    scanf("%f",&a);
    But what if user key in 'A' or "ABC" to it, it will show errors. Anyway to validate that?
    Did u compiled and checked this code out with gcc the compiler is very well intelligent it will take care these kind of issues.

  3. #3
    Registered User
    Join Date
    Apr 2009
    Posts
    8
    hmm ok, just try something already and it works. Thanks n_n

  4. #4
    The larch
    Join Date
    May 2006
    Posts
    3,573
    I don't think the compiler takes care of anything. You can test to see whether the scanf call succeeded by checking its return value (it returns the number of items that were successfully matched and assigned).
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  5. #5
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,230
    Quote Originally Posted by RockyMarrone View Post
    Did u compiled and checked this code out with gcc the compiler is very well intelligent it will take care these kind of issues.
    What are you talking about? Huh?
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  6. #6
    Registered User
    Join Date
    Sep 2009
    Location
    USA
    Posts
    63
    Quote Originally Posted by kekewong View Post
    Is there anyway to validate floating value? For example, i have a code of

    Code:
    float a;
    printf("Please key in a floating value : ");
    scanf("%f",&a);
    But what if user key in 'A' or "ABC" to it, it will show errors. Anyway to validate that?
    hmm for 'A' for this u can use getchar ...and u can try validating thru that

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. floating point number comparison
    By lehe in forum C++ Programming
    Replies: 10
    Last Post: 05-18-2009, 07:11 PM
  2. Testing Floating Point Return Values
    By jason_m in forum C Programming
    Replies: 5
    Last Post: 08-15-2008, 01:37 PM
  3. checking for floating point number
    By ssharish2005 in forum C Programming
    Replies: 6
    Last Post: 10-18-2005, 08:14 PM
  4. floating point operators
    By DavidP in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 10-22-2003, 07:53 PM
  5. Validate Float Logic?
    By Bajanine in forum Windows Programming
    Replies: 2
    Last Post: 11-14-2002, 09:00 PM

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