Class average help

I am in an intro to c++ class. I am being asked to calculate the average GPA from a text file. The text file is 2 columns: the first is the class (freshman, sophomore, junior, senior) and the second is the GPA. Each line is a random student from one of the classes. I have to find the GPA of each class. I am having trouble writing a loop that searches for only freshman to calculate the GPA. I don't want you to write my homework. Just need assistance with this one part!
Something like this should work.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
string educationLevel;
double GPA=0;
ifstream InFile;

if (InFile.is_open())
{
InFile >> educationLevel;
InFile >> GPA;

if (educationLevel="freshman")
	{
	 // do something
	}
else
	{
	// not a freshman
	}
}


It's usually more helpful for you to post your code and show what you can do first.
Well, the way I would go about doing this as I read in the info from the text file I use an if statement to test what class they are in and then depending on what class I add their GPA to a total for that class and add one to a counter for their class. When done looping through the file I would do the calculations and output the results. This should help set you in the right direction.

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
50
51
52
53
54
#include <iostream>
#include <string>
#include <fstream>
using namespace std;



int main()
{
	ifstream fin;
	string className;
	double GPA, GPAF = 0, GPASoph = 0, GPAJ = 0, GPASen = 0;
	int countF, countSoph, countJ, countSen;
	double averageF, averageSoph, averageJ, averageSen;

	//open file
	fin.open("GPAs.txt");

	//check it opened
	if (fin.fail())
	{
		cout << "Error opening output file!" << endl;
	}
	else
	{

		while (fin >> className >> GPA)
		{


			if (className == "freshman")
			{
				GPAF += GPA;
				countF++;
			}
			else //continue for the rest of the classes


		}


	}

	//close file
	fin.close();

	//do calculations 
	averageF = GPAF / countF;

	//output results


	return 0;
}
I think I see what I was doing wrong. I'll try this out when I get to a computer again. Thank you!
Topic archived. No new replies allowed.