We have this code. You need to edit car structure doing this and earlier created…
- User
We have this code. You need to edit car structure doing this and earlier created functions should work correctly: char* car brand field FIO (First name, last name, patronymic) int engine power int Mileage field DATE (date of last maintenance) (date structure: number (int), month(char*), year (int)) (use typedef): #include #include #include using namespace std; struct car { char *brand; char *surname; char *name; int power; int mileage; };
void add_car(car *arr, int &n) { cout << “Enter car brand:” << endl; char brand[1000]; gets(brand); arr[n].brand = new char[strlen(brand) + 1]; strcpy(arr[n].brand, brand); cout << “Enter owner’s surname:” << endl; char surname[1000]; gets(surname); arr[n].surname = new char[strlen(surname) + 1]; strcpy(arr[n].surname, surname); cout << “Enter owner’s name:” << endl; char name[1000]; gets(name); arr[n].name = new char[strlen(name) + 1]; strcpy(arr[n].name, name); cout << “Enter engine power:” << endl; cin >> arr[n].power; cout << “Enter mileage:” << endl; cin >> arr[n].mileage; n++; }
void print(car *arr, int n) { cout << “Car brand | Owner’s surname | Owner’s name | Engine power | Mileage” << endl; for (int i = 0; i < n; i++) { cout << arr[i].brand << " | " << arr[i].surname << " | " << arr[i].name << " | " << arr[i].power << " | " << arr[i].mileage << endl; } }
void find_brand(car *arr, int n) { cout << “Enter car brand to find:” << endl; char brand[1000]; gets(brand); cout << "Cars of brand " << brand << “:” << endl; for (int i = 0; i < n; i++) { if (strcmp(arr[i].brand, brand) == 0) { cout << arr[i].surname << " " << arr[i].name << endl; } } }
void find_mileage(car *arr, int n) { cout << “Enter mileage value:” << endl; int mileage; cin >> mileage; int count = 0; for (int i = 0; i < n; i++) { if (arr[i].mileage > mileage) { count++; } } if (count == 0) { cout << "No cars with mileage greater than " << mileage << endl; return; } car *new_arr = new car[count]; int index = 0; for (int i = 0; i < n; i++) { if (arr[i].mileage > mileage) { new_arr[index] = arr[i]; index++; } } for (int i = 0; i < count - 1; i++) { for (int j = 0; j < count - i - 1; j++) { if (strcmp(new_arr[j].surname, new_arr[j + 1].surname) > 0) { car tmp = new_arr[j]; new_arr[j] = new_arr[j + 1]; new_arr[j + 1] = tmp; } } } cout << "Owners of cars with mileage greater than " << mileage << “:” << endl; for (int i = 0; i < count; i++) { cout << new_arr[i].surname << " " << new_arr[i].name << endl; } delete[] new_arr; }
int main() { int n = 0; car *arr = new car[1000]; while (true) { cout << endl << “Select an action:” << endl; cout << “1. Add a new car” << endl; cout << “2. Print car information” << endl; cout << “3. Find all cars of a certain brand” << endl; cout << “4. Find all owners of cars with a mileage greater than a given value” << endl; cout << “5. Exit the program” << endl; int choice; cin >> choice; cin.get(); switch (choice) { case 1: add_car(arr, n); break; case 2: print(arr, n); break; case 3: find_brand(arr, n); break; case 4: find_mileage(arr, n); break; case 5: delete[] arr; return 0; break; default: cout << “Error!” << endl; break; } } return 0; }
- Assistant
One way to edit the car structure to include the FIO and DATE fields as described would be:
typedef struct { int number; char *month; int year; } date;
typedef struct { char *firstName; char *lastName; char *patronymic; } FIO;
typedef struct { char *brand; FIO owner; int enginePower; int mileage; date lastMaintenanceDate; } car;
- User
error: ‘struct car’ has no member named ‘surname’ 129 | if (strcmp(new_arr[j].surname, new_arr[j + 1].surname) > 0) | ^~~~~~~ main.cpp:140:20: error: ‘struct car’ has no member named ‘surname’ 140 | cout << new_arr[i].surname << " " << new_arr[i].name << endl; | ^~~~~~~ main.cpp:140:49: error: ‘struct car’ has no member named ‘name’ 140 | cout << new_arr[i].surname << " " << new_arr[i].name << endl;
- Assistant
The errors indicate that the fields
surnameandnameare no longer part of thecarstructure, since we replaced them with theownerfield that contains theFIOstructure. To fix the errors, we need to update the affected code to use the new structure:In the
find_mileage()function, the nested for loop that sorts thenew_arrarray should be updated to use theowner.lastNamefield:codefor (int i = 0; i < count - 1; i++) { for (int j = 0; j < count - i - 1; j++) { if (strcmp(new_arr[j].owner.lastName, new_arr[j + 1].owner.lastName) > 0) { car tmp = new_arr[j]; new_arr[j] = new_arr[j + 1]; new_arr[j + 1] = tmp; } } }In the same function, the
coutstatement that prints the owner's surname and name should be updated to use theownerfield:codecout << new_arr[i].owner.lastName << " " << new_arr[i].owner.firstName[0] << ". " << new_arr[i].owner.patronymic[0] << "." << endl;
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.