Total Purchase

This programming challenge is from an initial C++ programming course I took in Fall 2007. The Chapter (2) was titled “Introduction to C++”.

2.8: A customer in a store is purchasing five items. The prices of the five items are:

  • Price of item 1 = $12.95
  • Price of item 2 = $24.95
  • Price of item 3 = $6.95
  • Price of item 4 = $14.95
  • Price of item 5 = $3.95

Write a program that holds the prices of the five items in five variables. Display each item’s price, the subtotal of the sale, the amount of sales tax, and the total. Assume the sales tax is 6%.

My solution (using hard-coded values as per instructions):

/* Chapter 2 Challenge 8
Written by Kirk Hingsberger
September 13, 2007
Total Purchase Challenge
*/

#include
using namespace std;

int main()
{
float item1 = 12.95;
float item2 = 24.95;
float item3 = 6.95;
float item4 = 14.95;
float item5 = 3.95;
float tax = .06;
float saleSub = item1 + item2 + item3 + item4 + item5;
float saleTax = saleSub * tax;
float saleTotal = saleSub + saleTax;

cout << "Purchased Items:" << endl;
cout << "Item 1: $" << item1 << endl;
cout << "Item 2: $" << item2 << endl;
cout << "Item 3: $" << item3 << endl;
cout << "Item 4: $" << item4 << endl;
cout << "Item 5: $" << item5 << endl;
cout << "\nSale Subtotal: $" << saleSub << endl;
cout << "Sales Tax: $" << saleTax << " on current " << tax << " tax rate" << endl;
cout << "Total Sale Amount: $" << saleTotal << endl;
cout << "\n Thank you for shopping, come again!" << endl;

return 0;
}

As with many other beginning C++ programs, we were instructed to not sweat the fact that our figures computed in scientific notation or with many decimal places. We would learn those controls later.

Here is the capture script I turned in for grading:

Script started on Thu 13 Sep 2007 10:16:42 AM MDT
$ cat chap2chlg8.cc
/* Chapter 2 Challenge 8
Written by Kirk Hingsberger
September 13, 2007
Total Purchase Challenge
*/

#include
using namespace std;

int main()
{
float item1 = 12.95;
float item2 = 24.95;
float item3 = 6.95;
float item4 = 14.95;
float item5 = 3.95;
float tax = .06;
float saleSub = item1 + item2 + item3 + item4 + item5;
float saleTax = saleSub * tax;
float saleTotal = saleSub + saleTax;

cout << “Purchased Items:” << endl;
cout << “Item 1: $” << item1 << endl;
cout << “Item 2: $” << item2 << endl;
cout << “Item 3: $” << item3 << endl;
cout << “Item 4: $” << item4 << endl;
cout << “Item 5: $” << item5 << endl;
cout << “\nSale Subtotal: $” << saleSub << endl;
cout << “Sales Tax: $” << saleTax << ” on current ” << tax << ” tax rate” << endl;
cout << “Total Sale Amount: $” << saleTotal << endl;
cout << “\n Thank you for shopping, come again!” << endl;

return 0;
}
$ g++ -o chap2chlg8 chap2chlg8.cc -s
$ chap2chlg8
Purchased Items:
Item 1: $12.95
Item 2: $24.95
Item 3: $6.95
Item 4: $14.95
Item 5: $3.95

Sale Subtotal: $63.75
Sales Tax: $3.825 on current 0.06 tax rate
Total Sale Amount: $67.575

Thank you for shopping, come again!
$ exit
exit

Script done on Thu 13 Sep 2007 10:17:10 AM MDT

  • Share/Bookmark




Leave a Reply