Group
Advanced Conditional Structures in C++
Objective
1. Prompt the user to enter their age.
2. Use conditional logic to determine the price based on the following rules:
- Age 0-4: Free
- Age 5-17: $5
- Age 18-64: $10
- Age 65 and over: $6
3. Display the corresponding ticket price.
Implement a program that calculates the ticket price based on the user's age.
Example C++ Exercise
Show C++ Code
#include <iostream> // Include the input/output stream library
using namespace std;
int main() {
int age; // Variable to store the user's age
int ticketPrice; // Variable to store the calculated ticket price
// Ask the user to enter their age
cout << "Enter your age: ";
cin >> age;
// Determine the ticket price based on age
if (age >= 0 && age <= 4) {
ticketPrice = 0; // Children under 5 enter for free
} else if (age >= 5 && age <= 17) {
ticketPrice = 5; // Children and teenagers pay $5
} else if (age >= 18 && age <= 64) {
ticketPrice = 10; // Adults pay $10
} else if (age >= 65) {
ticketPrice = 6; // Seniors pay $6
} else {
// Handle invalid age input
cout << "Invalid age entered." << endl;
return 1; // Exit the program with an error code
}
// Output the ticket price
cout << "Your ticket price is: $" << ticketPrice << endl;
return 0; // End of program
}
Output
Enter your age: 70
Your ticket price is: $6