void readFile(vector &dealerVector) { ifstream infile; infile.open(\"in.txt\");
ID: 3721062 • Letter: V
Question
void readFile(vector &dealerVector) { ifstream infile; infile.open("in.txt"); if (!infile) { cout<<"No file was found"; } else { string VIN, make, model, dealerName; int year, dealerNumber; int numberOfCars; double price; Dealer dealer; Car newCar; while (!infile.eof()) { getline(infile, dealerName); dealer.setDealerName(dealerName); infile >> dealerNumber; dealer.setDealerNumber(dealerNumber); infile >> numberOfCars; dealer.setNumberOfCars(numberOfCars); infile.ignore(); Car *aCar = new Car[numberOfCars]; for (int i = 0; i < numberOfCars; i++) { getline(infile, VIN); newCar.setVIN(VIN); getline(infile, make); newCar.setMake(make); getline(infile, model); newCar.setModel(model); infile >> year; newCar.setYear(year); infile >> price; newCar.setPrice(price); aCar[i] = Car(VIN, make, model, year, price); }//for dealer.setCarArrayPtr(aCar); dealerVector.push_back(dealer); }//while infile.close(); }//else }//function
My question is regarding my code I have written above. The basic idea of what is happening here is that I am reading in information from a file. The information is for dealerships that have cars. So I have a class Car and a class Dealer. All of the dealer objects contain a dynamic array of type Car. All of objects of type Dealer are placed into a vector. The code above (visual studios, c++) compiles without error. And when I run debugging I can see that my array is being propagated. As well as the vector. However when I actually run this function nothing happens. Any ideas of what is wrong would be great. This function is a menu function in a program that reads in data from a file, places the data into vector and dynamic array as I explained above. And the rest of my functions simply manipulate the data in different ways. However my readFile() function is not working properly. Thanks!
Explanation / Answer
HI,
In the given code.
1. When looping through the file record, should create dealer object each time. So declaration should be inside while loop.
2. Car array object is created but not assinged this value to dealer car object. That assingment is missing.
Please refer updated code below.
Please ingnore class Car and class Dealer those are just to check if code compiles successfully or not.
Find updated read file function.
COPY TO CODE:
#include <iostream>
#include <string>
#include <fstream>
#include <vector>
using namespace std;
class Car{
public:
setVIN(string){}
setMake(string){}
setModel(string){}
setYear(int){}
setPrice(double){}
Car(string,string,string,int,double){
}
Car(){
}
};
class Dealer{
private :
public:
Car * cars;
setDealerName(string){}
setDealerNumber(int){}
setNumberOfCars(int){}
};
void readFile(vector<Dealer> &dealerVector)
{
ifstream infile;
infile.open("in.txt");
if (!infile)
{ cout<<"No file was found";
} else{
string VIN, make, model, dealerName;
int year, dealerNumber;
int numberOfCars;
double price;
while (!infile.eof()){
Dealer dealer;
Car newCar;
getline(infile, dealerName);
dealer.setDealerName(dealerName);
infile >> dealerNumber;
dealer.setDealerNumber(dealerNumber);
infile >> numberOfCars;
dealer.setNumberOfCars(numberOfCars);
infile.ignore();
Car *aCar = new Car[numberOfCars];
for (int i = 0;i < numberOfCars;i++){
getline(infile, VIN);
newCar.setVIN(VIN);
getline(infile, make);
newCar.setMake(make);
getline(infile, model);
newCar.setModel(model);
infile >> year;
newCar.setYear(year);
infile >> price;
newCar.setPrice(price);
aCar[i] = Car(VIN, make, model, year, price);
}//for dealer.setCarArrayPtr(aCar);
dealer.cars = aCar;
dealerVector.push_back(dealer);
}//while infile.close();
}//else
}//function
int main()
{
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.