file being read twice

Hey all,

I am a C++ newbie, and am working on some code. My code has several functions, one of which pulls data out of a file and puts it into some vectors I have set up.

The data in the file is in the following format:

int
string
int double double
int
string
int double double
.
.
.

It repeats that same progression several times.

Now ... everything works great, except that for some reason my code pulls out the last set of values twice.

So, if the string values in the file are Bob, James, and Stacy my string vector ends up with the following: [Bob, James, Stacy, Stacy].

I have no idea why the code would be pulling out the last items twice ... any ideas?

Its not a huge deal, I can just ignore the last position of the vector in all future code ... but I want to understand why its happening.

The code is below:

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
void input(ifstream& inp, vector<int>& itemID, vector<string>& itemName, 
vector<int>& pOrdered, vector<int>& pInStore, vector<int>& pSold, 
vector<double>& manufPrice, vector<double>& sellingPrice)
{
	string item;
	int id;
	int order;
	double manuf;
	double selling;

	while (inp)
	{
		inp >> id;
		inp.ignore(256, '\n');
		getline(inp, item);
		inp >> order >> manuf >> selling;
	
		itemID.push_back(id);
		itemName.push_back(item);
		pOrdered.push_back(order);
		pInStore.push_back(order);
		pSold.push_back(0);
		manufPrice.push_back(manuf);
		sellingPrice.push_back(selling);
	}
}
Last edited on
bump. :)
bump :)
[code] "Please use code tags" [/code]
I suggest that you use a struct to hold the data, as it is related. The code will be more clear too.
1
2
3
while( inp )
{
  inp >> id; //¿what if you reach eof here? 
However in that case the repeated element will be the last (actually I think it is undefined behaviour)

Please be patient.
Sorry, its my first time here and I'm not sure how often to bump things. It differs from forum to forum. :)

Thanks for the suggestion (and I didn't know about code tags, sorry!).

The file I'm using is set up so that it won't reach eof until the last double, so each vector should be the same size.

*edit* I misspoke earlier - the repeated element is the last element, not the first element.
Last edited on
You are not reaching eof when reading the last double, maybe because your file ends with a line break.
You then try to read the next id, it fails, but the program doesn't care and keep trying with the other fields. So you end with invalid data, that it's pushed in the vectors.

A must robust approach will be
1
2
while( inp >> id ){
//read the other fields 
That fixes it! The file given was provided by the teacher, and we've only 'learned' the while(inp) way to do it when reading from a file, so I assumed thats how it should be done. I really like your approach a lot more, it makes more sense to me when I think about it.

Thanks so much - I don't think I would've figured that out on my own! :)
Topic archived. No new replies allowed.