Monthly Sales Tax

the art of the cash register
Creative Commons License photo credit: mindluge

This programming challenge is from an initial C++ programming course I took in Fall 2007. The Chapter (3) was titled “Expressions and Interactivity”.

I post these old academic challenges for a few reasons. One is to demonstrate my programming learning, experience, and progression. Another reason is to make sure my code and solutions are indexed by search engines, so that other beginning programmers may get help if they need it.

3.12: A retail company must file a monthly sales tax report listing the sales for the month and the amount of sales tax collected. Write a program that asks for the month, the year, and the total amount collected at the cash register (that is, sales plus sales tax). Assume the state sales tax is 4 percent and the county sales tax is 2 percent.

If the total amount collected is known and the total sales tax is 6 percent, the amount of product sales may be calculated as: S = T / 1.06, where S is the product sales and T is the total income (product sales plus sales tax).

The program should display a report similar to: (output pictured in book, and faithfully reproduced in my programming output.)

My solution:

/* Chapter 3 Challenge 12
   Written by Kirk Hingsberger
   September 26, 2007
   Monthly Sales Tax
*/

#include <iomanip>
#include <iostream>
using namespace std;

int main()
{
  const float TAXRATE_STATE = .04;
  const float TAXRATE_COUNTY = .02;
  const float TAXRATE_TOTAL = .06;
  char month[16], year[5];
  float totalCollected, sales, taxsumCounty, taxsumState, taxsumTotal;

  cout << setprecision(2) << fixed;

  cout << "Month and year (separated by a space) you are reporting: " << endl;
  cin >> month >> year;

  cout << "What was the total dollar amount collected from register?" << endl;
  cin >> totalCollected;

  sales = totalCollected / 1.06;
  taxsumCounty = sales * TAXRATE_COUNTY;
  taxsumState = sales * TAXRATE_STATE;
  taxsumTotal = sales * TAXRATE_TOTAL;

  cout << left << setw(7) << "Month: " << setw(13) <<  month;
  cout << setw(5) << "Year: " << setw(5) << year << endl;
  cout << "--------------------" << endl;
  cout << left << setw(20) << "Total Collected:";
  cout << "$" << right << setw(9) << totalCollected << endl;
  cout << left << setw(20) << "Sales:";
  cout << "$" << right << setw(9) << sales << endl;
  cout << left << setw(20) << "County Sales Tax:";
  cout << "$" << right << setw(9) << taxsumCounty << endl;
  cout << left << setw(20) << "State Sales Tax:";
  cout << "$" << right << setw(9) << taxsumState << endl;
  cout << left << setw(20) << "Total Sales Tax:";
  cout << "$" << right << setw(9) << taxsumTotal << endl;

  return 0;
}

Here is the capture script I turned in for grading:

Script started on Thu 27 Sep 2007 01:23:52 PM MDT
$ cat chap3chlg12.cc
/* Chapter 3 Challenge 12
   Written by Kirk Hingsberger
   September 26, 2007
   Monthly Sales Tax
*/

#include <iomanip>
#include <iostream>
using namespace std;

int main()
{
  const float TAXRATE_STATE = .04;
  const float TAXRATE_COUNTY = .02;
  const float TAXRATE_TOTAL = .06;
  char month[16], year[5];
  float totalCollected, sales, taxsumCounty, taxsumState, taxsumTotal;

  cout << setprecision(2) << fixed;

  cout << "Month and year (separated by a space) you are reporting: " << endl;
  cin >> month >> year;

  cout << "What was the total dollar amount collected from register?" << endl;
  cin >> totalCollected;

  sales = totalCollected / 1.06;
  taxsumCounty = sales * TAXRATE_COUNTY;
  taxsumState = sales * TAXRATE_STATE;
  taxsumTotal = sales * TAXRATE_TOTAL;

  cout << left << setw(7) << "Month: " << setw(13) <<  month;
  cout << setw(5) << "Year: " << setw(5) << year << endl;
  cout << "--------------------" << endl;
  cout << left << setw(20) << "Total Collected:";
  cout << "$" << right << setw(9) << totalCollected << endl;
  cout << left << setw(20) << "Sales:";
  cout << "$" << right << setw(9) << sales << endl;
  cout << left << setw(20) << "County Sales Tax:";
  cout << "$" << right << setw(9) << taxsumCounty << endl;
  cout << left << setw(20) << "State Sales Tax:";
  cout << "$" << right << setw(9) << taxsumState << endl;
  cout << left << setw(20) << "Total Sales Tax:";
  cout << "$" << right << setw(9) << taxsumTotal << endl;

  return 0;
}
$ g++ -o chap3chlg12 chap3chlg12.cc -s
$ chap3chlg12
Month and year (separated by a space) you are reporting:
October 2009
What was the total dollar amount collected from register?
10000
Month: October      Year: 2009
--------------------
Total Collected:    $ 10000.00
Sales:              $  9433.96
County Sales Tax:   $   188.68
State Sales Tax:    $   377.36
Total Sales Tax:    $   566.04
$ exit
exit

Script done on Thu 27 Sep 2007 01:24:40 PM MDT
  • Share/Bookmark




Leave a Reply