C++ Project1
C++ Project1
#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;
if (idExists) {
cout << "Error: ID " << newId << " already exists. Please use a different
ID.\n";
return;
}
writeFile.write((char*)&record, sizeof(record));
writeFile.close();
void viewRecords() {
FlightRecord record;
ifstream file("flights.dat", ios::binary);
if (!file) {
cout << "No records found.\n";
return;
}
// 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);
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;
}
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;
}
file.close();
tempFile.close();
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;
}