How to format cout statements when using if... and else if...

Hello! I'm just starting to learn C++, so I'm not quite sure what I'm doing yet.
For this program, I have to write a program that will calculate how much a person pays to go to the zoo. The user inputs the cost of admission and age. If the person is under 5, it's free. If they're ages 5-12, it's half-off. If between 13 and 59, it's full price. If they're above 60, it's a 30% discount. The program also needs to ask the user if they want a hotdog and/or a soft drink, then add 7% sales tax to the food (I havent gotten to the tax part yet).

My main question right now is how to put all the cout statements at the bottom, so they all output together after everything else (kind of like a receipt). I'm just not sure how to do it because of the if statements (which I am using for the first time). How do I put all the cout statements at the end and still make it only output one cout, depending on their age?
Also if you have any other suggestions of how to improve my program, I would appreciate it!
Thanks in advance!

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
76
77
78
79
80
81
82
83
84
85
86
87
#include <iostream>
#include <iomanip>
using namespace std;

int main()
{
	double admissionPrice;//the price of admission
	int age;//the age of the user

	cout << "What is the price of admission? (excluding a dollar sign):" << endl;
	cin >> admissionPrice;

	cout << "What is the age of the person going to the zoo?" << endl;
	cin >> age;

	double freeAdmission;//the admission price for a child under 5
	double halfPriceAdmission;//the admission price for children ages 5-12
	double thirtyPercentDiscount;//the admission price for people over 60

	freeAdmission = admissionPrice * 0;
	halfPriceAdmission = admissionPrice / 2;
	thirtyPercentDiscount = admissionPrice - (admissionPrice * .3);

	if (age < 5)
		{
			cout << "Free Admission: $" << fixed << showpoint << setprecision(2) << freeAdmission << endl;
		}	
	else if (5 <= age && age <= 12)
		{
			cout << "Half Price Admission: $" << fixed << showpoint << setprecision(2) << halfPriceAdmission << endl;
		}
	else if (13 <= age && age <= 59)
		{
			cout << "Admission Price: $" << fixed << showpoint << setprecision(2) << admissionPrice << endl;
		}	
	else if (age >= 60)
		{
			cout << "30% Discount: $" << fixed << showpoint << setprecision(2) << thirtyPercentDiscount << endl;
		}

	double hotdogPrice;//the price of a hot dog
	char h;//their answer to the question if they want a hot dog

	char Y; char y;//yes
	char N; char n;//no

	hotdogPrice = 2.50;

	cout << "Do you want a hot dog? Y for yes, N for no." << endl;
	cin >> h;

	if(h == 'Y' || h == 'y')
		{
			cout << "Hot dog: $" << fixed << showpoint << setprecision(2) << hotdogPrice <<endl;
		}
	else if(h == 'N' || h == 'n')
		{
			cout << "No hotdog" << endl;
		}

	char d;//their answer to the question if they want a soft drink
	char s;//their answer to what size soft drink they want
	char M; char m;//medium soft drink
	char L; char l;//large soft drink
	double mediumPrice;//price of a medium soft drink
	double largePrice;//price of a large soft drink
	mediumPrice = 2.50;
	largePrice = 4.00;

	cout << "Do you want a soft drink? Y for yes, N for no." << endl;
	cin >> d;

	if (d == 'Y' || d == 'y')
		{
			cout << "Do you want a medium or large soft drink? M for medium, L for large" << endl;
			cin >> s;
			if (s == 'M' || s == 'm')
				cout << "Medium drink: $" << mediumPrice << endl;
			else if (s == 'L' || s == 'l')
				cout << "Large soft drink: $" << largePrice << endl;
		}
	else if (d == 'N' || d == 'n')
		cout << "No soft drink" << endl;

	system("pause");
	return 0;
}
Last edited on
If you want people to make the effort to read your code, you need to make the effort to make it readable. Use code tags when posting code.
Oh oops, sorry. There you go!
Topic archived. No new replies allowed.