My teacher made this statement regarding the attached code "In the tip program, move common statements outside of the if/else structure and instead use it to perform only the required calculations. No sense in pre-computing the 10% tip if it will end up being 20%." I don't understand "common statements" or the " No sense in pre-computing the 10% tip" statement. code runs fine I got a 95 but I have to do it again using while input validation
//Albert Grennan:
// Assignment 4.3
/*
Create a program that prompts the user for the total cost of a restaurant meal. It will then ask
them if the service was good. If the user responds with a positive response (this could be "yes", "y",
or any variation on that; you must test for at least 3 possible positive responses) then your program
will compute how much a 20% tip would be and then output that, along with the new total, including
tip and tax. Otherwise, the calculation will be based on a 10% tip. Please note that tip calculations are
based on the cost before tax. Assume the tax is 6.25%. Please be sure that the output is
formatted so that it always shows 2 decimal places.
*/
#include<iostream>
#include<iomanip>
usingnamespace std;
int main()
{
// variables
double mealCost;
constdouble TAX = .0625;
double goodTip;
double badTip;
double goodTotal;
double badTotal;
double mealTax;
char response;
//prompts the user for the total cost of a restaurant meal.
cout << " How much did your meal cost? ";
cin >> mealCost;
// ask them if the service was good.
cout << " Was the service good?\n ";
// test for at least 3 possible positive responses
cout << " Please enter your response using a Y, y or yes" << endl;
cin >> response;
mealTax = mealCost * TAX;
goodTip = mealCost * .20;
badTip = mealCost * .10;
goodTotal = mealCost + mealTax + goodTip;
badTotal = mealCost + mealTax + badTip; //
cout << setprecision(2) << fixed;
// if its a positive response, then your program will compute a 20 % tip
if (response == 'Y' || response == 'y' || response == 'yes')
{
//output that, along with the new total, including tip and tax.
cout << " The tip will be $" << goodTip << endl;
cout << " The total bill will be $" << goodTotal << endl << endl;
}
else
{
// Otherwise, the calculation will be based on a 10% tip.
cout << " So sorry to hear that. We'll talk to the waitstaff \n";
cout << " The tip will only be 10%. It comes to $" << badTip << endl;
cout << " The total bill will be $" << badTotal << endl << endl;
}
system("pause");
return 0;
}
int main()
{
// variables
double mealCost;
constdouble TAX = .0625;
double mealTax;
char response;
//prompts the user for the total cost of a restaurant meal.
cout << " How much did your meal cost? ";
cin >> mealCost;
// ask them if the service was good.
cout << " Was the service good?\n ";
// test for at least 3 possible positive responses
cout << " Please enter your response using a Y, y or yes" << endl;
cin >> response;
cout << setprecision(2);
int tip, total;
// if its a positive response, then your program will compute a 20 % tip
if (response == 'Y' || response == 'y' || response == 'yes')
{
tip = mealCost * .20;
total = mealCost + mealTax + tip;
}
else
{
cout << " So sorry to hear that. We'll talk to the waitstaff \n";
tip = mealCost * .10;
total = mealCost + mealTax + tip;
}
cout << " The tip will be $" << goodTip << endl;
cout << " The total bill will be $" << goodTotal << endl << endl;
system("pause");
return 0;
}
The thing I like to do when defining these constant type variables it to put them at the very beginning. This could mean the function or near the top of the file after the "#include" statements. This way they are easy to find when a change is need and you do not have to look through your whole program to find where to change the magic numbers.