+ Reply to Thread
Results 1 to 3 of 3

Thread: Read a Floating-Point Value from the User

  1. #1
    dcs
    dcs is offline Guru
    Join Date
    Mar 2008
    Posts
    775
    Rep Power
    23

    Read a Floating-Point Value from the User

    Read a Floating-Point Value from the User, Part 1

    Obtaining user input can be done in many surprisingly different ways. This code is somewhere in the middle: safer than scanf("%lf", &n), but not bulletproof. It is meant to be a simple but relatively safe demonstration. Note also that there would be slight differences for using float instead of double.

    The function mygetd reads user input from the stdin into a string using fgets. Then it attempts to convert this string to a double using sscanf. If both functions succeed, a 1 is returned; if either fails, a 0 is returned.

    Code:
    #include <stdio.h>
    
    int mygetd(double *result)
    {
       char buff [ 32 ]; /* modify array size to suit your needs */
       return fgets(buff, sizeof buff, stdin) && sscanf(buff, "%lf", result) == 1;
    }
    
    int main(void)
    {
       double value;
       do {
          fputs("Enter a floating-point number: ", stdout);
          fflush(stdout);
       } while ( !mygetd(&value) );
       printf("value = %g\n", value);
       return 0;
    }
    
    /* my output
    Enter a floating-point number: one
    Enter a floating-point number: 
    Enter a floating-point number: f12.3
    Enter a floating-point number: -45.67
    value = -45.67
    
    Enter a floating-point number: -12.3f
    value = -12.3
    
    Enter a floating-point number:    125 help
    value = 125
    */
    Leading whitespace, and other trailing characters are some things not handled. Those issues are handled in Read a Floating-Point Value from the User, Part 2 and Read a Floating-Point Value from the User, Part 3.

  2. CODECALL Circuit advertisement
    Join Date
    Always
    Posts
    Many

     
  3. #2
    dcs
    dcs is offline Guru
    Join Date
    Mar 2008
    Posts
    775
    Rep Power
    23

    Re: CodeCall Contest #6

    Read a Floating-Point Value from the User, Part 2

    Some issues, such as leading whitespace and trailing characters that cannot be part of a number, were not handled in Read a Floating-Point Value from the User, Part 1. Here such issues receive lip service. See also Read a Floating-Point Value from the User, Part 3.

    Code:
    #include <stdio.h>
    #include <ctype.h>
    
    int mygetd(double *result)
    {
       char c, buff [ 32 ];
       return fgets(buff, sizeof buff, stdin) && !isspace(*buff) &&
       sscanf(buff, "%lf%c", result, &c) == 2 && (c == '\n' || c == '\0');
    }
    
    int main(void)
    {
       double value;
       do
       {
          fputs("Enter a floating-point number: ", stdout);
          fflush(stdout);
       } while ( !mygetd(&value) );
       printf("value = %g\n", value);
       return 0;
    }
    
    /* my output
    Enter a floating-point number: one
    Enter a floating-point number:
    Enter a floating-point number: f12.3
    Enter a floating-point number: -45.67
    value = -45.67
    
    Enter a floating-point number: -12.3f
    Enter a floating-point number:    125 help
    Enter a floating-point number: 1.2.3
    Enter a floating-point number: 1.23
    value = 1.23
    */

  4. #3
    dcs
    dcs is offline Guru
    Join Date
    Mar 2008
    Posts
    775
    Rep Power
    23

    Re: CodeCall Contest #6

    Read a Floating-Point Value from the User, Part 3

    This snippet uses strtod to be a little more stringent.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <ctype.h>
    
    int mygetd(double *result)
    {
       char *end, buff [ 32 ];
       return fgets(buff, sizeof buff, stdin) && !isspace(*buff) &&
              (*result = strtod(buff, &end)), ( *end == '\n' || *end == '\0' );
    }
    
    int main(void)
    {
       double value = -12.3;
       do
       {
          fputs("Enter a floating-point number: ", stdout);
          fflush(stdout);
       } while ( !mygetd(&value) );
       printf("value = %g\n", value);
       return 0;
    }
    
    /* my output
    Enter a floating-point number: one
    Enter a floating-point number:
    Enter a floating-point number: f12.3
    Enter a floating-point number: -45.67
    value = -45.67
     
    Enter a floating-point number: -12.3f
    Enter a floating-point number:    125 help
    Enter a floating-point number: 1.2.3
    Enter a floating-point number: 1.23
    value = 1.23
    */

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Floating point
    By ljl22 in forum C and C++
    Replies: 3
    Last Post: 03-22-2011, 07:43 AM
  2. Floating Point Precision
    By EebamXela in forum General Programming
    Replies: 2
    Last Post: 02-16-2011, 05:09 PM
  3. Floating point ...~
    By R3.RyozKidz in forum Java Help
    Replies: 3
    Last Post: 01-29-2010, 06:02 AM
  4. In need of floating point help
    By Ansems_keyblade in forum General Programming
    Replies: 3
    Last Post: 12-02-2009, 10:02 AM
  5. floating point variables in c/c++
    By avibiter in forum C and C++
    Replies: 1
    Last Post: 05-22-2009, 11:48 AM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts