Sorting Vector and Modifying

Hi!
Its my first time posting here so I don't know how to format this question.

I would like to add the option of "5. Choose a Dealer Number, List Cars and Modify a Car" but I don't know how to go about it.

I suppose the file I need help with is this one

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
#ifndef Car_h
#define Car_h
#include "Dealer.h"
#include <vector>
#include <fstream>
#include <string>
using namespace std;

//option 1 in main, pass empty vector by reference to change its contents
void loadVector(std::vector<Dealer> &dealers){

    //temp Car object and its associated data
    Car tempCar;
    std::string tempVin;
    std::string tempMake;
    std::string tempModel;
    int tempYear;
    double tempPrice;

    //temp Dealer object and its associated data
    Dealer tempDealer;
    Car *carPtr;
    std::string dealerName;
    int dealerNum, numOfCars;

    std::fstream inFile;//input file variable to read in data from in.txt

    //open in.txt and pass info into vector
    inFile.open("[FILE PATH]//in.txt");
    while(inFile.good()){

        //get Dealer info from in.txt
        getline(inFile, dealerName);
        inFile >> dealerNum;
        inFile >> numOfCars;
        inFile.ignore();

        //set temp Dealer object data to file data
        tempDealer.setDealerName(dealerName);
        tempDealer.setDealerNumber(dealerNum);
        tempDealer.setNumOfCars(numOfCars);

        //use empty Car object pointer to create dynamic array of Car objects
        carPtr = new Car[numOfCars];

        //loop through the Dealer object's number of cars, and store each Car object in the dynamic array
        for (int i = 0; i < numOfCars; i++) {
            getline(inFile, tempVin);
            getline(inFile, tempMake);
            getline(inFile, tempModel);
            inFile >> tempYear;
            inFile >> tempPrice;
            inFile.ignore();

            tempCar.setVin(tempVin);
            tempCar.setMake(tempMake);
            tempCar.setModel(tempModel);
            tempCar.setYear(tempYear);
            tempCar.setPrice(tempPrice);

            carPtr[i] = tempCar;//store each new Car object in the dynamic array
        }//for

        tempDealer.setCarArrayPtr(carPtr);//set Dealer object's car pointer to carPtr

        dealers.push_back(tempDealer);//add each dealer to the vector

    }//while
    std::cout << "Load successful.\n";
    std::cout << "Read in " << dealers.size() << " dealers." << std::endl;

    inFile.close();//close file after reading in data
}

//option 2, display dealers
void showDealers(std::vector<Dealer> dealers){

    for (int i = 0; i < dealers.size() ; i++) {
        std::cout << dealers[i];
    }
}

//option 3, choose a dealer and display that dealer's cars
void displayCars(std::vector<Dealer> dealers){
    //variables to hold which dealer user wants to search and an index for its position
    int dealerNum, dealerIndex = 0;
    Car *carArrayPtr;//Car pointer to store dealer object's pointer
    bool found = false;

    //search for dealer number
    std::cout << "Enter the dealer number who's cars you would like to see.\n";
    std::cin >> dealerNum;
    std::cin.ignore();
    for (int i = 0; i < dealers.size(); i++) {
        if (dealers[i].getDealerNumber() == dealerNum ) {
            dealerIndex = i;
            found = true;
        }//if
    }//for
    if (!found) {
        std::cout << "Sorry, that dealer is not found.\n";//dealer number not found
    }
    else
    {
        //set dummy pointer to the dealer's pointer
        carArrayPtr = dealers[dealerIndex].getCarArrayPtr();

        //display dealer's cars
        for (int i = 0; i < dealers[dealerIndex].getNumOfCars(); i++) {
            std::cout << carArrayPtr[i];
        }//for
    }//else
}

//option 4, choose dealer and add a car
void addCar(std::vector<Dealer> &dealers){
    //variables to hold which dealer user wants to search and an index for its position
    int dealerNum, dealerIndex = 0;
    Car *carArrayPtr;
    Car carToAdd;
    bool found = false;

    //new car info
    std::string vin;
    std::string make;
    std::string model;
    int year;
    double price;

    //find out what dealer the user wants to add to
    std::cout << "Enter the dealer number that you'd like to add a car to.\n";
    std::cin >> dealerNum;
    std::cin.ignore();
    for (int i = 0; i < dealers.size(); i++) {
        if (dealers[i].getDealerNumber() == dealerNum ) {
            dealerIndex = i;
            found = true;
        }//if
    }//for
    if (!found) {
        std::cout << "Sorry, that dealer is not found.\n";
    }
    else
    {
        //get new car info from user
        std::cout << "Enter the car VIN.\n";
        getline(std::cin, vin);
        std::cout << "Enter the car make.\n";
        getline(std::cin, make);
        std::cout << "Enter the car model.\n";
        getline(std::cin, model);
        std::cout << "Enter the car year.\n";
        std::cin >> year;
        std::cout << "Enter the car price.\n";
        std::cin >> price;
        std::cin.ignore();
        carToAdd.setVin(vin);
        carToAdd.setMake(make);
        carToAdd.setModel(model);
        carToAdd.setYear(year);
        carToAdd.setPrice(price);

        int setFlag = (dealers[dealerIndex].getNumOfCars() + 1);//for adding 1 to dealer object's dynamic array
        carArrayPtr = new Car[setFlag];//create new dynamic array 1 size bigger than previous, to hold new car

        //copy Dealer object's dynamic array info
        for (int i = 0; i < dealers[dealerIndex].getNumOfCars(); i++) {
            carArrayPtr[i] = dealers[dealerIndex].getCarArrayPtr()[i];
        }

        carArrayPtr[setFlag - 1] = carToAdd;//add new Car objec to dynamic array

        dealers[dealerIndex].setCarArrayPtr(carArrayPtr);//set Dealer object's carArrayPtr to the new dynamic array containing new car
        int newNumCars = (dealers[dealerIndex].getNumOfCars() + 1);//increment Dealer object's number of cars
        dealers[dealerIndex].setNumOfCars(newNumCars);//set Dealer object's numOfCars to the new number

    }//else
}

//option 5, sort cars by VIN in ascending order
void bubbleSort(std::vector<Dealer> &dealers){

    Car tempCar;//temp Scores object to hold sorted value
    int dealerNum, dealerIndex;//variables to find biggest VIN in array and its index
    bool found = true;//flag for whether dealer was found or not
    Car *carArrayPtr = nullptr;//dummy pointer to hold dynamic array info
    std::string maxElement;

    std::cout << "Enter the dealer number that you'd like to sort the cars by VIN.\n";
    std::cin >> dealerNum;
    std::cin.ignore();
    for (int i = 0; i < dealers.size(); i++) {
        if (dealers[i].getDealerNumber() == dealerNum ) {
            dealerIndex = i;
            found = true;
        }//if
    }//for
    if (!found) {
        std::cout << "Sorry, that dealer is not found.\n";
    }
    else
    {
        carArrayPtr = new Car[dealers[dealerIndex].getNumOfCars()];

        for (int i = 0; i < dealers[dealerIndex].getNumOfCars(); i++) {
            carArrayPtr[i] = dealers[dealerIndex].getCarArrayPtr()[i];
        }//for

        int flag;//used to determine dynamic array elements

        for (int j = 0; j < dealers[dealerIndex].getNumOfCars() - 1; j++) {
            for (flag = 0; flag < dealers[dealerIndex].getNumOfCars() - 1 - j; flag++) {
                if (carArrayPtr[flag].getVin() > carArrayPtr[flag + 1].getVin()) {
                    std::swap(carArrayPtr[flag], carArrayPtr[flag + 1]);
                }//if
            }//for
        }//for
    }//else
    dealers[dealerIndex].setCarArrayPtr(carArrayPtr);
}

//used for bubble sort in option 5
void swap(Car &a, Car &b){
    Car temp = a;
    a = b;
    b = temp;
}

//option 6, write vector of dealer object's and their cars to new file
void sendFileout(std::vector<Dealer> &dealers){

    //variable for sending data to file
    std::ofstream outFile;

    //add the new response and type to a new file
    outFile.open("[FILE PATH]//new.txt", std::fstream::app);

    //start loop to get dealer's info, and then the dealer's cars
    for (int i = 0; i < dealers.size(); i++) {

        //send dealer's info to new file
        outFile << dealers[i].getDealerName() << std::endl;
        outFile << dealers[i].getDealerNumber() << std::endl;
        outFile << dealers[i].getNumOfCars() << std::endl;

        //send each car's info to the file in order
        for (int j = 0; j < dealers[i].getNumOfCars(); j++) {
            outFile << dealers[i].getCarArrayPtr()[j];
        }//for
    }//for

    outFile.close();//close file
}

#endif /* Car_h */ 
Last edited on
From the remark of line 180 you did actually implement option 5?
Topic archived. No new replies allowed.