-1

I would like to read data from a filename that the user specifies into a vector of objects. There are five different member variables per vector element that I would like to read in. In the file, there will multiple entries (groups of five member variables) that must be read into each vector element. Here is my (incomplete) code so far:

while (!inputFile.eof())
            {
                for (unsigned int count = 0; inputFile.eof(); count++)
                {
                    cout << "Vehicle #" << (count + 1) << endl;

                    inputFile >> temp[count].setVIN();                              
                    cout << "VIN: " << temp[count].getVIN() << endl;            
                    inputFile >> temp[count].setMake() << endl;                 
                    cout << "Make: " << temp[count].getMake() << endl;          
                    inputFile >> temp[count].setModel() << endl;                
                    cout << "Model: " << temp[count].getModel() << endl;        
                    inputFile >> temp[count].setYear() << endl;                 
                    cout << "Year: " << temp[count].getYear() << endl;          
                    inputFile >> temp[count].setPrice() << endl;                
                    cout << "Price: " << temp[count].getPrice() << endl         
                         << endl;
                }
            }

However, there are several problems with this code already. One of which is that the setVIN(), setMake(), setModel(), setYear(), and setPrice member functions require one argument (the value in which to set VIN, Make, Model, etc). Here is the class declaration:

class Vehicle
{
    private:

        string VIN;
        string make;
        string model;
        int    year;
        double price;

    public:
        Vehicle(string, string, string, int, double);
        Vehicle();
        string getVIN();
        string getMake();
        string getModel();
        int    getYear();
        double getPrice();
        void   setVIN(string);
        void   setMake(string);
        void   setModel(string);
        void   setYear(int);
        void   setPrice(double);
 };

Lastly, given the first block of code I posted, on the lines that have inputFile >> ..... an error message states "no operand '>>' matches these operands operand types are std::ifstream >> void"

Could anybody help me get through this road block?

Thanks!

jshapy8
  • 1,733
  • 5
  • 23
  • 51
  • 1
    [`while (!inputFile.eof())`](http://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-considered-wrong) – πάντα ῥεῖ Apr 29 '15 at 00:58
  • Also, how is `inputFile >> temp[count].setVIN();` supposed to work? The actual signature is `void setVIN(string);` Which expects a `std::string` as parameter an returns `void`. That's by no means a suitable syntax to call this function. – πάντα ῥεῖ Apr 29 '15 at 01:03
  • Make a function that reads one object, and then if the read is successful put that object into the vector. – M.M Apr 29 '15 at 02:22
  • It would help to paste an example of the input file – M.M Apr 29 '15 at 02:23

1 Answers1

2

Firstly this code is bad.

inputFile >> temp[count].getVIN();

It gets a string from getVIN() and then tries to read into the temporary string. you instead need to use something like:

string vin;
inputFile >> vin;
temp[count].setVin(vin); 

Secondly its more ideomatic to create an operator>> that reads the whole object so that your loops can be cleaner.

istream& operator>>(istream& is, Vehicle & v) {
   string vin;
   inputFile >> vin;
   v.setVin(vin);
   ...
}

And if you make this a member function you can instead write

// You probably want to add error checking to each read too
void Vehicle::readFromStream(istream & is) {
   is>>vin;
   is>>make;
   ...
}


istream& operator>>(istream& is, Vehicle & v) {
   v.readFromStream(is);
   return is;
}

Then your loop will become

Vechicle v;
while(input>>v) {
   std::cout<<v<<std::endl;
}

So long as you add a sensible operator<< too (in the same way.)

If you really want to store them in a list then:

std::vector<Vehicle> vehicles;
Vehicle v;
while(input>>v) {
  vehicles.push_back(v);
}
Michael Anderson
  • 61,385
  • 7
  • 119
  • 164