简体   繁体   中英

How to add new objects to vector in C++

I am trying to create a program which allows a user to add an object of class called Vehicle to an inventory that is stored in a vector . The vector has an initial size of zero. Each object is a vehicle that the user enters the attributes of.

What I cannot wrap my head around is how to let the user keep adding vehicles if each vehicle needs to be its own separate object. How do you let C++ determine what the name of new objects should be if the user decides to keep adding more vehicles (objects) to the inventory ( vector called carList ).

Could someone guide me in the right direction? I apologize if this is obvious, I am new to the language. Must I do something that involves dynamically allocating objects or something similar?

Here is my (incomplete) code:

void addVehicle(vector<Vehicle> carList)
{
    char   stop;            // Needed for stopping the do-while loop 
    string VIN = "",        // Needed to hold user input for the VIN
           Make = "",       // Needed to hold user input for the Make
           Model = "";      // Needed to hold user input for the Model
    int    Year = 0;        // Needed to hold user input for the Year
    double Price = 0.0;     // Needed to hold user input for the Price

    cout << "You have chosen to add a vehicle to your inventory.\n\n";

    do
    {
        cout << "There are currently " << carList.size() << " vehicles in your inventory.\n\n"
             << "\t\t\tVehicle #" << (carList.size() + 1) << ": \n"
             << "\t\t___________________________\n\n";

        Vehicle /*new object needs to go here*/
        carList.push_back(/*new object from the line above*/);

        // Prompt user to input VIN
        cout << "VIN: ";
        cin >> VIN;

        // Prompt user to input Make
        cout << "Make: ";
        cin.ignore(); 
        getline(cin, Make);

        // Prompt user to input Model
        cout << "Model: ";
        getline(cin, Model);

        // Prompt user to input Year
        cout << "Year: ";
        cin >> Year;

        // Prompt user to input Price
        cout << "Price: $";
        cin >> Price;

        Call to the overloaded constructor to store user input in object
        /*(newly created object)*/.Vehicle::Vehicle(VIN, Make, Model, Year, Price);

        // Ask user if they would like to enter another vehicle
        cout << "\nWould you like to enter another vehicle? (Y/N):";
        cin.ignore();
        stop = cin.get();

    } while (stop != 'N');

}

Any help would be appreciated. Thanks!

How about you first create the object and then push a copy into the vector?

Call to the overloaded constructor to store user input in object
Vehicle temp(VIN, Make, Model, Year, Price);
carList.push_back(temp);

But there's no need for the variable, really:

Call to the overloaded constructor to store user input in object
carList.push_back(Vehicle(VIN, Make, Model, Year, Price));

And if you have C++11, you can even construct the object directly in place:

Call to the overloaded constructor to store user input in object
carList.emplace_back(VIN, Make, Model, Year, Price);

Look ma, no copies!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM