#include <cstring>
#include <ctime>
#include <cstdlib>
#include <fstream>

using namespace std;

struct date {
int day;
char *month;
int year;
};

struct fio {
char *name;
char *surname;
char *patronymic;
};

struct car {
char *brand;
fio owner;
int engine_power;
int mileage;
date last_service;
};

void load_data(car *cars, int n, const string &file_name);
void add_car(car *cars, int &n);
void print_cars(const car *cars, int n);
void search_cars(const car *cars, int n, date current_date);
void find_brand(const car *cars, int n, const char *brand);
void sort_print_high_mileage(car *cars, int n, int mileage_limit);
void save_data(const car *cars, int n, const string &file_name);
void free_memory(car *cars, int n);

int main() {
int n;
cout << "Enter the number of cars: ";
cin >> n;
cin.ignore();

car *cars = new car[n];

load_data(cars, n, “cars_data.txt”);

bool exit = false;
while (!exit) {
int choice;
cout << “1. Add a new car” << endl;
cout << “2. Print car information” << endl;
cout << “3. Find cars with more than 18 months since last service” << endl;
cout << “4. Find cars of a specified brand” << endl;
cout << “5. Find owners with mileage greater than specified (sorted by alphabet)” << endl;
cout << “6. Exit” << endl;
cout << "Choose an action: ";
cin >> choice;
cin.ignore();

time_t now = time(0);
tm *ltm = localtime(&now);
date current_date;
current_date.day = ltm->tm_mday;
current_date.month = new char[20];
strcpy(current_date.month, “xx”);
current_date.year = 1900 + ltm->tm_year;

if (choice == 1) {
cout << "Enter the number of new cars: ";
int k;
cin >> k;
cin.ignore();
car *new_cars = new car[k];
add_car(new_cars, k);
car *temp_cars = new car[n+k];
memcpy(temp_cars, cars, n * sizeof(car));
memcpy(temp_cars + n, new_cars, k * sizeof(car));
n += k;
delete[] cars;
delete[] new_cars;
cars = temp_cars;
}
else if (choice == 2) {
print_cars(cars, n);
}
else if (choice == 3) {
search_cars(cars, n, current_date);
}
else if (choice == 4) {
char *brand = new char[20];
cout << "Enter the brand to search for: ";
gets(brand);
find_brand(cars, n, brand);
delete[] brand;
}
else if (choice == 5) {
int mileage_limit;
cout << "Enter the mileage limit: ";
cin >> mileage_limit;
cin.ignore();
sort_print_high_mileage(cars, n, mileage_limit);
}
else {
exit = true;
}
}

save_data(cars, n, “cars_data.txt”);
free_memory(cars, n);
delete[] cars;
delete[] current_date.month;

return 0;
}

void load_data(car *cars, int n, const string &file_name) {
ifstream input_file(file_name);
if (!input_file.is_open()) {
cerr << “Failed to open file for reading.” << endl;
return;
}

for (int i = 0; i < n; i++) {
cars[i].brand = new char[20];
input_file.getline(cars[i].brand, 20);

cars[i].owner.name = new char[20];
input_file.getline(cars[i].owner.name, 20);

cars[i].owner.surname = new char[20];
input_file.getline(cars[i].owner.surname, 20);

cars[i].owner.patronymic = new char[20];
input_file.getline(cars[i].owner.patronymic, 20);

input_file >> cars[i].engine_power;
input_file >> cars[i].mileage;

cars[i].last_service.day = 0;
input_file >> cars[i].last_service.day;

cars[i].last_service.month = new char[20];
input_file.getline(cars[i].last_service.month, 20);

cars[i].last_service.year = 0;
input_file >> cars[i].last_service.year;

input_file.ignore();
}

input_file.close();
}

void add_car(car *cars, int &n) {
for (int i = 0; i < n; i++) {
cout << "New car: " << endl;
cout << "Brand: ";
cars[i].brand = new char[20];
cin.getline(cars[i].brand, 20);

cout << "Owner’s name: ";
cars[i].owner.name = new char[20];
cin.getline(cars[i].owner.name, 20);

cout << "Owner’s surname: ";
cars[i].owner.surname = new char[20];
cin.getline(cars[i].owner.surname, 20);

cout << "Owner’s patronymic: ";
cars[i].owner.patronymic = new char[20];
cin.getline(cars[i].owner.patronymic, 20);

cout << "Engine power: ";
cin >> cars[i].engine_power;
cin.ignore();

cout << "Mileage: ";
cin >> cars[i].mileage;
cin.ignore();

cout << "Last service date (day, month, year): ";
cars[i].last_service.day = 0;
cin >> cars[i].last_service.day;

cars[i].last_service.month = new char[20];
cin.ignore();
cin.getline(cars[i].last_service.month, 20);

cars[i].last_service.year = 0;
cin >> cars[i].last_service.year;

cout << endl;
}
}

void print_cars(const car *cars, int n) {
cout << "Car information: " << endl;
for (int i = 0; i < n; i++) {
cout << cars[i].brand << " | " << cars[i].owner.name << " | " << cars[i].owner.surname << " | " << cars[i].owner.patronymic
<< " | " << cars[i].engine_power << " | " << cars[i].mileage << " | " << cars[i].last_service.day << " " << cars[i].last_service.month << " " << cars[i].last_service.year << endl;
}
cout << endl;
}

void search_cars(const car *cars, int n, date current_date) {
cout << "Cars that need service: " << endl;
for (int i = 0; i < n; i++) {
char *month_names[] = {“January”, “February”, “March”, “April”, “May”, “June”, “July”, “August”, “September”, “October”, “November”, “December”};
int current_month_index = 0;
int last_service_month_index = 0;

for (int j = 0; j < 12; j++) {
if (strcmp(current_date.month, month_names[j]) == 0) {
current_month_index = j + 1;
}
if (strcmp(cars[i].last_service.month, month_names[j]) == 0) {
last_service_month_index = j + 1;
}
}

int months_since_last_service = (current_date.year - cars[i].last_service.year) * 12 + (current_month_index - last_service_month_index);
if (months_since_last_service > 18) {
cout << cars[i].brand << " | " << cars[i].owner.name << " | " << cars[i].owner.surname << " | " << cars[i].owner.patronymic
<< " | " << cars[i].engine_power << " | " << cars[i].mileage << " | " << cars[i].last_service.day << " " << cars[i].last_service.month << " " << cars[i].last_service.year << endl;
}
}
cout << endl;
}

void find_brand(const car *cars, int n, const char *brand) {
cout << "Cars with the specified brand: " << endl;
for (int i = 0; i < n; i++) {
if (strcmp(cars[i].brand, brand) == 0) {
cout << cars[i].brand << " | " << cars[i].owner.name << " | " << cars[i].owner.surname << " | " << cars[i].owner.patronymic
<< " | " << cars[i].engine_power << " | " << cars[i].mileage << " | " << cars[i].last_service.day << " | " << cars[i].last_service.month << " " << cars[i].last_service.year << endl;
}
}
cout << endl;
}

void sort_print_high_mileage(car *cars, int n, int mileage_limit) {
car *high_mileage_cars = nullptr;
int count = 0;

for (int i = 0; i < n; i++) {
if (cars[i].mileage > mileage_limit) {
count++;
car *temp = new car[count];
if (high_mileage_cars != nullptr) {
memcpy(temp, high_mileage_cars, (count - 1) * sizeof(car));
delete[] high_mileage_cars;
}
temp[count - 1] = cars[i];
high_mileage_cars = temp;
}
}

for (int i = 0; i < count - 1; i++) {
for (int j = 0; j < count - i - 1; j++) {
if (strcmp(high_mileage_cars[j].owner.surname, high_mileage_cars[j + 1].owner.surname) > 0) {
car temp = high_mileage_cars[j];
high_mileage_cars[j] = high_mileage_cars[j + 1];
high_mileage_cars[j + 1] = temp;
}
}
}

cout << "Owners with mileage greater than the specified limit (sorted by alphabet): " << endl;
for (int i = 0; i < count; i++) {
cout << high_mileage_cars[i].brand << " | " << high_mileage_cars[i].owner.name << " | " << high_mileage_cars[i].owner.surname << " | " << high_mileage_cars[i].owner.patronymic
<< " | " << high_mileage_cars[i].engine_power << " | " << high_mileage_cars[i].mileage << " | " << high_mileage_cars[i].last_service.day << " " << high_mileage_cars[i].last_service.month << " " << high_mileage_cars[i].last_service.year << endl;
}
cout << endl;

if (high_mileage_cars != nullptr) {
delete[] high_mileage_cars;
}
}

void save_data(const car *cars, int n, const string &file_name) {
ofstream output_file(file_name);
if (!output_file.is_open()) {
cerr << “Failed to open file for writing.” << endl;
return;
}

for (int i = 0; i < n; i++) {
output_file << cars[i].brand << endl
<< cars[i].owner.name << endl
<< cars[i].owner.surname << endl
<< cars[i].owner.patronymic << endl
<< cars[i].engine_power << endl
<< cars[i].mileage << endl
<< cars[i].last_service.day << endl
<< cars[i].last_service.month << endl
<< cars[i].last_service.year << endl;
}

output_file.close();
}

void free_memory(car *cars, int n) {
for (int i = 0; i < n; i++) {
delete[] cars[i].brand;
delete[] cars[i].owner.name;
delete[] cars[i].owner.surname;
delete[] cars[i].owner.patronymic;
delete[] cars[i].last_service.month;
}
}

main.cpp:45:20: error: extended character “ is not valid in an identifier
   45 | load_data(cars, n, “cars_data.txt”);
      |                    ^
main.cpp:45:31: error: extended character ” is not valid in an identifier
   45 | load_data(cars, n, “cars_data.txt”);
      |                               ^
main.cpp:50:9: error: extended character “ is not valid in an identifier
   50 | cout << “1. Add a new car” << endl;
      |         ^
main.cpp:50:23: error: extended character ” is not valid in an identifier
   50 | cout << “1. Add a new car” << endl;
      |                       ^
main.cpp:51:9: error: extended character “ is not valid in an identifier
   51 | cout << “2. Print car information” << endl;
      |         ^
main.cpp:51:23: error: extended character ” is not valid in an identifier
   51 | cout << “2. Print car information” << endl;
      |                       ^
main.cpp:52:9: error: extended character “ is not valid in an identifier
   52 | cout << “3. Find cars with more than 18 months since last service” << endl;
      |         ^
main.cpp:52:59: error: extended character ” is not valid in an identifier
   52 | cout << “3. Find cars with more than 18 months since last service” << endl;
      |                                                           ^
main.cpp:53:9: error: extended character “ is not valid in an identifier
   53 | cout << “4. Find cars of a specified brand” << endl;
      |         ^
main.cpp:53:38: error: extended character ” is not valid in an identifier
   53 | cout << “4. Find cars of a specified brand” << endl;
      |                                      ^
main.cpp:54:9: error: extended character “ is not valid in an identifier
   54 | cout << “5. Find owners with mileage greater than specified (sorted by alphabet)” << endl;
      |         ^
main.cpp:54:81: error: extended character ” is not valid in an identifier
   54 | cout << “5. Find owners with mileage greater than specified (sorted by alphabet)” << endl;
      |                                                                                 ^
main.cpp:55:9: error: extended character “ is not valid in an identifier
   55 | cout << “6. Exit” << endl;
      |         ^
main.cpp:55:13: error: extended character ” is not valid in an identifier
   55 | cout << “6. Exit” << endl;
      |             ^
main.cpp:65:28: error: extended character “ is not valid in an identifier
   65 | strcpy(current_date.month, “xx”);
      |                            ^
main.cpp:65:28: error: extended character ” is not valid in an identifier
main.cpp:108:20: error: extended character “ is not valid in an identifier
  108 | save_data(cars, n, “cars_data.txt”);
      |                    ^
main.cpp:108:31: error: extended character ” is not valid in an identifier
  108 | save_data(cars, n, “cars_data.txt”);
      |                               ^
main.cpp:119:9: error: extended character “ is not valid in an identifier
  119 | cerr << “Failed to open file for reading.” << endl;
      |         ^
main.cpp:119:42: error: extended character ” is not valid in an identifier
  119 | cerr << “Failed to open file for reading.” << endl;
      |                                          ^
main.cpp:208:24: error: extended character “ is not valid in an identifier
  208 | char *month_names[] = {“January”, “February”, “March”, “April”, “May”, “June”, “July”, “August”, “September”, “October”, “November”, “December”};
      |                        ^
main.cpp:208:24: error: extended character ” is not valid in an identifier
main.cpp:208:35: error: extended character “ is not valid in an identifier
  208 | char *month_names[] = {“January”, “February”, “March”, “April”, “May”, “June”, “July”, “August”, “September”, “October”, “November”, “December”};
      |                                   ^
main.cpp:208:35: error: extended character ” is not valid in an identifier
main.cpp:208:47: error: extended character “ is not valid in an identifier
  208 | char *month_names[] = {“January”, “February”, “March”, “April”, “May”, “June”, “July”, “August”, “September”, “October”, “November”, “December”};
      |                                               ^
main.cpp:208:47: error: extended character ” is not valid in an identifier
main.cpp:208:56: error: extended character “ is not valid in an identifier
  208 | char *month_names[] = {“January”, “February”, “March”, “April”, “May”, “June”, “July”, “August”, “September”, “October”, “November”, “December”};
      |                                                        ^
main.cpp:208:56: error: extended character ” is not valid in an identifier
main.cpp:208:65: error: extended character “ is not valid in an identifier
  208 | char *month_names[] = {“January”, “February”, “March”, “April”, “May”, “June”, “July”, “August”, “September”, “October”, “November”, “December”};
      |                                                                 ^
main.cpp:208:65: error: extended character ” is not valid in an identifier
main.cpp:208:72: error: extended character “ is not valid in an identifier
  208 | char *month_names[] = {“January”, “February”, “March”, “April”, “May”, “June”, “July”, “August”, “September”, “October”, “November”, “December”};
      |                                                                        ^
main.cpp:208:72: error: extended character ” is not valid in an identifier
main.cpp:208:80: error: extended character “ is not valid in an identifier
  208 | char *month_names[] = {“January”, “February”, “March”, “April”, “May”, “June”, “July”, “August”, “September”, “October”, “November”, “December”};
      |                                                                                ^
main.cpp:208:80: error: extended character ” is not valid in an identifier
main.cpp:208:88: error: extended character “ is not valid in an identifier
  208 | char *month_names[] = {“January”, “February”, “March”, “April”, “May”, “June”, “July”, “August”, “September”, “October”, “November”, “December”};
      |                                                                                        ^
main.cpp:208:88: error: extended character ” is not valid in an identifier
main.cpp:208:98: error: extended character “ is not valid in an identifier
  208 | char *month_names[] = {“January”, “February”, “March”, “April”, “May”, “June”, “July”, “August”, “September”, “October”, “November”, “December”};
      |                                                                                                  ^
main.cpp:208:98: error: extended character ” is not valid in an identifier
main.cpp:208:111: error: extended character “ is not valid in an identifier
  208 | char *month_names[] = {“January”, “February”, “March”, “April”, “May”, “June”, “July”, “August”, “September”, “October”, “November”, “December”};
      |                                                                                                               ^
main.cpp:208:111: error: extended character ” is not valid in an identifier
main.cpp:208:122: error: extended character “ is not valid in an identifier
  208 | char *month_names[] = {“January”, “February”, “March”, “April”, “May”, “June”, “July”, “August”, “September”, “October”, “November”, “December”};
      |                                                                                                                          ^
main.cpp:208:122: error: extended character ” is not valid in an identifier
main.cpp:208:134: error: extended character “ is not valid in an identifier
  208 | char *month_names[] = {“January”, “February”, “March”, “April”, “May”, “June”, “July”, “August”, “September”, “October”, “November”, “December”};
      |                                                                                                                                      ^
main.cpp:208:134: error: extended character ” is not valid in an identifier
main.cpp:283:9: error: extended character “ is not valid in an identifier
  283 | cerr << “Failed to open file for writing.” << endl;
      |         ^
main.cpp:283:42: error: extended character ” is not valid in an identifier
  283 | cerr << “Failed to open file for writing.” << endl;
      |                                          ^
main.cpp: In function ‘int main()’:
main.cpp:39:1: error: ‘cout’ was not declared in this scope
   39 | cout << "Enter the number of cars: ";
      | ^~~~
main.cpp:5:1: note: ‘std::cout’ is defined in header ‘’; did you forget to ‘#include ’?
    4 | #include <fstream>
  +++ |+#include <iostream>
    5 | 
main.cpp:40:1: error: ‘cin’ was not declared in this scope
   40 | cin >> n;
      | ^~~
main.cpp:40:1: note: ‘std::cin’ is defined in header ‘’; did you forget to ‘#include ’?
main.cpp:45:20: error: ‘“cars_data’ was not declared in this scope
   45 | load_data(cars, n, “cars_data.txt”);
      |                    ^~~~~~~~~~
main.cpp:50:9: error: ‘“1’ was not declared in this scope
   50 | cout << “1. Add a new car” << endl;
      |         ^~
main.cpp:51:9: error: ‘“2’ was not declared in this scope
   51 | cout << “2. Print car information” << endl;
      |         ^~
main.cpp:52:9: error: ‘“3’ was not declared in this scope
   52 | cout << “3. Find cars with more than 18 months since last service” << endl;
      |         ^~
main.cpp:53:9: error: ‘“4’ was not declared in this scope
   53 | cout << “4. Find cars of a specified brand” << endl;
      |         ^~
main.cpp:54:9: error: ‘“5’ was not declared in this scope
   54 | cout << “5. Find owners with mileage greater than specified (sorted by alphabet)” << endl;
      |         ^~
main.cpp:55:9: error: ‘“6’ was not declared in this scope
   55 | cout << “6. Exit” << endl;
      |         ^~
main.cpp:65:28: error: ‘“xx”’ was not declared in this scope
   65 | strcpy(current_date.month, “xx”);
      |                            ^~~~
main.cpp:92:5: warning: ‘char* gets(char*)’ is deprecated [-Wdeprecated-declarations]
   92 | gets(brand);
      | ~~~~^~~~~~~
In file included from /usr/include/c++/11/cstdio:42,
                 from /usr/include/c++/11/ext/string_conversions.h:43,
                 from /usr/include/c++/11/bits/basic_string.h:6608,
                 from /usr/include/c++/11/string:55,
                 from /usr/include/c++/11/bits/locale_classes.h:40,
                 from /usr/include/c++/11/bits/ios_base.h:41,
                 from /usr/include/c++/11/ios:42,
                 from /usr/include/c++/11/istream:38,
                 from /usr/include/c++/11/fstream:38,
                 from main.cpp:4:
/usr/include/stdio.h:605:14: note: declared here
  605 | extern char *gets (char *__s) __wur __attribute_deprecated__;
      |              ^~~~
main.cpp:111:10: error: ‘current_date’ was not declared in this scope
  111 | delete[] current_date.month;
      |          ^~~~~~~~~~~~
main.cpp: In function ‘void load_data(car*, int, const string&)’:
main.cpp:119:1: error: ‘cerr’ was not declared in this scope
  119 | cerr << “Failed to open file for reading.” << endl;
      | ^~~~
main.cpp:119:1: note: ‘std::cerr’ is defined in header ‘’; did you forget to ‘#include ’?
main.cpp:119:9: error: ‘“Failed’ was not declared in this scope
  119 | cerr << “Failed to open file for reading.” << endl;
      |         ^~~~~~~
main.cpp: In function ‘void add_car(car*, int&)’:
main.cpp:156:1: error: ‘cout’ was not declared in this scope
  156 | cout << "New car: " << endl;
      | ^~~~
main.cpp:156:1: note: ‘std::cout’ is defined in header ‘’; did you forget to ‘#include ’?
main.cpp:159:1: error: ‘cin’ was not declared in this scope
  159 | cin.getline(cars[i].brand, 20);
      | ^~~
main.cpp:159:1: note: ‘std::cin’ is defined in header ‘’; did you forget to ‘#include ’?
main.cpp: In function ‘void print_cars(const car*, int)’:
main.cpp:197:1: error: ‘cout’ was not declared in this scope
  197 | cout << "Car information: " << endl;
      | ^~~~
main.cpp:197:1: note: ‘std::cout’ is defined in header ‘’; did you forget to ‘#include ’?
main.cpp: In function ‘void search_cars(const car*, int, date)’:
main.cpp:206:1: error: ‘cout’ was not declared in this scope
  206 | cout << "Cars that need service: " << endl;
      | ^~~~
main.cpp:206:1: note: ‘std::cout’ is defined in header ‘’; did you forget to ‘#include ’?
main.cpp:208:24: error: ‘“January”’ was not declared in this scope
  208 | char *month_names[] = {“January”, “February”, “March”, “April”, “May”, “June”, “July”, “August”, “September”, “October”, “November”, “December”};
      |                        ^~~~~~~~~
main.cpp:208:35: error: ‘“February”’ was not declared in this scope
  208 | char *month_names[] = {“January”, “February”, “March”, “April”, “May”, “June”, “July”, “August”, “September”, “October”, “November”, “December”};
      |                                   ^~~~~~~~~~
main.cpp:208:47: error: ‘“March”’ was not declared in this scope
  208 | char *month_names[] = {“January”, “February”, “March”, “April”, “May”, “June”, “July”, “August”, “September”, “October”, “November”, “December”};
      |                                               ^~~~~~~
main.cpp:208:56: error: ‘“April”’ was not declared in this scope
  208 | char *month_names[] = {“January”, “February”, “March”, “April”, “May”, “June”, “July”, “August”, “September”, “October”, “November”, “December”};
      |                                                        ^~~~~~~
main.cpp:208:65: error: ‘“May”’ was not declared in this scope
  208 | char *month_names[] = {“January”, “February”, “March”, “April”, “May”, “June”, “July”, “August”, “September”, “October”, “November”, “December”};
      |                                                                 ^~~~~
main.cpp:208:72: error: ‘“June”’ was not declared in this scope
  208 | char *month_names[] = {“January”, “February”, “March”, “April”, “May”, “June”, “July”, “August”, “September”, “October”, “November”, “December”};
      |                                                                        ^~~~~~
main.cpp:208:80: error: ‘“July”’ was not declared in this scope
  208 | char *month_names[] = {“January”, “February”, “March”, “April”, “May”, “June”, “July”, “August”, “September”, “October”, “November”, “December”};
      |                                                                                ^~~~~~
main.cpp:208:88: error: ‘“August”’ was not declared in this scope
  208 | char *month_names[] = {“January”, “February”, “March”, “April”, “May”, “June”, “July”, “August”, “September”, “October”, “November”, “December”};
      |                                                                                        ^~~~~~~~
main.cpp:208:98: error: ‘“September”’ was not declared in this scope
  208 | char *month_names[] = {“January”, “February”, “March”, “April”, “May”, “June”, “July”, “August”, “September”, “October”, “November”, “December”};
      |                                                                                                  ^~~~~~~~~~~
main.cpp:208:111: error: ‘“October”’ was not declared in this scope
  208 | char *month_names[] = {“January”, “February”, “March”, “April”, “May”, “June”, “July”, “August”, “September”, “October”, “November”, “December”};
      |                                                                                                               ^~~~~~~~~
main.cpp:208:122: error: ‘“November”’ was not declared in this scope
  208 | char *month_names[] = {“January”, “February”, “March”, “April”, “May”, “June”, “July”, “August”, “September”, “October”, “November”, “December”};
      |                                                                                                                          ^~~~~~~~~~
main.cpp:208:134: error: ‘“December”’ was not declared in this scope
  208 | char *month_names[] = {“January”, “February”, “March”, “April”, “May”, “June”, “July”, “August”, “September”, “October”, “November”, “December”};
      |                                                                                                                                      ^~~~~~~~~~
main.cpp: In function ‘void find_brand(const car*, int, const char*)’:
main.cpp:231:1: error: ‘cout’ was not declared in this scope
  231 | cout << "Cars with the specified brand: " << endl;
      | ^~~~
main.cpp:231:1: note: ‘std::cout’ is defined in header ‘’; did you forget to ‘#include ’?
main.cpp: In function ‘void sort_print_high_mileage(car*, int, int)’:
main.cpp:268:1: error: ‘cout’ was not declared in this scope
  268 | cout << "Owners with mileage greater than the specified limit (sorted by alphabet): " << endl;
      | ^~~~
main.cpp:268:1: note: ‘std::cout’ is defined in header ‘’; did you forget to ‘#include ’?
main.cpp: In function ‘void save_data(const car*, int, const string&)’:
main.cpp:283:1: error: ‘cerr’ was not declared in this scope
  283 | cerr << “Failed to open file for writing.” << endl;
      | ^~~~
main.cpp:283:1: note: ‘std::cerr’ is defined in header ‘’; did you forget to ‘#include ’?
main.cpp:283:9: error: ‘“Failed’ was not declared in this scope
  283 | cerr << “Failed to open file for writing.” << endl;