Floating point checking problem

This is a discussion on Floating point checking problem within the C Programming forums, part of the General Programming Boards category; Hi, I'm new in C, and I'm stuck in my C project. Here is a Staff Salary checking, I don't ...

  1. #1
    Registered User
    Join Date
    Dec 2003
    Posts
    3

    Floating point checking problem

    Hi, I'm new in C, and I'm stuck in my C project. Here is a Staff Salary checking, I don't know how to check the input value is valid, like entering 'nothing' or characters which is not numbers.

    Code:
    {
    float salary;
    
    printf("Salary : ");
    scanf("%f",&salary);
    
    ... how to check ...
    
    }
    I know how to check value for characters, but really don't know which function can be used for float.

    Please advise, thanks!

  2. #2
    .
    Join Date
    Nov 2003
    Posts
    307
    Most implementations of scanf() will reject the first non-numeric character in the input stream because of the "%f" format specifier. So if you were to enter fg.ab as an answer, scanf will return 0.0000

    One way to test the input stream is to use a char datatype, then check the data. See ctype.h If it's okay, then convert to float.

  3. #3
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    You could always read the FAQ.

    Quzah.
    Hope is the first step on the road to disappointment.

  4. #4
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    Rather than using scanf, you can have greater control over error checking by using fgets to read an entire line and then parsing it with strtod. Here is an example of a validation function for floating-point values:
    Code:
    #include <stdlib.h>
    #include <string.h>
    #include <errno.h>
    
    /*
     * Validate a string for floating-point input, save
     * the value in 'result'.
     *
     * Returns:
     *   0 - Failure, 'result' is either HUGE_VAL or 0
     *   1 - Successful validation, 'result' contains a valid double
     *   2 - Extraneous characters, 'resut' contains a valid double
     */
    int validate_float ( char *line, double *result )
    {
      char   *endp;
      size_t len = strlen ( line );
      
      errno = 0;
      *result = strtod ( line, &endp );
      if ( errno == ERANGE || endp == line )
        return 0; /* Real error */
      if ( endp != line + len && *endp != '\n' )
        return 2; /* Extraneous characters */
      return 1;
    }
    It isn't bulletproof, but good enough for most uses.
    My best code is written with the delete key.

  5. #5
    Registered User
    Join Date
    Dec 2003
    Posts
    3
    Ok, I just grap some idea in the FAQ section. I plan to use isdigit or isalpha, but somehow it always got the same error, saying "cannot convert 'char*' to 'int'.

    Code:
    #include <stdio.h> 
    #include <stdlib.h>
    #include (ctype.h> 
    
    int main(void)
    {
      char wage[10];
      float i;
      
      printf ("Enter your number: ");
      
      if (fgets(wage, 10, stdin) != NULL)
      {
        if (isalpha(wage)) printf("\nits not a number");
        else 
        i = atof(wage);
        printf ("\nEnter okay %f", i);
       }  
      
      return(0);
    }
    How to make it work if using isalpha() ? I only need to know whether the value contains characters.

    Thanks

  6. #6
    Been here, done that.
    Join Date
    May 2003
    Posts
    1,161
    The line
    if (isalpha(wage)) ...
    is in error. isalpha requires a character input, not a pointer to a character. You must test each character in the string in a loop.
    Definition: Politics -- Latin, from
    poly meaning many and
    tics meaning blood sucking parasites
    -- Tom Smothers

  7. #7
    Registered User
    Join Date
    Nov 2003
    Posts
    46
    The following code probably isn't the best way to do what you want but it works ok. The function isalpha takes a single character as it's argument. You were trying to send isalpha an entire string all at once.

    Code:
    #include <stdio.h> 
    #include <stdlib.h>
    #include <string.h> 
    
    #define MAXLINELENGTH 100
    
    int main(void)
    {
      char wage[MAXLINELENGTH];
      float i;
      int j;
      int flag;
        
      printf ("Enter your number: ");
      
      if (fgets(wage, MAXLINELENGTH, stdin) != NULL){
        if (strlen(wage)>10){
            printf("Warning: That was a very long entry for a wage!\n");
        }
        for (j=0; j<MAXLINELENGTH && (flag=isalpha(wage[j]))==0 && wage[j]!='\0'; j++);
        
        if (flag!=0){
           printf("\nits not a number");
        } else {
           i = atof(wage);
           printf ("\nEnter okay %f", i);
        }
      }  
       return(0);
    }
    Last edited by petermichaux; 12-02-2003 at 11:42 PM.

  8. #8
    Registered User
    Join Date
    Dec 2003
    Posts
    3
    oh thanks. that helps a lot. know how to do it now. but I wanna ask, what does '\0' of wage[j]!='\0' means?

  9. #9
    .
    Join Date
    Nov 2003
    Posts
    307
    The last character of a string array is ALWAYS an ASCII zero character. It's how all code finds the end of a string.

    ASCII zero terminated strings are usually called "null-terminated" because the name of the ASCII zero character is nul.

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. Problem aligning floating point numbers
    By esbo in forum C Programming
    Replies: 4
    Last Post: 01-05-2009, 07:09 PM
  3. For the numerical recipes in C types!
    By Smattacus in forum C Programming
    Replies: 5
    Last Post: 10-28-2008, 07:57 PM
  4. Abstract class problem
    By VanJay011379 in forum C++ Programming
    Replies: 9
    Last Post: 07-31-2002, 01:30 PM
  5. Replies: 2
    Last Post: 09-10-2001, 12:00 PM

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