Previous Page TOC Next Page



- Project 4 -
Simple Operators


In this lesson, you learned about many of Visual C++'s operators. You saw the following:

Project 4 Listing. An introduction to operators and if testing.




1:// Filename: PROJECT4.CPP



  2:// Computes a tax amount for the tax preparer's client



  3:#include <iostream.h>



  4:



  5:void main()



  6:{



  7:  float earnings;



  8:  float taxOwed = 0.00;   // Stays zero unless changed



  9:                          // in computations



 10:



 11:  cout << "Tax Time!" << endl;   // Title



 12:  cout << "---------" << endl << endl;



 13:



 14:  cout << "How much did the client earn? ";



 15:  cin >> earnings;



 16:



 17:  if ((earnings < 0.0) || (earnings > 999999.99))



 18:    { 



 19:      cout << "I believe that you made a mistake. Try again."



 20:           << endl;



 21:      return;   // Return early to QuickWin



 22:    }



 23:



 24:  if (earnings > 20000.00)



 25:    { 



 26:      taxOwed = (earnings - 20000.00F) * .28F;



 27:      taxOwed += (9999.99F * .20F);   // 20% for $10K to 19.9K



 28:      taxOwed += (4999.99F * .10F);   // 10% for $5K to 9.9K



 29:    }



 30:



 31:  if ((earnings >= 10000.00) && (earnings < 20000.00))



 32:    { 



 33:      taxOwed = (earnings - 10000.00F) * .20F;



 34:      taxOwed += (4999.99F * .10F);   // 10% for $5K to 9.9K



 35:    }



 36:



 37:  if ((earnings >= 4999.99) && (earnings < 10000.00))



 38:    { 



 39:      taxOwed = (earnings - 4999.99F) * .10F; 



 40:    }



 41:  



 42:  cout.precision(2);



 43:  cout.setf(ios::fixed);



 44:  cout.setf(ios::showpoint);



 45:  cout << endl << "The client owes a total of " << taxOwed



 46:       << " in taxes.";



 47:



 48:  return;



 49:}

Output

Description

1: A C++ comment that includes the program's filename.

2: A C++ comment that describes the program.

3: Include the definitions for using cin and cout.

4: Blank lines make the program more readable.

5: All functions have names, and the first function in all C++ programs is main().

6: All functions begin with a left brace.

7: Defines a floating-point variable that will hold the client's earnings.

8: Defines a floating-point variable that will hold the client's computed tax.

9: When continuing a comment on a second line, you need to use the comment symbol again.

10: Extra blank lines make your program more readable.

11: A printed title helps describe the program to the user.

12: Dashes underline the title.

13: Extra blank lines make your program more readable.

14: A prompt for the client's total earnings amount.

15: Gets the earnings from the user and stores them in earnings.

16: Extra blank lines make your program more readable.

17: Checks whether the user entered an earnings value between $0.01 and $999,999.99.

18: Opening brace starts the if processing.

19: Prints an error message.

20: A new line to tidy the output.

21: Returns to QuickWin to finish.

22: The closing brace terminates the body of the if block.

23: Extra blank lines make your program more readable.

24: Checks whether the earnings are more than $20,000.

25: Opening brace starts the if processing

26: Computes a 28% rate on all earnings over $20,000.

27: Computes a 20% rate on earnings from $10,000 to $19,999.99.

28: Computes a 10% rate on earnings from $5,000 to $9,999.99.

29: The closing brace terminates the if's body.

30: Extra blank lines make your program more readable.

31: Checks whether the earnings are between $10,000 and $19,999.99.

32: Opening brace starts the if processing

33: Computes a 20% rate on earnings from $10,000 to $19,999.99.

34: Computes a 10% rate on earnings from $5,000 to $9,999.99.

35: The closing brace terminates the if's body.

36: Extra blank lines make your program more readable.

37: Checks whether the earnings fall between $5,000 and $9,999.00.

38: Opening brace starts the if processing.

39: Computes a tax rate of 10% on all earnings of $5,000.00 or more.

40: The closing brace terminates the if's body.

41: Extra blank lines make your program more readable.

42: Sets the precision for output to 2 decimal places.

43: Sets the output format to fixed format rather than exponent.

44: Makes sure the decimal point is always shown.

45: Prints the amount of tax computed. A zero remains in earnings if all of the if tests were false.

46: Divide very long statements over several lines to make it more readable.

47: Extra blank lines make your program more readable.

48: Return to QuickWin.

49: main()'s closing brace terminates the program.



8: The initial value is zero and remains zero only if the earnings are less than $5,000.

11: Put a title at the top of your screens so that the user knows exactly what the program is about to do.

17: If the earnings don't fall between $0.01 and $999,999.99, the program assumes that there's a problem with the user's input.

31: Both sides of the && must be true before the body of the if executes.

42: Take care to present your program output neatly to the user.


Previous Page Page Top TOC Next Page