Results 1 to 6 of 6

Thread: [C] Scanf and error trapping question

  1. #1

    Thread Starter
    Frenzied Member System_Error's Avatar
    Join Date
    Apr 2004
    Posts
    1,111

    [C] Scanf and error trapping question

    I'm just beginning at C, so I have a few questions on something. I'm trying to get user input and multiply it by tax rates. The scanf function is not working for me. It doesn't read anything into my variable and then it prematurely exits.

    Code:
    #include <stdio.h> //include library stdio
    
    
    //main method
    void main()
    {
         /***********************
          *  Del Mar - 7.25%    *
          *  Encinitas - 7.5%   *
          *  La Jolla - 7.75%   *
          ***********************/
          
         //print out the 'menu' of tax rates
         printf("*** Tax Rates ***\n");
         printf("-----------------\n");
         printf("Del Mar - 7.25%\n");
         printf("Encinitas - 7.5%\n");
         printf("Del Mar - 7.75%\n\n\n");
         
         
         
         
         
         
         
         double new_amount = 0;
         printf("Current amount = $125\n\n");
         printf("Enter an amount: ");
         scanf ("%d", &new_amount);
         
         new_amount += 125;
         double tax_rate = .0725;
         double tax_total = tax_rate*new_amount;
         double total_amount = tax_total+new_amount;
         
         printf("$%d at Encinitas tax rate:\n",new_amount);
         printf("-- Tax:    %f\n", tax_total);
         printf("-- Total:  %f\n", total_amount);
    
    
         getchar();
         
    }

    Why is it always giving me 0 as the new amount no matter what I input?

    Also, is there some way to add some simple error checking, like making sure the user enters a number? If so, could you point me to a tutorial or something?

  2. #2
    PowerPoster sunburnt's Avatar
    Join Date
    Feb 2001
    Location
    Boulder, Colorado
    Posts
    1,403

    Re: [C] Scanf and error trapping question

    %d will not read a floating point number. You should use %f, which stands for float -- specifically, %lf, which means a long float or double.

    scanf returns the number of variables sucessfully read, so if one variable is expected and it is not read correctly, scanf will return 0.

    You can find the specifications for the % format specifiers on the printf() man page, or by searching for printf or scanf on google.
    Every passing hour brings the Solar System forty-three thousand miles closer to Globular Cluster M13 in Hercules -- and still there are some misfits who insist that there is no such thing as progress.

  3. #3

    Thread Starter
    Frenzied Member System_Error's Avatar
    Join Date
    Apr 2004
    Posts
    1,111

    Re: [C] Scanf and error trapping question

    Thanks sunburt. I just went ahead and used fgets and fputs to be on the safe side.

  4. #4
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594

    Re: [C] Scanf and error trapping question

    How is that the "safe side"? You have to do all the conversions yourself, have to watch buffer space ...

    By the way, it's int main in C, too.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  5. #5

    Thread Starter
    Frenzied Member System_Error's Avatar
    Join Date
    Apr 2004
    Posts
    1,111

    Re: [C] Scanf and error trapping question

    I have the following method doing the input:

    Code:
    double get_input()
    {
       //first get the input as a string
       char text[20];
       fputs("Enter an amount: ", stdout);
       fflush(stdout);
       
       //make sure the input is valid
       if ( fgets(text, sizeof text, stdin) )
       {
            //try to convert the input to a double
            int num;
            if (sscanf(text,"%d",&num) == 1 )
            {
               return num;
            }
            //else, there was a parse error
            else
            {
                //print out an error message and exist
                printf("Error -- Press Enter To Exit\n");
                char c;
                scanf("&c",c);
                exit(1);
            }
       }       
    }
    But miracle C has a problem with one of the lines and gives the following error:

    C:\Program Files\Miracle C\TaxRatesPartOne.c: line 15: Parse Error, expecting `'(''
    'if ( fgets(text, sizeof text, (&_files[0])) ) { int num'
    aborting compile

    What exactly is wrong? Everything looks fine to me?

  6. #6
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594

    Re: [C] Scanf and error trapping question

    No reason for the fgets here, just use fscanf (or even scanf, since you're hard-coding stdout anyway) directly as you do with sscanf.

    As for the error, I see two possibilities:
    1) The compiler cannot swallow using sizeof without parentheses. Try sizeof(text).
    2) The compiler cannot swallow the variable definition inside the if. Try placing it at the top of the function. Same for the char in the else-block; however, that is a real error in C89. In C prior to the C99 standard, variable declarations must be at the top of their block.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

Posting Permissions

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



Click Here to Expand Forum to Full Width