Homework help

Hello everyone. I have this homework assignment that is not working properly. I steped through this function many times and I can't figure out what is wrong. The function first opens an input file and an output file. It then checks if the file exists. It then goes into a while loop that continues until it gets to the end of the file. Inside this loop it first reads the student initials. It then goes into another loop that gets the student's letter grades one at a time and converts them into a double. After it is done with that student it does a gpa calculation. According to my step throgh at this point it should go back up and read the second student's initials. The fucnction works fine until it tries to read the second student's letter grades. At this point it displays the first student's calculated gpa. It then displays the second student's letter grades. I can't figure out why it does this, if it does get the second student's initials.

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
void openFile(string inputFile, string outputFile)
{
    ifstream gradeFile;
    ofstream gpaStudentInitials;
    string studentInitials;
    char letterGrade;
    double letterToNumber = 0;
    double gpaTotal;
    double studentsTotalGpa;
    int letters = 0;
    int totalStudents = 1;

    gradeFile.open(inputFile.c_str());
    gpaStudentInitials.open(GPAS.c_str());

    if (!gradeFile)
        cout << "FATAL ERROR, FILE DOES NOT EXIST";

    while(!gradeFile.eof())
    {
        gradeFile >> studentInitials;
        gradeFile.ignore(3, ' ');

        while(letterGrade != '\n')
        {
            gradeFile.get(letterGrade);
            if (letterGrade != '\n')
            {
                letterToNumber = convertLetterGrade(letterGrade);
                cout << " " << letterToNumber << " ";
                gpaTotal = gpaTotal + letterToNumber;
                letters++;
            }

        }

        studentsTotalGpa = calculateGpa(letters, gpaTotal);
        cout << fixed << setprecision(2) << showpoint << studentInitials;
        gpaStudentInitials << studentInitials << " " << studentsTotalGpa << endl;


        totalStudents++;
        cout << " total students " << totalStudents << " ";
    }

}
could you give us the layout of the file?

Last edited on
Yes. This is what my input file looks like:

MKS AAAABBAAAA
NMT BBBBCCCC

And my output looks like this:

MKS 3.80
NMT 3.80
It might be because letterGrade is never changed from '\n' after the first loop.
Try changing it to something else and see if that works.
what is structure of claculategpa()???
Sloppy... but should help.
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
#include <string>
#include <fstream>
#include <iostream>
#include <iomanip>
using namespace std;


int main()

{

	 ifstream gradeFile;
    ofstream gpaStudentInitials;
    string studentInitials;
    char letterGrade=' ';
    double letterToNumber = 0;
    double gpaTotal =0;
    double studentsTotalGpa;
    int letters = 0;
    int totalStudents = 0;

    gradeFile.open("grades.txt");
    gpaStudentInitials.open("GPAS.txt");

    if (gradeFile.fail())
        cout << "FATAL ERROR, FILE DOES NOT EXIST";

    while(!gradeFile.eof())
    {
		gradeFile >> studentInitials;
       if (studentInitials == "")
	   break;
		gradeFile.get(letterGrade);
	
        while(letterGrade != '\n')
        {
			
            gradeFile.get(letterGrade);
			if(gradeFile.eof())
				{
					break;
				}
            if (letterGrade != '\n')
			    {
                
				if(letterGrade == 'A')
				{
					letterToNumber=4;
				}
				if(letterGrade == 'B')
				{
					letterToNumber=3;
				}
				if(letterGrade == 'C')
				{
					letterToNumber=2;
				}
				if(letterGrade == 'D')
				{
					letterToNumber=1;
				}
				if(letterGrade == 'F')
				{
					letterToNumber=0;
				}
               // cout << " " << letterToNumber << " ";
                gpaTotal = gpaTotal + letterToNumber;
                letters++;

            }
		
        }
		studentsTotalGpa= gpaTotal /letters;
		if(studentInitials == "")
		{
			break;
		}

        //studentsTotalGpa = calculateGpa(letters, gpaTotal);
       cout << fixed << setprecision(2) << studentInitials;
       gpaStudentInitials << studentInitials << " " << studentsTotalGpa << endl;
	   gpaTotal=0;
	   letters = 0;
	   studentInitials = "";
        totalStudents++;
        cout << " total students " << totalStudents << " ";
    }

	
 gpaStudentInitials.close();
 gradeFile.close();

	return 0;
}
Last edited on
Thank you everybody for your help.
Topic archived. No new replies allowed.