#include <iostream> #include <string> #include <vector> #include <iomanip> using namespace std; // Define a Flight structure struct Flight { int flightNumber; string departureCity; string arrivalCity; string departureTime; string arrivalTime; int totalSeats; int availableSeats; double ticketPrice; string frequency; Flight(int number, const string& departure, const string& arrival, const string& departTime, const string& arriveTime, int seats, double price, const string& freq) : flightNumber(number), departureCity(departure), arrivalCity(arrival), departureTime(departTime), arrivalTime(arriveTime), totalSeats(seats), availableSeats(seats), ticketPrice(price), frequency(freq) {} }; // Define a Passenger structure struct Passenger { string name; int age; Passenger(const string& n, int a) : name(n), age(a) {} }; // Define a Ticket structure struct Ticket { Passenger passenger; Flight* flight; Ticket(const Passenger& p, Flight* f) : passenger(p), flight(f) {} }; // Define the AirlineSystem class class AirlineSystem { private: vector<Flight> flights; vector<Ticket> tickets; public: // Constructor to initialize flights AirlineSystem() { // Add predefined flights addFlight(1, "Karachi", "Islamabad", "08:00", "10:00", 200, 22500, "Daily"); addFlight(2, "Karachi", "Lahore", "09:00", "11:00", 200, 22500, "Daily"); addFlight(3, "Karachi", "Peshawar", "10:00", "12:00", 200, 12000, "Monday, Tuesday, Wednesday, Thursday"); addFlight(4, "Karachi", "Quetta", "11:00", "13:00", 200, 11500, "Monday, Tuesday, Wednesday, Thursday"); addFlight(5, "Karachi", "Faisalabad", "12:00", "14:00", 200, 22500, "Daily"); addFlight(6, "Islamabad", "Peshawar", "13:00", "15:00", 200, 22500, "Daily"); addFlight(7, "Islamabad", "Lahore", "14:00", "16:00", 200, 22500, "Daily"); addFlight(8, "Islamabad", "Quetta", "15:00", "17:00", 200, 25000, "Monday, Tuesday, Wednesday, Thursday"); addFlight(9, "Karachi", "Skardu", "16:00", "18:00", 200, 35000, "Friday, Saturday, Sunday"); addFlight(10, "Islamabad", "Skardu", "17:00", "19:00", 200, 30000, "Friday, Saturday, Sunday"); } // Book a ticket on a specific flight bool bookTicket(const string& passengerName, int passengerAge) { int chosenFlight; cout << "Enter the flight number to book a ticket: "; cin >> chosenFlight; for (int i = 0; i < flights.size(); i++) { // Traditional for loop Flight& flight = flights[i]; if (flight.flightNumber == chosenFlight) { if (flight.availableSeats > 0) { Passenger passenger(passengerName, passengerAge); tickets.push_back(Ticket(passenger, &flight)); flight.availableSeats--; cout << "Ticket booked for " << passengerName << " on flight " << chosenFlight << ".\n"; return true; } else { cout << "No seats available on flight " << chosenFlight << ".\n"; return false; } } } cout << "Flight " << chosenFlight << " not found.\n"; return false; } // Cancel a ticket bool cancelTicket(const string& passengerName) { int flightNumber; cout << "Enter the flight number to cancel the ticket: "; cin >> flightNumber; for (int i = 0; i < tickets.size(); i++) { // Find the ticket if (tickets[i].passenger.name == passengerName && tickets[i].flight->flightNumber == flightNumber) { tickets[i].flight->availableSeats++; // Return the seat tickets.erase(tickets.begin() + i); // Remove the ticket cout << "Ticket for " << passengerName << " on flight " << flightNumber << " has been canceled.\n"; return true; } } cout << "Ticket not found for " << passengerName << " on flight " << flightNumber << ".\n"; return false; } // Display all available flights void displayFlights() const { cout << "Available Flights:\n"; cout << setw(15) << "Flight Number" << setw(20) << "Departure" << setw(20) << "Arrival" << setw(20) << "Departure Time" << setw(20) << "Arrival Time" << setw(20) << "Available Seats" << setw(15) << "Ticket Price" << setw(25) << "Frequency (Days)" << "\n"; for (int i = 0; i < flights.size(); i++) { // Traditional for loop const Flight& flight = flights[i]; cout << setw(15) << flight.flightNumber << setw(20) << flight.departureCity << setw(20) << flight.arrivalCity << setw(20) << flight.departureTime << setw(20) << flight.arrivalTime << setw(20) << flight.availableSeats << setw(15) << flight.ticketPrice << setw(25) << flight.frequency << "\n"; } } // Display all booked tickets void displayTickets() const { cout << "Booked Tickets:\n"; cout << setw(20) << "Passenger Name" << setw(10) << "Age" << setw(15) << "Flight Number" << setw(20) << "Departure" << setw(20) << "Arrival" << "\n"; for (int i = 0; i < tickets.size(); i++) { // Traditional for loop const Ticket& ticket = tickets[i]; cout << setw(20) << ticket.passenger.name << setw(10) << ticket.passenger.age << setw(15) << ticket.flight->flightNumber << setw(20) << ticket.flight->departureCity << setw(20) << ticket.flight->arrivalCity << "\n"; } } private: // Function to add a flight void addFlight(int number, const string& departure, const string& arrival, const string& departTime, const string& arriveTime, int seats, double price, const string& freq) { flights.push_back(Flight(number, departure, arrival, departTime, arriveTime, seats, price, freq)); } }; // Function to display the menu void displayMenu() { cout << "\nAirline System Menu:\n"; cout << "1. Book a ticket\n"; cout << "2. Display available flights\n"; cout << "3. Cancel a ticket\n"; cout << "4. Display booked tickets\n"; cout << "5. Exit\n"; cout << "Enter your choice: "; } // Main program with interactive menu int main() { AirlineSystem airlineSystem; int choice; while (true) { displayMenu(); // Show menu cin >> choice; // Get user's choice switch (choice) { case 1: { // Book a ticket string passengerName; int passengerAge; cout << "Enter passenger name: "; cin.ignore(); // Clear newline in buffer getline(cin, passengerName); cout << "Enter passenger age: "; cin >> passengerAge; airlineSystem.bookTicket(passengerName, passengerAge); break; } case 2: { // Display available flights airlineSystem.displayFlights(); break; } case 3: { // Cancel a ticket string passengerName; cout << "Enter passenger name: "; cin.ignore(); // Clear newline in buffer getline(cin, passengerName); airlineSystem.cancelTicket(passengerName); break; } case 4: { // Display booked tickets airlineSystem.displayTickets(); break; } case 5: { // Exit the program cout << "Exiting the program. Goodbye!\n"; return 0; } default: cout << "Invalid choice. Please try again.\n"; break; } } return 0; }
Write, Run & Share C++ code online using OneCompiler's C++ online compiler for free. It's one of the robust, feature-rich online compilers for C++ language, running on the latest version 17. Getting started with the OneCompiler's C++ compiler is simple and pretty fast. The editor shows sample boilerplate code when you choose language as C++
and start coding!
OneCompiler's C++ online compiler supports stdin and users can give inputs to programs using the STDIN textbox under the I/O tab. Following is a sample program which takes name as input and print your name with hello.
#include <iostream>
#include <string>
using namespace std;
int main()
{
string name;
cout << "Enter name:";
getline (cin, name);
cout << "Hello " << name;
return 0;
}
C++ is a widely used middle-level programming language.
When ever you want to perform a set of operations based on a condition If-Else is used.
if(conditional-expression) {
//code
}
else {
//code
}
You can also use if-else for nested Ifs and If-Else-If ladder when multiple conditions are to be performed on a single variable.
Switch is an alternative to If-Else-If ladder.
switch(conditional-expression){
case value1:
// code
break; // optional
case value2:
// code
break; // optional
......
default:
code to be executed when all the above cases are not matched;
}
For loop is used to iterate a set of statements based on a condition.
for(Initialization; Condition; Increment/decrement){
//code
}
While is also used to iterate a set of statements based on a condition. Usually while is preferred when number of iterations are not known in advance.
while (condition) {
// code
}
Do-while is also used to iterate a set of statements based on a condition. It is mostly used when you need to execute the statements atleast once.
do {
// code
} while (condition);
Function is a sub-routine which contains set of statements. Usually functions are written when multiple calls are required to same set of statements which increases re-usuability and modularity. Function gets run only when it is called.
return_type function_name(parameters);
function_name (parameters)
return_type function_name(parameters) {
// code
}