Skip to content
TheChatArchiveThe Chat Archive
gpt-3.5-turbo-0301· OpenAIAi2 WildChat-4.8M (AI2)

измени getline на gets void add_car(car *cars, int &n) { for (int i = 0; i < n; i++) {…

  1. User

    измени getline на gets 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; } }

  2. Assistant

    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]; gets(cars[i].brand);

        cout << "Owner’s name: ";
        cars[i].owner.name = new char[20];
        gets(cars[i].owner.name);
    
        cout << "Owner’s surname: ";
        cars[i].owner.surname = new char[20];
        gets(cars[i].owner.surname);
    
        cout << "Owner’s patronymic: ";
        cars[i].owner.patronymic = new char[20];
        gets(cars[i].owner.patronymic);
    
        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();
        gets(cars[i].last_service.month);
    
        cars[i].last_service.year = 0;
        cin >> cars[i].last_service.year;
    
        cout << endl;
    }
    

    }

  3. User

    поменяй getline на gets 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(); }

  4. Assistant

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

    cars[i].owner.name = new char[20];
    gets(cars[i].owner.name);
    
    cars[i].owner.surname = new char[20];
    gets(cars[i].owner.surname);
    
    cars[i].owner.patronymic = new char[20];
    gets(cars[i].owner.patronymic);
    
    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.ignore();
    gets(cars[i].last_service.month);
    
    cars[i].last_service.year = 0;
    input_file >> cars[i].last_service.year;
    
    input_file.ignore();
    

    }

    input_file.close(); }

  5. User

    можешь сделать без bool? 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();

    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; } }

  6. Assistant

    while (true) { 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();

    switch (choice) {
        case 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;
            break;
        }
        case 2: {
            print_cars(cars, n);
            break;
        }
        case 3: {
            search_cars(cars, n, current_date);
            break;
        }
        case 4: {
            char *brand = new char[20];
            cout << "Enter the brand to search for: ";
            gets(brand);
            find_brand(cars, n, brand);
            delete[] brand;
            break;
        }
        case 5: {
            int mileage_limit;
            cout << "Enter the mileage limit: ";
            cin >> mileage_limit;
            cin.ignore();
            sort_print_high_mileage(cars, n, mileage_limit);
            break;
        }
        case 6: {
            return 0;
        }
        default: {
            cout << "Invalid choice. Please choose again." << endl;
            break;
        }
    }
    

    }