116k views
1 vote
WRITE CODE C++ PLEASE : An airline describes air travel costs as follows. o Flight cost is adjusted as follows:: ▪ Children 2 or under = $0 cost //lap child ▪ Children older than 2 and younger than 13 = 90% of cost ▪ Seniors (60 or older) = 80% of cost o A first checked bag is free, second is $25, and each additional is $60. • Prompt the user to enter the following: o Flight cost, passenger age, and number of checked bags. • Use constant variables where appropriate • Output the total travel cost • Hints: o First use an if-else-if statement to determine the flight cost to be added to total travel cost o Use another if-else statement to update total travel cost based on the number of checked bags o Think carefully about what expression correctly calculates checked bag cost when bags are 3 or more. • Make sure you test your program for multiple ages and checked bags. Test the boundary values.

User Arnav Rao
by
7.6k points

1 Answer

4 votes

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.

User Milos Fec
by
8.0k points