Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <fstream>>
- #include <string.h>
- using namespace std;
- class Passengers
- {
- public:
- char name[20];
- int id, age;
- char destination[30];
- void WriteText()
- {
- ofstream out;
- out.open("test.txt", ios::out | ios::app);
- if (out.is_open())
- {
- out << "hello guys"
- << "\n";
- out.close();
- }
- else
- {
- cout << "file can't be found"
- << "\n";
- }
- }
- void ReadText()
- {
- char c, str[20];
- fstream in;
- in.open("test.txt", ios::in);
- if (in.is_open())
- {
- in >> str;
- cout << str;
- in.get(c);
- cout << c;
- in.get(str, 20);
- cout << str;
- in.close();
- }
- else
- {
- cout << "file is not found"
- << "\n";
- }
- }
- void writePassenger()
- {
- char ch;
- fstream outfile;
- outfile.open("Passengers.txt", ios::out | ios::app);
- if (outfile.is_open())
- {
- Passengers p1;
- do
- {
- cout << "enter passenger's id : ";
- cin >> p1.id;
- cout << "enter passenger's name : ";
- cin >> p1.name;
- cout << "enter passenger's age : ";
- cin >> p1.age;
- cout << "enter passenger's destination : ";
- cin >> p1.destination;
- cout << "passenger's Information have been saved successfully"
- << "\n";
- cout << "do you want to enter new passengers (y/n) : ";
- cin >> ch;
- outfile.write((char *)&p1, sizeof(p1));
- }
- while (ch == 'y');
- outfile.close();
- }
- else
- {
- cout << "file is not found"
- << "\n";
- }
- }
- void Readpassengers()
- {
- Passengers p1;
- fstream in;
- int id = 0;
- in.open("Passengers.txt", ios::in);
- if (in.is_open())
- {
- cout << "ID"
- << "\t"
- << "name"
- << "\t"
- << "age"
- << "\t"
- << "Destination"
- << "\n";
- in.read((char *)&p1, sizeof(p1));
- while (!in.eof())
- {
- if (p1.id != id)
- {
- cout << p1.id << "\t" << p1.name << "\t" << p1.age << "\t" << p1.destination << "\n";
- in.read((char *)&p1, sizeof(p1));
- }
- }
- in.close();
- }
- else
- {
- cout << "file is not found"
- << "\n";
- }
- }
- void SearchforName()
- {
- bool found = false;
- char str[20];
- cout << "write a name to search for : ";
- cin >> str;
- Passengers p1;
- fstream in;
- int id = 0;
- in.open("Passengers.txt", ios::in);
- if (in.is_open())
- {
- in.read((char *)&p1, sizeof(p1));
- while (!in.eof())
- {
- if (strcmp(str, p1.name) == 0)
- {
- cout << "ID"
- << "\t"
- << "name"
- << "\t"
- << "age"
- << "\t"
- << "Destination"
- << "\n";
- cout << p1.id << "\t" << p1.name << "\t" << p1.age << "\t" << p1.destination << "\n";
- found = true;
- }
- in.read((char *)&p1, sizeof(p1));
- }
- if (!found)
- {
- cout << "Name is not found"
- << "\n";
- }
- in.close();
- }
- else
- {
- cout << "file is not found"
- << "\n";
- }
- }
- void CalculateLength()
- {
- fstream in;
- in.open("test.txt", ios::in);
- if (in.is_open())
- {
- in.seekg(0, ios::end);
- cout << "the length of the file is : " << in.tellg();
- in.close();
- }
- }
- void CopyContent()
- {
- char ch;
- fstream in;
- fstream out;
- in.open("test.txt", ios::in);
- out.open("copy test.txt", ios::out);
- if (in.is_open())
- {
- while (in.get(ch))
- out.put(ch);
- in.close();
- out.close();
- }
- };
- void updatepassenger()
- {
- bool found = false;
- char name[20];
- cout << "Enter the name of the passenger to update: ";
- cin >> name;
- Passengers p;
- fstream file;
- file.open("Passengers.txt", ios::in | ios::out | ios::binary);
- if (file.is_open())
- {
- while (!file.eof())
- {
- streampos pos = file.tellg();
- file.read((char*)&p, sizeof(p));
- if (strcmp(p.name, name) == 0)
- {
- cout << "What do you want to update? (name/age/id/destination): ";
- char field[20];
- cin >> field;
- if (strcmp(field, "name") == 0)
- {
- char new_name[20];
- cout << "Enter the new name: ";
- cin >> new_name;
- strcpy(p.name, new_name);
- }
- else if (strcmp(field, "age") == 0)
- {
- int new_age;
- cout << "Enter the new age: ";
- cin >> new_age;
- p.age = new_age;
- }
- else if (strcmp(field, "id") == 0)
- {
- int new_id;
- cout << "Enter the new id: ";
- cin >> new_id;
- p.id = new_id;
- }
- else if (strcmp(field, "destination") == 0)
- {
- char new_dest[30];
- cout << "Enter the new destination: ";
- cin >> new_dest;
- strcpy(p.destination, new_dest);
- }
- else
- {
- cout << "Invalid field name. Please try again." << endl;
- return;
- }
- file.seekp(pos);
- file.write((char*)&p, sizeof(p));
- found = true;
- cout << "Passenger updated successfully." << endl;
- break;
- }
- }
- if (!found)
- {
- cout << "Passenger not found." << endl;
- }
- file.close();
- }
- else
- {
- cout << "File not found." << endl;
- }
- }
- void deletepassenger()
- {
- Passengers p1;
- p1.Readpassengers();
- bool found = false;
- char str[20];
- cout << "write a name to delete : ";
- cin >> str;
- fstream in;
- fstream out;
- out.open("temp.txt", ios::out);
- int id = 0;
- in.open("Passengers.txt", ios::in);
- if (in.is_open())
- {
- in.read((char *)&p1, sizeof(p1));
- while (!in.eof())
- {
- if (strcmp(str, p1.name) != 0)
- {
- out.write((char *)&p1, sizeof(p1));
- found = true;
- }
- in.read((char *)&p1, sizeof(p1));
- }
- if (!found)
- cout << "Name is not found"
- << "\n";
- in.close();
- out.close();
- remove("Passengers.txt");
- rename("temp.txt", "Passengers.txt");
- }
- else
- {
- cout << "file is not found"
- << "\n";
- }
- p1.Readpassengers();
- }
- };
- int main()
- {
- Passengers p1;
- int ch;
- do
- {
- cout << "------------BUS RESERVATION SYSTEM---------------"
- << "\n";
- cout << "(Choose one of these to do)"
- << "\n";
- cout << "1 - Enter new passenger"
- << "\n";
- cout << "2 - View all passengers"
- << "\n";
- cout << "3 - Search for a passenger"
- << "\n";
- cout << "4 - Change passenger information"
- << "\n";
- cout << "5 - Delete a passenger"
- << "\n";
- cout << "6 - Exit"
- << "\n";
- cout << "Your choice : ";
- cin >> ch;
- switch (ch)
- {
- case 1:
- p1.writePassenger();
- break;
- case 2:
- p1.Readpassengers();
- break;
- case 3:
- p1.SearchforName();
- break;
- case 4:
- p1.updatepassenger();
- break;
- case 5:
- p1.deletepassenger();
- break;
- case 6:
- cout << "\n Thanks for using our System , we hope you like it :)"
- << "\n";
- return 0;
- }
- }
- while (ch >= 1 && ch <= 6);
- if (ch < 1 || ch > 6)
- {
- cout << "\n please choose one of the shown choices"
- << "\n"
- << "\n";
- do
- {
- cout << "------------BUS RESERVATION SYSTEM---------------"
- << "\n";
- cout << "((Choose one of these to do))"
- << "\n";
- cout << "1 - Enter new passenger"
- << "\n";
- cout << "2 - View all passengers"
- << "\n";
- cout << "3 - Search for a passenger"
- << "\n";
- cout << "4 - Change passenger information"
- << "\n";
- cout << "5 - Delete a passenger"
- << "\n";
- cout << "6 - Exit"
- << "\n";
- cout << "Your choice : ";
- cin >> ch;
- switch (ch)
- {
- case 1:
- p1.writePassenger();
- cout << "\n";
- break;
- case 2:
- p1.Readpassengers();
- cout << "\n";
- break;
- case 3:
- p1.SearchforName();
- cout << "\n";
- break;
- case 4:
- p1.updatepassenger();
- cout << "\n";
- break;
- case 5:
- p1.deletepassenger();
- cout << "\n";
- break;
- case 6:
- cout << "\n Thanks for using our System , we hope you like it :)"
- << "\n";
- return 0;
- }
- }
- while (ch > 6 || ch < 1);
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement