Grade Calculator

I am a math teacher who is taking a programming class. My district wants to incorporate some programming tasks into the high school math curricula. I am working on a project to calculate a final grade and output this along with a comment to a text file. I have spent hours researching information and teaching myself. The following data is the input that I created:

3 300 100 100 100 .15
4 400 100 98 94 92 .45
3 300 98 100 92 .40

The final grade output results in gibberish.
Thank you

#include <iostream>
#include <fstream> // For file I/O
#include <string>
#include <iomanip>
#include <cmath>

using namespace std;

int main()
{
int count;
int numHw, numPrgrm, numExam;
float finalGrd;
float HwGrade, PrgrmGrade, ExamGrade;
float totalHwPts, totalPrgrmPts, totalExamPts;
float maxHwPts, maxPrgrmPts, maxExamPts;
float hwPercent, prgrmPercent, examPercent;
float HwPart, PrgrmPart, ExamPart;
char LetterGrade;
const string FIRST = "John";
const string LAST = "Smith";
const char MIDDLE = 'W';
ifstream inData;
ofstream outData;

inData.open("gradeFile.txt"); // Attempt to open files
outData.open("myGrade.txt");
if (!inData || !outData) // Was it opened?
{
cout << "Can't open the files."; // No--print message
return 1; // Terminate program
}

inData >> numHw;
inData >> maxHwPts;
count = 0;
while (count < numHw)
{
inData >> HwGrade;
totalHwPts = totalHwPts + HwGrade;
count ++;
}
inData >> hwPercent;

inData >> numPrgrm;
inData >> maxPrgrmPts;
count = 0;
while (count < numPrgrm)
{
inData >> PrgrmGrade;
totalPrgrmPts = totalPrgrmPts + PrgrmGrade;
count ++;
}
inData >> prgrmPercent;

inData >> numExam;
inData >> maxExamPts;
count = 0;
while (count < numExam)
{
inData >> ExamGrade;
totalExamPts = totalExamPts + ExamGrade;
count ++;
}
inData >> examPercent;

// Calculate Final Grade
if (maxHwPts > 0)
{
HwPart = (totalHwPts/maxHwPts) * (hwPercent);
}
if (maxPrgrmPts > 0)
{
PrgrmPart = (totalPrgrmPts/maxPrgrmPts) * (prgrmPercent);
}
if (maxExamPts > 0)
{
ExamPart = (totalExamPts/maxExamPts) * (examPercent);
}
else
{
outData << "Error!" << endl;
return 1;
}

finalGrd = (HwPart + PrgrmPart + ExamPart)* 100;

//Print Equivalent Letter Grade
string lastFirst;
lastFirst = LAST + ", " + FIRST + " " + MIDDLE + ".";

outData << "Student Name: " << setw(25) << lastFirst << endl;
outData << "Final Grade is " << fixed << showpoint << setw(25) << finalGrd << "." << endl;

if (finalGrd <= 60)
{
outData << "You earned an F." << endl;
LetterGrade = 'F';
}
else if (finalGrd <=69)
{
outData << "You earned a D." << endl;
LetterGrade = 'D';
}
else if (finalGrd <=79)
{
outData << "You earned a C." << endl;
LetterGrade = 'C';
}
else if (finalGrd <=89)
{
outData << "You earned a B." << endl;
LetterGrade = 'B';
}
else if (finalGrd <=100)
{
outData << "You earned an A." << endl;
LetterGrade = 'A';
}

//Print Message Indicating Status
switch (LetterGrade)
{
case 'A': outData << "Teacher's pet, I see!" << endl;
break;
case 'B': outData << "You're doing an awesome job!" << endl;
break;
case 'C': outData << "Keep your grades up." << endl;
break;
case 'D': outData << "Pick up the pace a bit more." << endl;
break;
case 'F': outData << "Do you want to flip burgers for the rest of your life?" << endl;
break;
default: outData << LetterGrade << " is not a valid letter grade." << endl;
break;
}

//Close files
inData.close();
outData.close();

system ("pause");
return 0;
}


What is your question?
How do I fix my code so it outputs the correct final grade along with the corresponding comment?
Thank You
Sorry. Missed your comment
The final grade output results in gibberish.


In the following lines, your totals are uninitialied. This will result in bogus calculations.
1
2
3
totalHwPts = totalHwPts + HwGrade;
totalPrgrmPts = totalPrgrmPts + PrgrmGrade;
totalExamPts = totalExamPts + ExamGrade;


You need to initialize them to 0 before you use them.

PLEASE USE CODE TAGS (the <> formatting button) when posting code.
Last edited on
Here you go, analyze the 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
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <fstream>
#include <sstream>
using namespace std;

class Person
{
    public:
    string FirstName;
    string LastName;
    char comment[256];
    double totalgrade;
    double hwpoints,hwpointsmax;
    double exampoints,exampointsmax;
    double prgmpoints,prgmpointsmax;
    double percentage;
    string grade;
    string name;
};

double percentage(double grade1,double grade2)
{
    double percent = grade1 / grade2 * 100;
    return percent;
}

string grade(double percent)
{
    string grade;
    if(percent >= 98)
    {
        grade = "A+";
    }
    if(percent >= 93 && percent <= 97)
    {
        grade = "A";
    }
    if(percent >= 90 && percent <= 92)
    {
        grade = "A-";
    }
    if(percent >= 88 && percent <= 89)
    {
        grade = "B+";
    }
    if(percent >= 83 && percent <= 87)
    {
        grade = "B";
    }
    if(percent >= 80 && percent <= 82)
    {
        grade = "B-";
    }
    if(percent >= 78 && percent <= 79)
    {
        grade = "C+";
    }
    if(percent >= 73 && percent <= 77)
    {
        grade = "C";
    }
    if(percent >= 70 && percent <= 72)
    {
        grade = "C-";
    }
    if(percent >= 68 && percent <= 69)
    {
        grade = "D+";
    }
    if(percent >= 63 && percent <= 67)
    {
        grade = "D";
    }
    if(percent >= 60 && percent <= 62)
    {
        grade = "D-";
    }
    if(percent <= 50)
    {
        grade = "F";
    }

    return grade;
}

double finalscore(double g1,double g2,double g3,double g4,double g5,double g6)
{
    double t1 = g1 + g3 + g5;
    double t2 = g2 + g4 + g6;

    double result = t1 / t2 * 100;
    return result;
}

Person getPerson()
{
    Person person;
    ofstream data;

    cout << "\nEnter another student\n"
    << "First Name: ";
    cin >> person.FirstName;

    cout << "Last Name: ";
    data.open("Grades.txt", fstream::in|fstream::app);
    cin >> person.LastName;
    person.name = person.FirstName + " " + person.LastName;
    data << "Name: \t\t\t\t" << person.name;
    data << "\n";
    data.close();

    cout << "Homework Points: ";
    data.open("Grades.txt", fstream::in|fstream::app);
    cin >> person.hwpoints;
    data << "Homework Points: \t\t\t" << person.hwpoints;
    data << "\n";
    data.close();

    cout << "Max Homework Points: ";
    data.open("Grades.txt", fstream::in|fstream::app);
    cin >> person.hwpointsmax;
    data << "Max Homework Points: \t\t" << person.hwpointsmax;
    data << "\n";
    data.close();

    person.percentage = percentage(person.hwpoints,person.hwpointsmax);
    person.grade = grade(person.percentage);
    data.open("Grades.txt", fstream::in|fstream::app);
    data << "Homework Percentage and Grade: \t" << person.percentage << "%\t" << person.grade << "\n";
    data.close();

    cout << "Exam Points: ";
    data.open("Grades.txt", fstream::in|fstream::app);
    cin >> person.exampoints;
    data << "Exam Points: \t\t\t" << person.exampoints;
    data << "\n";
    data.close();

    cout << "Max Exam Points: ";
    data.open("Grades.txt", fstream::in|fstream::app);
    cin >> person.exampointsmax;
    data << "Max Exam Points: \t\t\t" << person.exampointsmax;
    data << "\n";
    data.close();

    person.percentage = percentage(person.exampoints,person.exampointsmax);
    person.grade = grade(person.percentage);
    data.open("Grades.txt", fstream::in|fstream::app);
    data << "Exam Percentage and Grade: \t" << person.percentage << "%\t" << person.grade << "\n";
    data.close();

    cout << "Programming Points: ";
    data.open("Grades.txt", fstream::in|fstream::app);
    cin >> person.prgmpoints;
    data << "Programming Points: \t\t" << person.prgmpoints;
    data << "\n";
    data.close();

    cout << "Max Programming Points: ";
    data.open("Grades.txt", fstream::in|fstream::app);
    cin >> person.prgmpointsmax;
    data << "Max Programming Points: \t\t" << person.prgmpointsmax;
    data << "\n";
    data.close();

    person.percentage = percentage(person.prgmpoints,person.prgmpointsmax);
    person.grade = grade(person.percentage);
    data.open("Grades.txt", fstream::in|fstream::app);
    data << "Programming Percentage and Grade: \t" << person.percentage << "%\t" << person.grade << "\n";
    data.close();

    person.totalgrade = finalscore(person.hwpoints,person.hwpointsmax,person.exampoints,person.exampointsmax,person.prgmpoints,person.prgmpointsmax);
    person.grade = grade(person.totalgrade);

    data.open("Grades.txt", fstream::in|fstream::app);
    data << "Final Grade: \t\t\t" << person.totalgrade << "%\t" << person.grade << "\n";
    data.close();

    cin.getline(person.comment,256);
    cout << "Please enter your comment: ";
    data.open("Grades.txt", fstream::in|fstream::app);
    cin.getline(person.comment,256);
    data << "Comment: " << person.comment;
    data << "\n ---------------------------------------------------------------------------------------------------------------------------\n" << endl;
    data.close();

    return person;
}

int getPeople(Person people[], int nMaxSize)
{
    int index;
    for(index = 0; index < nMaxSize; index++)
    {
        char cAnswer;
        cout << "Enter another student? (Y or N): ";
        cin >> cAnswer;

        if(cAnswer != 'Y' && cAnswer != 'y')
        {
            break;
        }

        people[index] = getPerson();
    }
    return index;
}

void displayPerson(Person person)
{
    cout << "Name:\t\t\t\t" << person.FirstName << " " << person.LastName << endl;
    cout << "Homework Points / MAX:\t\t" << person.hwpoints << "/" << person.hwpointsmax << endl;
    cout << "Exam Points / MAX:\t\t" << person.exampoints << "/" << person.exampointsmax << endl;
    cout << "Programming Points / MAX:\t" << person.prgmpoints << "/" << person.prgmpointsmax << endl;
    cout << "Final Grade:\t\t\t" << person.totalgrade << "%" << endl << endl;
    cout << "Your comment: " << person.comment << endl;
    cout << "-----------------------------------------------------------\n" << endl;
}

void displayPeople(Person people[], int nCount)
{
    for(int index = 0;index < nCount; index++)
    {
        displayPerson(people[index]);
    }
}

int main(int nNumberofArgs,char* pszArgs[])
{
    Person people[256];

    cout << "Hello!, this program stores student scores." << endl;
    cout << "It will store it temporarily in this console while storing it in 'Grades.txt'." << endl;
    cout << "Letter grades and percentages are stored in the .txt file." << endl;

    int count = getPeople(people, 256);

    cout << endl << "Here is what your entered: " << endl << endl;
    displayPeople(people,count);

    system("PAUSE");
    return 0;
}
Thank you for your time.
The program runs perfectly now.
You are very welcome
Topic archived. No new replies allowed.