Strange output using std::list and ifstream

Im writing a program to practice with the language, but im getting some pretty weird output from code that seems right to me.

The code:
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
#include <iostream>
#include <fstream>
#include <list>
struct Car
{
    std::string make;
    std::string model;
    int partNo;
    double price;
    int quantity;
    std::string partname;
};
void AddItem();
void _Update(int PartNo, int quantity);
void UpdateList(std::list<Car>& _Car);
int main()
{
    std::list<Car> _Car;
    UpdateList(_Car);
    for(std::list<Car>::iterator iter = _Car.begin(); iter != _Car.end(); iter++)
     {
         std::cout << iter->make << " " << iter->model << " " << iter->partNo << " " << iter->price << " " << iter->quantity << " " << iter->partname << std::endl;
     }
}

void UpdateList(std::list<Car>& _Car)
{
    std::ifstream File("CarParts.txt");
    if(!File.is_open())
        std::cerr << "Bad file input....... closing....";

    while(!File.eof())
    {
        Car tempObj;
        File >> tempObj.make >> tempObj.model >> tempObj.partNo >> tempObj.price >> tempObj.quantity;
        getline(File,tempObj.partname);
        _Car.push_back(tempObj);
    }
    File.close();

}


Outpost given:

Pajero NA1H25 1 3.65 11 BLADE W/S WIPER
Honda_Sivic R34gFk 2 4.97 15 ENGINE CHANGE
   2 4.97 15


Notepad file:

Pajero NA1H25 1 3.65 11 BLADE W/S WIPER
HondaSivic R34gFk 2 4.97 15 ENGINE CHANGE


what is with the three numbers under the two lines i actually wanted printed out? It's really confusing me... Thanks if you can help!
Last edited on
Still need help!
You are looping on eof, which is an error. eof is not set until you attempt to read from the file after it is completely read.
What should i use to replace the eof?
Try

1
2
3
4
5
6
7
for(Car tempObj; 
      File >> tempObj.make >> tempObj.model >> tempObj.partNo
           >> tempObj.price >> tempObj.quantity
      && getline(File, tempObj.partname) ; )
    {
        _Car.push_back(tempObj);
    }


Or, better yet, give your Car an operator>>
Last edited on
how would that for loop run? wouldn't it just run until it gets an error? Also, what could i do if a gave the Car an operator>>? I appreciate the help, please excuse my lack of knowledge!
@Need4Sleep just the way you thought your loop ran. It stops when it reaches the end of file.

If you gave Car an operator>>, you could write simpler loops:

1
2
for(Car tempObj; File >> tempObj; )
    _Car.push_back(tempObj);

Or, you could create containers directly from input streams (through iterators):

1
2
3
ifstram File("CarParts.txt");
istream_iterator<Car> beg(File), end;
list<Car> _Car(beg, end);
Last edited on
If car is not in a class, how would i give it the operator? i thought i could only use the overloading in a class
A struct can have constructors, member functions, operator overloads, etc, just like any other class.
Topic archived. No new replies allowed.