Restaurant Bill Challenge
This programming challenge is from an initial C++ programming course I took in Fall 2007. The Chapter (2) was titled “Introduction to C++”.
2.4: Write a program that computes the tax and tip on a restaurant bill for a patron with a $44.50 meal charge. The tax should be 6.75 percent of the meal cost. The tip should be 15 percent of the total after adding the tax. Display the meal cost, tax amount, tip amount, and total bill on the screen.
My solution (using hard-coded values as per instructions):
/* Chapter 2 Challenge 4
Written by Kirk Hingsberger
Sept 12, 2007
Restaurant Bill Challenge
*/
#include
using namespace std;
int main()
{
float mealCost = 44.50;
float tax = .0675;
float mealTax = mealCost * tax;
float mealwTax = mealTax + mealCost;
float tip = mealwTax * .15;
float totalBill = tip + mealwTax;
cout << "Given a $" << mealCost << " meal cost," << endl;
cout << "the tax amount is $" << mealTax << "," << endl;
cout << "the tip amount is $" << tip << "," << endl;
cout << "and the total bill is $" << totalBill << "." << 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:11:36 AM MDT
$ cat chap2chlg4.cc
/* Chapter 2 Challenge 4
Written by Kirk Hingsberger
Sept 12, 2007
Restaurant Bill Challenge
*/
#include
using namespace std;
int main()
{
float mealCost = 44.50;
float tax = .0675;
float mealTax = mealCost * tax;
float mealwTax = mealTax + mealCost;
float tip = mealwTax * .15;
float totalBill = tip + mealwTax;
cout << “Given a $” << mealCost << ” meal cost,” << endl;
cout << “the tax amount is $” << mealTax << “,” << endl;
cout << “the tip amount is $” << tip << “,” << endl;
cout << “and the total bill is $” << totalBill << “.” << endl;
return 0;
}
$ g++ chap2chlg4 chap -o chap2chlg4.cc -s
$ chap2chlg4
Given a $44.5 meal cost,
the tax amount is $3.00375,
the tip amount is $7.12556,
and the total bill is $54.6293.
$ exit
exit
Script done on Thu 13 Sep 2007 10:12:29 AM MDT










