Final answer:
The provided C++ code calculates the total travel cost for an airline passenger by considering age-based discounts on flight costs and fees for checked bags. It prompts the user for necessary inputs and uses control structures like if-else statements to compute the final amount.
Step-by-step explanation:
To solve the given scenario where we have to calculate the total travel cost of an airline passenger based on age and the number of checked bags, we can write a simple C++ program. The following code prompts the user for flight cost, passenger age, and the number of checked bags, and then calculates the final cost considering discounts for children and seniors along with baggage fees. The code uses constant variables for the percentages and baggage fees to adhere to best practices.
#include
using namespace std;
int main() {
const double CHILD_DISCOUNT = 0.9;
const double SENIOR_DISCOUNT = 0.8;
const int FREE_BAGS = 1;
const double SECOND_BAG_COST = 25;
const double ADDITIONAL_BAG_COST = 60;
double flightCost;
int age, numBags;
double totalCost = 0;
cout << "Enter flight cost: ";
cin >> flightCost;
cout << "Enter passenger age: ";
cin >> age;
cout << "Enter number of checked bags: ";
cin >> numBags;
if (age <= 2) {
totalCost = 0; // Lap children fly free
} else if (age > 2 && age < 13) {
totalCost = flightCost * CHILD_DISCOUNT;
} else if (age >= 60) {
totalCost = flightCost * SENIOR_DISCOUNT;
} else {
totalCost = flightCost;
}
if (numBags == 1) {
// First bag is free
} else if (numBags == 2) {
totalCost += SECOND_BAG_COST;
} else if (numBags > 2) {
totalCost += SECOND_BAG_COST + (numBags - 2) * ADDITIONAL_BAG_COST;
}
cout << "Total travel cost: $" << totalCost << endl;
return 0;
}
The code uses an if-else-if statement to adjust the flight cost based on age, followed by another if-else statement to incorporate the baggage fees. This example assumes that children 2 or under fly free, while other age groups get respective discounts. The bag fees escalate once a passenger checks more than one bag.