Calculate Ticket Price Based on User's Age in C++

In this C++ exercise, you will create a program that calculates the price of a ticket based on the age of the user. Different age groups are often charged different prices for events, transportation, or attractions. This program helps simulate such a pricing system.

You will use conditional statements to determine the appropriate price category. For example, children might receive a discounted rate or enter for free, adults pay the full price, and seniors may also receive a reduced fare.

This task enhances your understanding of input handling, conditional logic, and control structures in C++. By the end of the exercise, you'll be comfortable using if-else statements to create dynamic behaviors based on user input.

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

 Copy 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

Share this C++ Exercise


More C++ Programming Exercises of Advanced Conditional Structures in C++

Explore our set of C++ Programming Exercises! Specifically designed for beginners, these exercises will help you develop a solid understanding of the basics of C++. From variables and data types to control structures and simple functions, each exercise is crafted to challenge you incrementally as you build confidence in coding in C++.