When I try to display all elements in the vector, it only display name of dealer, and number of dealer, but number of cars is MISSING! So how do I display the whole vector completely?
I declare a vector in Main and pass it to the readIn and display function. or is there something wrong with the push_back?
however when I use debugger
The result Suppose be like:
John Elway Dodge 123 2
Tesla Cherry Creek 234 1
However it is now like:
John Elway Dodge 123
Tesla Cherry Creek 234
The "2" and the "1" is missing for some reason. Here is my code. Thankyou in advance.
#include <fstream>
#include <iostream>
#include <vector>
#include <string>
using namespace std;
class Dealer
{
private:
string dealerName;
int dealerNumber;
int numberOfCars;
public:
Dealer();
Dealer(string _dealerName, int _dealerNumber, int _numberOfCars);
void readIn(vector <Dealer> &tempDealerOne);
};
void display(vector <Dealer> &tempDealerOne);
int main()
{
vector <Dealer> dealerOne;
Dealer dealer1;
dealer1.readIn(dealerOne);
display(dealerOne);
}
// function to read in data
void Dealer::readIn (vector <Dealer> &tempDealerOne)
{
const int size = 23;
ifstream infile;
infile.open("in.txt");
string tempName[size];
int tempDealerNum[size],tempCarNum[size];
Dealer temp1;
for(int i = 0; i < size; i++)
{
getline(infile,tempName[i]);
if(i == 0)
{
infile >> tempDealerNum[i];
infile >> tempCarNum[i];
temp1.dealerName = tempName[i];
temp1.dealerNumber = tempDealerNum[i];
// this
temp1.numberOfCars = tempCarNum[i];
// assign the data to vector
tempDealerOne.push_back(temp1);
}
if(i == 14)
{
infile >> tempDealerNum[i];
infile >> tempCarNum[i];
temp1.dealerName = tempName[i];
temp1.dealerNumber = tempDealerNum[i];
temp1.numberOfCars = tempCarNum[i];
// assign the data to vector
tempDealerOne.push_back(temp1);
}
}
// read in data store in vector
// then pass it to Dealer class
infile.close();
}
void display(vector <Dealer> &tempDealerOne)
{
// what is the problem
for(int i = 0; i < tempDealerOne.size(); i++)
cout << tempDealerOne[0]<< "\n";
}
And here is the textfile
John Elway Dodge
123
2
*****
*****
*****
*****
*****
*****
*****
*****
*****
*****
Tesla Cherry Creek
234
1
*****
*****
*****
*****
*****