0% found this document useful (0 votes)
9 views5 pages

C++ Project1

The document is a C++ program for managing flight bookings at Sky High Airlines. It allows users to add, view, search, update, and delete flight records stored in a binary file. The program includes a welcome page and utilizes a structure to define flight record attributes.

Uploaded by

Joseph Solomon
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views5 pages

C++ Project1

The document is a C++ program for managing flight bookings at Sky High Airlines. It allows users to add, view, search, update, and delete flight records stored in a binary file. The program includes a welcome page and utilizes a structure to define flight record attributes.

Uploaded by

Joseph Solomon
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 5

#include <iostream>

#include <fstream>
#include <cstring>
using namespace std;

struct FlightRecord {
int id;
char passengerName[50];
char flightNumber[10];
char seatNumber[5];
char departureCity[50];
char arrivalCity[50];
char departureDate[20];
float price;
};

void welcomePage() {
cout <<
"----------------------------------------------------------------------------------
---------------------------\n";
cout << " Welcome to Sky High Airlines\n";
cout <<
"----------------------------------------------------------------------------------
---------------------------\n";
cout << "Thank you for choosing us for your travel needs. We offer a seamless
experience for all your flight bookings.\n";
cout <<
"----------------------------------------------------------------------------------
---------------------------\n";
}

void addRecord() {
FlightRecord record;
ifstream readFile("flights.dat", ios::binary);
ofstream writeFile("flights.dat", ios::binary | ios::app);

int newId;
bool idExists = false;

cout << "Enter Booking ID: ";


cin >> newId;

// Check if ID already exists


if (readFile) {
FlightRecord tempRecord;
while (readFile.read((char*)&tempRecord, sizeof(tempRecord))) {
if (tempRecord.id == newId) {
idExists = true;
break;
}
}
}
readFile.close();

if (idExists) {
cout << "Error: ID " << newId << " already exists. Please use a different
ID.\n";
return;
}

// Proceed with adding the new record


record.id = newId;
cin.ignore();
cout << "Enter Passenger Name: ";
cin.getline(record.passengerName, 50);
cout << "Enter Flight Number: ";
cin.getline(record.flightNumber, 10);
cout << "Enter Seat Number: ";
cin.getline(record.seatNumber, 5);
cout << "Enter Departure City: ";
cin.getline(record.departureCity, 50);
cout << "Enter Arrival City: ";
cin.getline(record.arrivalCity, 50);
cout << "Enter Departure Date (YYYY-MM-DD): ";
cin.getline(record.departureDate, 20);
cout << "Enter Price: ";
cin >> record.price;

writeFile.write((char*)&record, sizeof(record));
writeFile.close();

cout << "Booking added!\n";


}

void viewRecords() {
FlightRecord record;
ifstream file("flights.dat", ios::binary);

if (!file) {
cout << "No records found.\n";
return;
}

// Print the table header


cout << "\
n----------------------------------------------------------------------------------
---------------------------------------------\n";
cout << "Booking ID | Passenger Name | Flight Number | Seat Number|
Departure City | Arrival City |Departure Date | Price\n";
cout <<
"----------------------------------------------------------------------------------
---------------------------------------------\n";

// Loop through the file and display all records in a table format
while (file.read((char*)&record, sizeof(record))) {
cout.width(11); cout << left <<record.id<<"|"; // Booking ID
cout.width(20); cout << left << record.passengerName<<"|"; // Passenger
Name
cout.width(15); cout << left << record.flightNumber<<"|"; // Flight
Number
cout.width(12); cout << left << record.seatNumber<<"|"; // Seat Number
cout.width(17); cout << left << record.departureCity<<"|"; // Departure
City
cout.width(15); cout << left << record.arrivalCity<<"|"; // Arrival City
cout.width(18); cout << left << record.departureDate<<"|"; // Departure
Date
cout.width(8); cout << right << "$" << record.price; // Price
cout << endl;
}

cout <<
"----------------------------------------------------------------------------------
---------------------------------------------\n";

file.close();
}

void searchRecord() {
int id;
FlightRecord record;
ifstream file("flights.dat", ios::binary);

cout << "Enter Booking ID to search: ";


cin >> id;

bool found = false;


while (file.read((char*)&record, sizeof(record))) {
if (record.id == id) {
cout << "Booking ID: " << record.id << "\n";
cout << "Passenger Name: " << record.passengerName << "\n";
cout << "Flight Number: " << record.flightNumber << "\n";
cout << "Seat Number: " << record.seatNumber << "\n";
cout << "Departure City: " << record.departureCity << "\n";
cout << "Arrival City: " << record.arrivalCity << "\n";
cout << "Departure Date: " << record.departureDate << "\n";
cout << "Price: $" << record.price << "\n";
found = true;
break;
}
}

if (!found) {
cout << "Record not found.\n";
}

file.close();
}

void updateRecord() {
int id;
FlightRecord record;
fstream file("flights.dat", ios::binary | ios::in | ios::out);

if (!file) {
cout << "No records found.\n";
return;
}

cout << "Enter Booking ID to update: ";


cin >> id;

bool found = false;


while (file.read((char*)&record, sizeof(record))) {
if (record.id == id) {
cout << "Record found. Enter new details:\n";
cin.ignore();
cout << "Enter Passenger Name: ";
cin.getline(record.passengerName, 50);
cout << "Enter Flight Number: ";
cin.getline(record.flightNumber, 10);
cout << "Enter Seat Number: ";
cin.getline(record.seatNumber, 5);
cout << "Enter Departure City: ";
cin.getline(record.departureCity, 50);
cout << "Enter Arrival City: ";
cin.getline(record.arrivalCity, 50);
cout << "Enter Departure Date (YYYY-MM-DD): ";
cin.getline(record.departureDate, 20);
cout << "Enter Price: ";
cin >> record.price;

// Move file pointer back to overwrite the record


file.seekp(-sizeof(record), ios::cur);
file.write((char*)&record, sizeof(record));
found = true;
cout << "Record updated!\n";
break;
}
}

if (!found) {
cout << "Record not found.\n";
}

file.close();
}

void deleteRecord() {
int id;
FlightRecord record;
ifstream file("flights.dat", ios::binary);
ofstream tempFile("temp.dat", ios::binary);

if (!file) {
cout << "No records found.\n";
return;
}

cout << "Enter Booking ID to delete: ";


cin >> id;

bool found = false;


while (file.read((char*)&record, sizeof(record))) {
if (record.id == id) {
found = true;
cout << "Record with Booking ID " << id << " deleted.\n";
} else {
tempFile.write((char*)&record, sizeof(record));
}
}

file.close();
tempFile.close();

// Replace original file with temp file


remove("flights.dat");
rename("temp.dat", "flights.dat");

if (!found) {
cout << "Record not found.\n";
}
}

int main() {
int choice;

welcomePage();

do {
cout << "\n1. Add Record\n2. View Records\n3. Search Record\n4. Update
Record\n5. Delete Record\n6. Exit\nEnter your choice: ";
cin >> choice;

switch (choice) {
case 1:
addRecord();
break;
case 2:
viewRecords();
break;
case 3:
searchRecord();
break;
case 4:
updateRecord();
break;
case 5:
deleteRecord();
break;
case 6:
cout << "Exiting...\n";
break;
default:
cout << "Invalid choice.\n";
}
} while (choice != 6);

return 0;
}

You might also like