//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()
{
double mealCost;
constdouble TAX = .0625;
double goodTip;
double badTip;
double goodTotal;
double badTotal;
double mealTax;
char response;
string yes = "yes";
cout << " How much did your meal cost? ";
cin >> mealCost;
cout << " Was the service good?\n ";
cout << " Please enter your response using a Y, y, 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 (response == 'Y' || 'y' || "yes")
{
cout << " The tip will be $ " << goodTip << endl;
cout << " The total bill will be $" << goodTotal << endl << endl;
}
else
{
cout << " Sorry to hear that \n";
cout << " The tip will be $ " << badTip << endl;
cout << " The total bill will be $" << badTotal << endl << endl;
}
system("pause");
return 0;
}
Code should work now. When setting multiple conditions, initialize the variable each time for each condition. Also you should note that when you are prompted to enter yes or no. Any word that begins with the letter 'y' will set the if statement condition to true, this due to using a type char.
//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()
{
double mealCost;
constdouble TAX = .0625;
double goodTip;
double badTip;
double goodTotal;
double badTotal;
double mealTax;
char response;
string yes = "yes";
cout << " How much did your meal cost? ";
cin >> mealCost;
cout << " Was the service good?\n ";
cout << " Please enter your response using a Y, y, 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 (response == 'Y' || response == 'y' || response == 'yes')//EDIT
{
cout << " The tip will be $ " << goodTip << endl;
cout << " The total bill will be $" << goodTotal << endl << endl;
}
else
{
cout << " Sorry to hear that \n";
cout << " The tip will be $ " << badTip << endl;
cout << " The total bill will be $" << badTotal << endl << endl;
}
system("pause");
return 0;
}