1

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:

#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!

Syntactic Fructose
  • 18,936
  • 23
  • 91
  • 177

2 Answers2

2

This is a common issue people run into while reading file data in C++. The issue is your usage of eof. That flag is only set after an attempt to read data has failed.

Because of that, after reading the first two lines, it still has not hit the end of the file. It has read right up to it, but eof has not been set. Then it will loop around a third time, attempt to read 2 lines, and then exit after that. The problem is that you never check for eof before pushing the results of that third loop into your car list.

In your case, you can either move your eof check to after the getline call, or make use of the getline return value.

For example:

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

This will check whether the data was read successfully before pushing.

loganfsmyth
  • 156,129
  • 30
  • 331
  • 251
-3

For starters, set the namespace of the program to std to avoid the nasty std::string etc syntax.

Near the top of your file, under your imports, insert: using namespace std;

It looks like the objective is to populate a list from a file.

Looking at this part:

 File >> tempObj.make >> tempObj.model >> tempObj.partNo >> tempObj.price >> tempObj.quantity;
 getline(File,tempObj.partname);

I think a better solution would be to parse each line withgetLine, and then parse the data accordingly with a space delimeter or a comma. Whitespace delimiters can be finiky.

roguequery
  • 964
  • 13
  • 28
  • 3
    `Near the top of your file, under your imports, insert: using namespace std;` That is very bad advice and can introduce bugs. – Jesse Good May 22 '12 at 05:03
  • with getLine() callz, you are putting the entire line into the variable tempObj.partname. – roguequery May 22 '12 at 05:03
  • 1
    Why is using std namespace a bad practice? It creates cleaner code and a common namespace to work from if you aren't implementing anything no included in the std lib. – roguequery May 22 '12 at 05:05
  • 3
    Please read the most-upvoted answer [here](http://stackoverflow.com/questions/1265039/using-std-namespace). I recommend to **never** use it. – Jesse Good May 22 '12 at 05:07
  • 2
    No, use the "nasty" `std::` syntax. You get used to it quickly, it's not a lot of typing, and it makes the code more readable (I can see immediately at a glance whether a type is from the standard library, or some home-made one. It also avoids name collisions. If you reeeeally want to, you can put a `using namespace std` at the top of your .cpp file, but *never* in a header. And even in the .cpp file, i find you're better off without it. Oh, and there are no such things as "imports" in C++. It's not just a different name, what it *does* is different from "importing a library" – jalf May 22 '12 at 07:27