if/else 'Y' response restaraunt program

When I enter a response OTHER than 'y', 'Y' or "yes" it doesn't go to the ELSE statement can anyone see why ? Id appreciate a response Thanks !

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

}
 
Last edited on
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.
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
//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()
{
	double mealCost;
	const double 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;

}
 
Last edited on
That's it ! Thanks so much nicholasjb1996 I just read that in the book the other day to. Thanks again !
Topic archived. No new replies allowed.