0

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 enter image description here

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
*****
*****
*****
*****
*****
1201ProgramAlarm
  • 32,384
  • 7
  • 42
  • 56
Lin
  • 1
  • In the debugger, the "2" and "1" are in the vector dealerOne, they are under the numberOfCars colomn! – Lin Dec 02 '19 at 02:17
  • 2
    The problem is probably in the `std::ostream& operator<<(std::ostream&, Dealer)` function that you are not showing us. – n314159 Dec 02 '19 at 02:20
  • `for(int i = 0; i < tempDealerOne.size(); i++) cout << tempDealerOne[0]<< "\n";` Why are you printing the 0th item of the vector multiple times? Shouldn't it be `for(int i = 0; i < tempDealerOne.size(); i++) cout << tempDealerOne[i]<< "\n";` – Jerry Jeremiah Dec 02 '19 at 02:26
  • The `display()` function is outputting `tempDealerOne[0]` repeatedly - look carefully at what is in the `[]`. It doesn't display anything else. Voting to close as a typo. – Peter Dec 02 '19 at 02:26

0 Answers0