Getline While loop not working properly

It should be a simple code to get all lines from a CSV file and output them on the screen, but somehow it isn't working properly.

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
#include <iostream>
#include <fstream>
#include <string>

using namespace std;

int main()
{
	ifstream file;
	string header, line;
	
	file.open("file.csv");
	
	getline(file, header);
	cout << header
	     << endl;
	
	while(getline(file, line));
	{
		cout << line
		     << endl;
	}
	
	return 0;
}


It only outputs the first line of the file, since it's outside the while loop. I can't see what's missing in the code.
voro wrote:
I can't see what's missing


I don't think anything's MISSING.

I think you've got SEMI-COLONITIS. (End of line 18).
Last edited on
Hello voro,

As lastchance said the semi-colon at the end of 18 only does the while loop and the block after the while loop only is reached when the while condition fails.

Since the while loop will continue until there is nothing left to read and "eof" causes the loop to fail it is more likely that it is printing the last line of the file.

Hope that helps,

Andy
Topic archived. No new replies allowed.