Newbie question about function to get float 
Author Message
 Newbie question about function to get float

Hi!

I'm a complete newbie to C programming.  It is very exciting!  I've
put together a small function to get a float from the user.  It seems
to work fine on my Linux system with gcc 2.95.3, but I'm wondering
about the style and possibly portability.  If it wouldn't be too much
trouble for the more experienced programmers to comment, I'd
appreciate it.  Here it is:

# include <stdio.h>
# include <stdlib.h>
# include <string.h>

int main (void)

  {

    char *ptr1, String[50], Letters[85] =
;:/?>,<";
    float FloatNumber;
    int x = 0, Length;

    while (x == 0)
      {
        printf("Enter your number : ");
        fgets(String, 50, stdin);    /*Check to see if the user*/
        Length = strlen(String) - 1; /*input nothing*/

        if (Length == 0)
          {
            printf("No input.  Try again\n");
            x = 0;
          }
        else if (Length == 1)
          {
            printf("One digit input can't be a float.  Try again.\n");
            x = 0;
          }
        else if (strpbrk(String,Letters) != NULL) /*Check String*/
          {                                     /*for characters above*/
            printf("Input contains non-numeric characters.  Try Again.\n");
            x = 0;
          }
        else
          x = 1;
      }

    FloatNumber = strtod(String, &ptr1);

    printf("Your result is %3.3f\n", FloatNumber);

    return 0;

  }

Regards,

Allen



Mon, 11 Oct 2004 01:26:34 GMT  
 Newbie question about function to get float

Quote:

> I'm a complete newbie to C programming.  It is very exciting!  I've
> put together a small function to get a float from the user.  It seems
> to work fine on my Linux system with gcc 2.95.3, but I'm wondering
> about the style and possibly portability.  If it wouldn't be too much
> trouble for the more experienced programmers to comment, I'd
> appreciate it.  Here it is:
> # include <stdio.h>
> # include <stdlib.h>
> # include <string.h>
> int main (void)
>   {
>     char *ptr1, String[50], Letters[85] =
;:/?>,<";
>     float FloatNumber;
>     int x = 0, Length;
>     while (x == 0)
>       {
>         printf("Enter your number : ");
>    fgets(String, 50, stdin);    /*Check to see if the user*/
>    Length = strlen(String) - 1; /*input nothing*/
>         if (Length == 0)
>           {
>             printf("No input.  Try again\n");
>             x = 0;
>           }
>    else if (Length == 1)
>      {
>        printf("One digit input can't be a float.  Try again.\n");
>        x = 0;
>      }
>    else if (strpbrk(String,Letters) != NULL) /*Check String*/
>      {                                     /*for characters above*/
>        printf("Input contains non-numeric characters.  Try Again.\n");
>        x = 0;
>      }
>    else
>      x = 1;
>       }
>     FloatNumber = strtod(String, &ptr1);
>     printf("Your result is %3.3f\n", FloatNumber);
>     return 0;
>   }

Your first problem is that Letters is neither sound nor complete: it contains

implementation's character set; and, likewise, there are characters in the
implementation's character set which aren't in Letters.  This is a serious
portability problem.

This is not really a good way to do it anyway; strtod() performs its own
error-detection, so you end up scanning the string twice.

Another problem is that you immediately subtract one from the length of
String.  This gives you erroneous results: an empty string will have Length
-1 (which you don't catch).  There are other problems associated with the
Length: a string length 1 (Length 0) is valid; a string of length 2 (Length
1) is valid; but you dismiss them both.

Also, there are other miscellaneous problems: if stdout is line-buffered,
your prompt likely won't be displayed before the input is read; using a
variable (x) to trigger exit from the loop is IMHO less elegant than a simple
break; floats should almost never be used (use doubles unless you are
creating huge arrays and need to save memory).

If you want to do the error-detection yourself, you might then try rewriting
it as so, with the aformentioned problems solved:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>

int
main(void)
{
        double FloatNumber;
        char String[50], char *end_str;

        goto read_number;
        do {
                fprintf(stderr, "Bad input.  Try again\n");

        read_number :
                printf("Enter your number: ");
                fflush(stdout);

                if (! fgets(String, sizeof String, stdin)) {
                        /* either an EOF or error: deal with it */
                }

                /* chop the newline off the end of the String */
                size_t Length = strlen(String);
                if (String[Length - 1] == '\n')
                        String[--Length] = 0;

                errno = 0;
                FloatNumber = strtod(String, end_str);
        /*
         * if *end_str is not the null character, then it doesn't point to
         * the end of the string, and thus there was some bad input was given
         *
         * if FloatNumber == HUGE_VAL and errno is set, the number overflowed
         * (is too large to fit into a double)
         *
         */
        } while (*end_str || FloatNumber == HUGE_VAL && errno);

        printf("Your result is %3.3f\n", FloatNumber);

        return 0;

Quote:
}

Something to keep in mind is that functions in the standard library almost
always return to you useful information and error conditions.  e.g. in this
example I've checked the error conditions returned to me by fgets() and
strtod(), which helped me out a lot.

--
 /"\                                                 m i k e   b u r r e l l

  X        AGAINST HTML MAIL,
 / \      AND NEWS TOO, dammit



Mon, 11 Oct 2004 04:34:10 GMT  
 Newbie question about function to get float
Allen rambled on saying:

Quote:
> Hi!

> I'm a complete newbie to C programming.  It is very exciting!  I've
> put together a small function to get a float from the user.  It seems
> to work fine on my Linux system with gcc 2.95.3, but I'm wondering
> about the style and possibly portability.  If it wouldn't be too much
> trouble for the more experienced programmers to comment, I'd
> appreciate it.  Here it is:

Everyone has thir own style of programming but there are a few accepted
'rules' as it were. See comments below.

Quote:

> # include <stdio.h>
> # include <stdlib.h>
> # include <string.h>

> int main (void)

>   {

>     char *ptr1, String[50], Letters[85] =
> "\\aAbBcCdDeEfFgGhHiIjJkKlLmMnNoOpPq\


As mike burrell has posted there are problems with this string.

You could just try...
int x=1;

while ( x )
{

                printf("Enter your number : ");
                fgets(String, 50, stdin);    

        if ((strlen(String) < 4) || (strpbrk(String,Letters) != NULL) )
        {
                printf("This can't be a float. Try Again.\n");

        }
        else
        {
                x=0;
                printf("Your result is %3.3f\n", atof(String);
        }

Quote:
}

...as your while loop.

Of course you may want to put these in a switch instead, that would
possibly be more elegent and remove the need for the x variable. All a
matter of personal style. Do try to avoid goto where possible though,
unless you are really careful it can lead to trouble and it is always hard
to read.

--
[root]# rm -rf /*
You know it makes sense!



Mon, 11 Oct 2004 05:39:04 GMT  
 
 [ 3 post ] 

 Relevant Pages 

1. use of float, newbie question

2. newbie question - gcc under linux, getting started

3. Standard method for getting string input ?(newbie question)

4. newbie question re: getting started

5. Newbie Question -- getting DOS window to stay

6. Horrible Newbie Question - Getting Numbers from Edit Controls

7. newbie.question - getting compile size down

8. Newbie Question - getting my ListBox to work

9. Newbie question: Getting a key pressed in a dialog-based application

10. Newbie question: Getting a key pressed in a dialog-based application

11. Newbie Question; Getting info from closed Dialog

12. Question on function returning array of floats

 

 
Powered by phpBB® Forum Software