while loop outputs extra line with eof file -help

Hello when i run this program it adds an extra line of text. how can i remove this line.

i read in from a *.txt file using sstream library. Its around lines 16 to 27.

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
void viewAllInventory()
{
    system("cls");
	cout << "\n\n\nAll Vehicle Information: \n\n\n";
	cout << " - Year - Make - Model - Color - Mileage - Price - \n\n";
	ifstream inCar;
	inCar.open ("E:\\CarLib.txt");
	if (inCar.fail())
	{
		cout << "Unable to find Vehicle Listing.\n";
	    system("pause");
	}
	else
	{
	string line,year,make,model,color,mileage,price;
	while (!inCar.eof())
	{
            getline(inCar,line);
            istringstream lines(line);
            getline(lines, year, ' ');
            getline(lines, make, ' ');
            getline(lines, model, ' ');
            getline(lines, color, ' ');
            getline(lines, mileage, ' ');
            getline(lines, price, ' ');
    	    cout << " - " << year << " - " << make << " - " << model << " - " << color << " - " << mileage << " - " << price << " - " << endl;
 	}
	}
	inCar.close();
	cout << "\n\n";
	system("pause");
}


thanks for any help you can give me
matt
with one vehicle information
1995 toyota camry green 10000 5000
the output looks like this:



All Vehicle Information:



- Year - Make - Model - Color - Mileage - Price -

- 1995 - toyota - camry - green- 10000 - 5000 -
- - toyota - camry - green - 10000 - 5000 -


press any key to continue...
thank you i feel kind of stupid for asking.

thank you for the help though

i didnt understand it at first it took me about 20 mins to work it out, but i used

while (inCar >> line)

instead of

while (!inCar.eof())

it seems to have worked very well.
nevermind it didnt completely work

it doesnt continue past the last of the file, but for some reason it will not print out the year on any of the files anymore.
operator>> reads a single word. You are reading lines so you should probably still use getline. Both of them returns the stream object so they can be used the same way.

while (getline(inCar,line))
(If you do this you have to remove getline on line 18)
That was it.

i had tried it when i read it from the link that was posted earlier but i didnt remove the statement from line 18 and it kept giving me an error. i delete out line 18 and it works like a charm.

this site has saved me this semester. Thank you! you guys are wicked smart.
-matt
Topic archived. No new replies allowed.