Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <string>
- #include <fstream>
- #include <random>
- #include <conio.h>
- using namespace std;
- struct Flights {
- size_t id;
- string pilot_name;
- int day_of_the_month;
- int passengers_1st_class;
- int passengers_2nd_class;
- int price_1st_class;
- int price_2nd_class;
- };
- void create_file(Flights[], size_t);
- void edit_file(Flights[], size_t);
- void flights_for_a_pilot(Flights[], size_t);
- void all_flights(Flights[], size_t);
- int stefan() {
- size_t choice, array_size;
- cout << "Number of flights: ";
- cin >> array_size;
- Flights *flights = new Flights[array_size];
- do {
- system("cls");
- cout << "Menu: \n";
- cout << "[1] Enter information about flights \n";
- cout << "[2] Edit information about flights \n";
- cout << "[3] Flights for a pilot \n";
- cout << "[4] All flights for the month\n\n";
- cout << "[0] Exit\n\n";
- cout << "Your choice:";
- cin >> choice;
- switch (choice) {
- case 1:
- system("cls");
- create_file(flights, array_size);
- break;
- case 2:
- system("cls");
- edit_file(flights, array_size);
- cout << "\n\nFile modified successfully! -- Press any key to get back to the menu!";
- _getch();
- break;
- case 3:
- system("cls");
- flights_for_a_pilot(flights, array_size);
- cout << "\n\nPress any key to get back to the menu!";
- _getch();
- break;
- case 4:
- system("cls");
- all_flights(flights, array_size);
- cout << "\n\nPress any key to get back to the menu!";
- _getch();
- break;
- }
- } while (choice != 0);
- return 0;
- }
- void create_file(Flights array[], size_t array_size) {
- fstream file;
- random_device rd;
- mt19937 generator(rd());
- uniform_int_distribution<> dist(1, 1000);
- file.open("flights.dat", ios::binary | ios::out);
- for (size_t i = 0; i < array_size; i++) {
- cin.ignore();
- array[i].id = dist(generator);
- cout << "Enter pilot's full name: ";
- getline(cin, array[i].pilot_name);
- cout << "Enter flight #" << array[i].id << " day of the month: ";
- cin >> array[i].day_of_the_month;
- cout << "Enter flight #" << array[i].id << " 1st class passengers: ";
- cin >> array[i].passengers_1st_class;
- cout << "Enter flight #" << array[i].id << " 2nd class passengers: ";
- cin >> array[i].passengers_2nd_class;
- cout << "Enter flight #" << array[i].id << " 1st class price: ";
- cin >> array[i].price_1st_class;
- cout << "Enter flight #" << array[i].id << " 2nd class price: ";
- cin >> array[i].price_2nd_class;
- }
- file.write(reinterpret_cast<char *> (&array), array_size * sizeof(array));
- file.close();
- }
- void edit_file(Flights array[], size_t array_size) {
- fstream file;
- file.open("flights.dat", ios::binary | ios::in);
- file.read(reinterpret_cast<char *> (&array), array_size * sizeof(array));
- size_t flight_id;
- cout << "Enter flight id to search by: ";
- cin >> flight_id;
- cin.ignore();
- file.close();
- for (size_t i = 0; i < array_size; i++) {
- if (array[i].id == flight_id) {
- cout << "--Edit number of passengers--\n\n";
- cout << "Enter flight #" << array[i].id << " 1st class passengers: ";
- cin >> array[i].passengers_1st_class;
- cout << "Enter flight #" << array[i].id << " 2nd class passengers: ";
- cin >> array[i].passengers_2nd_class;
- }
- }
- file.open("flights.dat", ios::binary | ios::out);
- file.write(reinterpret_cast<char *> (&array), array_size * sizeof(array));
- file.close();
- }
- void flights_for_a_pilot(Flights array[], size_t array_size) {
- fstream file;
- string pilot_name;
- bool found = false;
- file.open("flights.dat", ios::binary | ios::in);
- file.read(reinterpret_cast<char *> (&array), array_size * sizeof(array));
- cin.ignore();
- cout << "Enter pilot name to search by: ";
- getline(cin, pilot_name);
- for (size_t i = 0; i < array_size; i++) {
- if (strcmp(array[i].pilot_name.c_str(), pilot_name.c_str()) == 0) {
- cout << "--Information about pilot name " << pilot_name << "--\n\n";
- cout << "Flight's number: " << array[i].id << endl;
- cout << "Flight's day of the month: " << array[i].day_of_the_month << endl;
- cout << "Flight's passengers 1st class: " << array[i].passengers_1st_class << endl;
- cout << "Flight's passengers 2nd class: " << array[i].passengers_2nd_class << endl;
- cout << "Flight's price 1st class: " << array[i].price_1st_class << endl;
- cout << "Flight's price 2nd class: " << array[i].price_2nd_class << endl << endl;
- found = !found;
- }
- }
- if (!found)
- cout << "\nNo records found in the database!\n";
- file.close();
- }
- void all_flights(Flights array[], size_t array_size) {
- fstream file;
- file.open("flights.dat", ios::binary | ios::in);
- file.read(reinterpret_cast<char *> (&array), array_size * sizeof(array));
- for (size_t i = 0; i < array_size; i++) {
- cout << "Flight's number: " << array[i].id << endl;
- cout << "Flight's pilot name: " << array[i].pilot_name << endl;
- cout << "Flight's day of the month: " << array[i].day_of_the_month << endl;
- cout << "Flight's passengers 1st class: " << array[i].passengers_1st_class << endl;
- cout << "Flight's passengers 2nd class: " << array[i].passengers_2nd_class << endl;
- cout << "Flight's price 1st class: " << array[i].price_1st_class << endl;
- cout << "Flight's price 2nd class: " << array[i].price_2nd_class << endl << endl;
- }
- file.close();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement