-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathP2-Railway-Ticket-Reservation-System.cpp
More file actions
126 lines (107 loc) · 3.08 KB
/
P2-Railway-Ticket-Reservation-System.cpp
File metadata and controls
126 lines (107 loc) · 3.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
#include <iostream>
#include <vector>
#include <string>
#include <iomanip>
#include <fstream>
using namespace std;
struct Ticket {
string passengerName;
int trainNumber;
int seatNumber;
string departure;
string destination;
};
vector<Ticket> tickets;
int seatCounter = 1;
void saveTickets() {
ofstream file("tickets.txt");
if (!file) {
cout << "Error opening file!\n";
return;
}
for (const auto &t : tickets) {
file << t.passengerName << "," << t.trainNumber << ","
<< t.seatNumber << "," << t.departure << "," << t.destination << "\n";
}
file.close();
}
void loadTickets() {
ifstream file("tickets.txt");
if (!file) {
cout << "No previous bookings found.\n";
return;
}
Ticket t;
while (file >> t.passengerName >> t.trainNumber >> t.seatNumber >> t.departure >> t.destination) {
tickets.push_back(t);
seatCounter = t.seatNumber + 1;
}
file.close();
}
void bookTicket() {
Ticket t;
cout << "Enter Passenger Name: ";
cin.ignore();
getline(cin, t.passengerName);
cout << "Enter Train Number: ";
cin >> t.trainNumber;
cout << "Enter Departure Station: ";
cin.ignore();
getline(cin, t.departure);
cout << "Enter Destination Station: ";
getline(cin, t.destination);
t.seatNumber = seatCounter++;
tickets.push_back(t);
saveTickets();
cout << "Ticket booked successfully! Seat Number: " << t.seatNumber << endl;
}
void displayTickets() {
if (tickets.empty()) {
cout << "No tickets booked yet.\n";
return;
}
cout << "\nBooked Tickets:\n";
cout << setw(15) << left << "Passenger" << setw(10) << "Train No"
<< setw(10) << "Seat No" << setw(15) << "Departure"
<< setw(15) << "Destination" << endl;
cout << string(65, '-') << endl;
for (const auto &t : tickets) {
cout << setw(15) << left << t.passengerName
<< setw(10) << t.trainNumber
<< setw(10) << t.seatNumber
<< setw(15) << t.departure
<< setw(15) << t.destination << endl;
}
}
void cancelTicket() {
int seatNo, found = 0;
cout << "Enter Seat Number to Cancel: ";
cin >> seatNo;
for (auto it = tickets.begin(); it != tickets.end(); ++it) {
if (it->seatNumber == seatNo) {
tickets.erase(it);
saveTickets(); // Save updated list after deletion
cout << "Ticket with Seat Number " << seatNo << " cancelled successfully!\n";
found = 1;
break;
}
}
if (!found)
cout << "Ticket not found!\n";
}
int main() {
loadTickets();
int choice;
while (true) {
cout << "\n1. Book Ticket\n2. Display Tickets\n3. Cancel Ticket\n4. Exit\n";
cout << "Enter choice: ";
cin >> choice;
switch (choice) {
case 1: bookTicket(); break;
case 2: displayTickets(); break;
case 3: cancelTicket(); break;
case 4: return 0;
default: cout << "Invalid choice! Try again.\n";
}
}
}