Download this source file


/*
 * Tax.c - A program to compute taxes: 15% of the first 10,000 and 50% of the rest.
 * This program illustrates the correct use of scanf return values for error checking,
 * if-statements, and the use of fprintf to send output (prompts and error messages)
 * to the screen even when the user uses I/O redirection.
 *
 * Written 2001 by Wayne Pollock, Tampa Florida USA.
 */

#include <stdio.h>

int main ( void )
{
   float income, tax;

   for ( ;; )       /* repeat forever */
   {
      fprintf( stderr, "\nEnter amount of net income (zero to quit): " );
      if ( scanf( "%f", &income ) != 1 )
      {
         fprintf( stderr, "***ERROR: problem with scanf!\a\n" );
         return -1;  /* exit the program, return error status to OS */
      }
      if ( income == 0.0 )
         break;     /* all done, exit the loop */

      if ( income < 0.0 )
      {
         fprintf(stderr, "***ERROR: Must enter a POSITIVE number!\a\n");
         continue;   /* start the loop over again */
      }

      printf( "Income = %8.2f grickles,\n", income );

      if ( income <= 10000.00 )
         tax = 0.15 * income;
      else
         tax = 0.15 * 10000.00 + 0.5 * ( income - 10000.00 );

      printf( "Tax    = %8.2f grickles.\n", tax );

      if ( tax >= 500.00 )
         printf("\t...And that's a lot of grickles!\n");
   }
   return 0;
}




Send comments and mail to the WebMaster.