For this program, you will need to write 4 functions, as follows: calculateBill
ID: 3732523 • Letter: F
Question
For this program, you will need to write 4 functions, as follows:
calculateBill
Takes in a double reference parameter for the price of the food.
Takes in a double value parameter for the chosen tip percent
calls the addTax function to add taxes to the price.
calls the calculateTip function to add the tip to the price.
calls the formatPrice function to print out the price
returns nothing
addTax
Takes in a double reference parameter for the price of the food.
Updates the price by adding on the tax, assuming the tax rate is 9.25% in Santa Clara County
returns nothing
calculateTip
Takes in a double reference parameter for the current bill (price + tax).
Takes in a second double - this time a value parameter - for the percent tip.
Calculates the amount of the tip and updates the current price by adding on the tip amount
returns nothing
formatPrice
Takes in a double value parameter for the price.
prints the price in the form $X.XX
(Hint: How do you get the price to have only 2 decimal values?)
Also prints a message about the final price: "With tax and tip, your total comes to... $"
returns nothing
Additional Specifications
You must include function prototypes at the top of your program.
Each prototype must also have a comment in the style describe.
You should write the full function definitions below main.
Important: You should call the calculateBill function inside of main, not the other three functions.
The other 3 functions should be called inside of the calculateBill function.
The code is below:
void calculateBill(double& price, double percent);
//calls addTax, calculateTip and displayPrice
//to calculate and display the final price
void addTax(double& price);
//calculates the tax at 9.25% and adds it to the price of the meal
......
int main()
{
cout << "Welcome to the Restaurant Calculator ";
cout << " Enter the price of your meal: ";
double price;
cin >> price;
double percent;
cout << "Enter the percent tip (10-25): ";
cin >> percent;
calculateBill(price, percent);
cout << "Goodbye!";
}
void addTax(double& price)
{
price *= 1.0925;
}
void calculateBill(double& price, double percent)
{
addTax(price);
//add two additional function calls here
}
Explanation / Answer
#include <iostream>
#include <iomanip> //for outputing precission
using namespace std;
// functions prototypes
void addTax(double& price);
void calculateBill(double& price, double percent);
void formatPrice(double price);
void calculateTip(double& price, double percent);
int main()
{
cout << "Welcome to the Restaurant Calculator ";
cout << " Enter the price of your meal: ";
double price;
cin >> price;
double percent;
cout << "Enter the percent tip (10-25): ";
//checking if tip is (10-15)
do{
cin >> percent;
if (percent<10 && percent>25 )
cout<<"Please Enter tip in range (10-25) only: ";
}while((percent<10 && percent>25 ));
calculateBill(price, percent);
cout << "Goodbye!"<<endl;
return 0;
}
// to add tax to the current price
void addTax(double& price)
{
price *= 1.0925;
}
//to calculate total bill
void calculateBill(double& price, double percent)
{
addTax(price);
calculateTip(price, percent);
formatPrice(price);
}
// for formatting print of bill
void formatPrice(double price)
{
cout<<"With tax and tip, your total bill comes out $"<<std::fixed;
cout<<setprecision(2)<<price<<endl;
}
// calculate the tip and adding it to current price
void calculateTip(double& price, double percent)
{
price += (percent/100) * price ;
}
OUTPUT:
dps@machine:~/Documents/Chegg$ g++ calculate.cc
dps@machine:~/Documents/Chegg$ g++ calculate.cc -o calculate
dps@machine:~/Documents/Chegg$ ./calculate
Welcome to the Restaurant Calculator
Enter the price of your meal: 100
Enter the percent tip (10-25): 15
With tax and tip, your total bill comes out $125.64
Goodbye!
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.