When reading from text file, How to read from the second line instead of the first line?

--------------- ENGLISH CHINESE HISTORY SCIENCE
TEE KEE SONG 99 56 22 79
JOHN STONE 88 22 99 99

Above is the format of my text file.
I want to use the marks to calculate the average marks of the students.
The following is my code:

#include <iostream>
#include <fstream>

using namespace std;

int main()
{
ifstream readfile;
readfile.open("students.txt");
string line;
getline (readfile,line);
double sub1,sub2,sub3,sub4;
string name;
while(!readfile.eof())
{
getline(readfile,line);
readfile>>name>>sub1>>sub2>>sub3>>sub4;
}
cout<< name<<sub1<<sub2<<sub3<<sub4;
readfile.close();
return 0;
}

How can I modify it to get it works?
Last edited on
How to read from the second line instead of the first line?
Read first line. Do not do anything with it.
Now you can start reading from second line and do what you want.
how to do so?
1
2
3
4
5
#include <limits>
//...
std::ifstream data("data.txt");
data.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
//read stuff from data 

You already have it...

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>
#include <fstream>

using namespace std;

int main()
{
ifstream readfile;
readfile.open("students.txt");
string line;
getline (readfile,line);  // Reads first line
// double sub1,sub2,sub3,sub4; // Remove this to skip using first line
string name;
while(!readfile.eof())
	{
	getline(readfile,line);
	readfile>>name>>sub1>>sub2>>sub3>>sub4;
	}
cout<< name<<sub1<<sub2<<sub3<<sub4;
readfile.close();
return 0;
}
Topic archived. No new replies allowed.