Reading a data file with a Header Value

closed account (S3TkoG1T)
Hello!

I am working on reading a data file using a class called 'Student'. But I am having a hard time printing(reading) it(I'm also getting compiling errors as well) so I am looking for some advice. I'm currently working on my logic too so bear with me: The purpose of this program below is to read four fields to the record inside a text file called "input.txt" carrying the information of a roster with a students lastName, firstName, gpa & totalCredits. My problem is reading the file& printing it. Please correct me if I am wrong, I am very open to criticism :)


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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#include <iostream>
#include <fstream>
#include <stdlib.h>
using namespace std;

class Student {

public:
	string lastName;
	string firstName;
	int gpa;
	int totalCredits;
};


int main () {

	ifstream infile("input.txt");
	if (!infile) {
		cerr << "Error opening input.txt" << endl;
		exit(1);
	}

	int numItems;
	infile >> numItems;

	for(int i = 0; i < numItems; i++) { 	// from this point changes are made
		Student student;	        
		infile >> student.lastName;
			getline(infile, student.lastName);
			cout << student.lastName;	
		infile >> student.firstName;
			getline(infile, student.firstName);
			cout << student.firstName;
		infile >> student.gpa;
			getline(infile, student.gpa);
			cout << student.gpa; 
		infile >> student.totalCredits;
			getline(infile, student.totalCredits);
			cout << student.totalCredits;
		
	}
	
		
		
	infile.close();

return 0;
}
It would be nice to get some hint what's going wrong.
Line 29, 32, 35, 38 read in some variable.
Line 30, 33, 36, 39 overwrite the value currently read in by some data following the previously read data.
Line 36, 39 shouldn't compile. As long as I know there's no getline() defined for ìnt.
Topic archived. No new replies allowed.