Clarification Needed on teachers statement

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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75


//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>
using namespace std;

int main()
{
	// variables
	double mealCost;
	const double 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;

}

  
   
  



  
They were referring to this part of the code
1
2
3
4
	goodTip = mealCost * .20;
	badTip = mealCost * .10;
	goodTotal = mealCost + mealTax + goodTip;
	badTotal = mealCost + mealTax + badTip; 


I think they wanted you to write the program in this way:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
int main()
{
	// variables
	double mealCost;
	const double 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;
}
Hello Alb13G,

The use of magic numbers should be avoided, i.e., the ".20" and ".10".
1
2
goodTip = mealCost * .20;
badTip = mealCost * .10;

Now and in the future it would help you to do something like:
1
2
3
4
5
constexpr double GOODTIP{ 0.20 };
constexpr double BADTIP( 0.10 };

goodTip = mealCost * GOODTIP;
badTip = mealCost * BADTIP;

This is the same thing Grime did with "TAX".

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.

Hope that helps,

Andy
Thanks for the help Andy and Grimes
Topic archived. No new replies allowed.