How to read in text file

writing a program where i have to read in grade and credit hour values to calculate gpa.

this is my input txt file
ID# Grade cr hrs Grd cr hrs Grd cr hrs Grd cr hrs Grd cr hrs
30713 4 3 3 4 3 3 2 3 4 3
21356 4 4 4 3 3 3 2 3 2 3
21265 4 1.5 3 3 3 0.5 2 3 3 3
19364 4 3 3 3
19335 0 3 0 4 2 3 3 1.5
18264 2 3 3 0.5 3 0.5 3 3 2 3
20135 4 1 3 4 3 1.5 0 3
22185 4 3 4 4 4 3 3 3 4 3

this is how it looks in the input file on c++
1
2
3
4
5
6
7
8
30713 4 3 3 4 3 3 2 3 4 3
21356 4 4 4 3 3 3 2 3 2 3
21265 4 1.5 3 3 3 0.5 2 3 3 3
19364 4 3 3 3 -1 -1 -1 -1 -1 -1
19335 0 3 0 4 2 3 3 1.5 -1 -1 
18264 2 3 3 0.5 3 0.5 3 3 2 3
20135 4 1 3 4 3 1.5 0 3 -1 -1 
22185 4 3 4 4 4 3 3 3 4 3


the -1's are the sentinel values, where when it reads -1, the loop ends inside my while loop and moves to the next line.

How DO i read in grade and credit hours for each course
is it like this??
infile>>ID>>grade>>crhrs>>grade>>crhrs>>grade>>crhrs>>grade>>crhrs>>grade>>crhrs;
Is it a big file, do you have to make it efficient or just working?

check out this in the reference:
fstream - to handle your file
getline - to read line by line
push_back - build a vector with data. (ok but slow if you run many.)
i mean it reads in, but my professor told me the way i'm reading it in needs to be changed (i guess)

because for example
30713 4 3 3 4 3 3 2 3 4 3
30713 represents the ID
the first 4 represents the grade
the first 3 represents the credit hours (crhrs)
then the next 3 represents the grade
and the next 4 represents the credit hours (crhrs)
etc, etc.'

I think that needs to be changed...how to read that in correctly, I have to calculate the gpa with this. my professor there's some type of loop where it can read a VARYING number of items in a record, but i don't know what loop it is..
this is what i have so far in my code

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
int main()
{
	double gpa, points, hours, ID, grade, crhrs;

	ofstream OutputFile;
	OutputFile.open("Output.dat");

	ifstream infile;
	infile.open("Input.dat");

	OutputFile<<setw(30)<<"Thomas Powe"<<endl;
	OutputFile<<setw(29)<<"03-27-12"<<endl<<endl;
	OutputFile<<setw(15)<<"ID"<<setw(18)<<"GPA"<<endl<<endl;
	infile>>ID>>grade>>crhrs>>grade>>crhrs>>grade>>crhrs>>grade>>crhrs>>grade>>crhrs;
while (!infile.eof() && grade <= -1)
	{
	//infile>>ID>>grade>>crhrs>>grade>>crhrs>>grade>>crhrs>>grade>>crhrs>>grade>>crhrs;
	//OutputFile<<setw(15)<<ID<<setw(18)<<"Comin Soon"<<endl;
	points = (grade * crhrs) + (grade * crhrs); //+ //(grade * crhrs); //+ (grade * crhrs) + (grade * crhrs);
	hours=crhrs + crhrs; //+ crhrs +crhrs +crhrs;
	gpa=points/hours;
	infile>>ID>>grade>>crhrs>>grade>>crhrs>>grade>>crhrs>>grade>>crhrs>>grade>>crhrs;
	OutputFile<<setw(15)<<ID<<setw(18)<<gpa<<endl;
	}
	OutputFile.close();
	infile.close();
	return 0;
}
I guess brake the forum codex and spoil your fun if I pass you a solution. But you shouldn't walk away empty handed, right.

I persist, look up getline. Then you need to figure out how to traverse, or maybe iterate with a counter, through the present line.
i don't think i can use "getline" because we have not learned that yet in class. is there any other way?
Well my assignment to you is, look up getline. That's the way you will do it starting right after your assignment.

I guess your professor want you to look for special characters in the string. In c++ string is sort of an array with chars. Look for characters like say: '\n' and ' ' in the char array.
Last edited on
try this....

while(!infile.eof() )
{
if( grade !=-1)
{
.......
......
.....
}
}
..........
Topic archived. No new replies allowed.