(Latest Revision: 10/18/2001)
(10/18/2001: change numbers in tip #3 to make it more transparent)
(10/18/2001: modify code in tip #4 to conform with text style conventions)

Tips on Writing the Tax Program



TIP #1 

As indicated in the structure chart, it is a good design decision to
write four separate functions to compute the tax:  one function for
each tax schedule.  It will work well to use a a switch statement to
determine which function is called to compute the tax.  (See TIP #4
for example code for the switch statement.)

TIP #2

Code like this will get a "warning" from the compiler because a real
number is being assigned to the integer variable tax:

int tax ;
if (txbleIncome > 34548) tax = 1521.08 +  0.093*(txbleIncome-34548) ;

Nevertheless the code above is OK.  It just computes the real number
corresponding to the expression, and then the decimal part is
"thrown away" when the value is assigned to the variable.  The value
that tax gets is incorrect by no more than $0.99.

TIP #3

Code like this will do the job of computing the taxes:

     if ( (txbleIncome >=  0) && (txbleIncome <=  27050) )
     tax =    0.00 + 0.15 * (txbleIncome-0) ;
else if ( (txbleIncome >  27050) && (txbleIncome <=  65550) )
     tax =   4057.50 + 0.28 * (txbleIncome-27050) ;
else if ( (txbleIncome >  65550) && (txbleIncome <= 136750) )
     tax =   14837.50 + 0.31 * (txbleIncome-65550) ;
else /*... (... more of the same kind of code ...) */

The kind of code above comes from direct copying of the
table.  However code like the code below works from the
bottom of a table towards the top:

     if (txbleIncome > 297350) tax = 94725.50 + 0.396 * (txbleIncome-297350) ;
else if (txbleIncome > 136750) tax = 36909.50 + 0.36  * (txbleIncome-136750) ;
else if (txbleIncome >  65550) tax = 14837.50 + 0.31  * (txbleIncome- 65550) ;
else /* ...  (... more of the same kind of code ...) */

This kind of code works too, and it requires fewer terms.  You
can finish writing this code sooner and you are likely to make
fewer errors.

TIP #4

This example code shows how to treat the character
input of 'S', 'H', 'J', or 'M'

char  getStatus()
{ 
   char status ;

      // code here prints directions and prompt for the user.

   cin >> status ;

       // Note that you have to put char data in single quotes
       // so the compiler will not confuse char data with 
       // names for variables.

   if    ( status == 'S' || status == 'H' 
            || status == 'J' || status == 'M')
         return status ;   
   else 
   {
      cout << "BAD STATUS VALUE" << endl ;
      exit (-1) ;
   }
}

    // This function illustrates use of char data as the
    selector // in a case statement.

/* Figure the tax on txbleIncome, using the table for filing
   status "status" */

int computeTax(char status, int txbleIncome)
{ 
  int tax ;
  switch (status) 
  {
    case 'S': tax = singleTax(txbleIncome)     ; break ;
    case 'H': tax = headHouseTax(txbleIncome)  ; break ;
    case 'J': tax = marriedJQWTax(txbleIncome) ; break ;
    case 'M': tax = marriedFSTax(txbleIncome)  ; break ;
    default: cout << "BAD STATUS VALUE" << endl ; tax = 0 ;
  }
  return tax ;
}