How to modify a nested struct on a vector?

后端 未结 3 815
粉色の甜心
粉色の甜心 2021-01-29 10:20

I am working on a program that keeps inventory of vehicles, so I created a struct for it. It also needs to keep a list of the drivers, so I created a nested struct for that. Her

3条回答
  •  庸人自扰
    2021-01-29 10:51

    #include 
    #include 
    
    using namespace std;
    
     struct Driver
     {
          string Name;
          int Code;
          int Id;
    };
    
    struct Vehicle
    {
       string License;
       string Place;
       int Capacity;
       ///from my opinion driver should be declare like this
       Driver driver;
    
    };
    
    vector  vtnewV ;
    /// the vector need to declare outside the void
    /// else it will keep recreate a new vector
    void AddVehicle()
    {
       Vehicle newV;
    
       cout << "Enter license plate number: " << endl;
       cin >> newV.License;
       cout << "Enter the vehicle's ubication: " << endl;
       cin >> newV.Place;
       cout << "Enter the vehicle's capacity: " << endl;
       cin >> newV.Capacity;
       cout << "Enter the driver's name: " << endl;
       cin >> newV.driver.Name;
       cout << "Enter the driver's code: " << endl;
       cin >> newV.driver.Code;
       cout << "Enter the driver's identification number: " << endl;
       cin >> newV.driver.Id;
       vtnewV.push_back(newV);
    };
    void ShowInfo()
    {
        for(int i= 0; i

    I have modify and added some comment to your code base by my own opinion hope that will solve you problem

提交回复
热议问题