Please review and comment
Problem:
Program allows the user to enter grade information for up to 20
students in a class.
Name, Exam 1 grade, Exam 2 grade, Homework grade, Final Exam Grade
For each student, first calculate a final grade , using the formula:
finalgrade = 0.20 * Exam 1 + 0.20 * Exam 2 + 0.35 * Homework + 0.25 * Final Exam
then, assign a letter grade on the basis of 90-100=A, 80-89=B, 70-79=c,
60-69=d, less than 60=F.
All the information, including the final grade and the letter grade should be
written and displayed to a file.
Note: Program should STOP after 20 loops. Ask the user if they want to continue?
If the counter < 20, prompt the user, store - Ex. - enter a y to continue or an n
to quit. You can use a do-while loop condition such as: while counter less than 20
and proceed equal to 'Y'
Question for you: How would you handle the exit condition?
Code:
#include <fstream.h>
#include <iomanip.h>
#include <stdlib.h>
int main()
{
const int MAXSTUDENTS = 20;
const int MAXNAME = 20;
const int MAXCHARS = 10;
char firstname [MAXCHARS] = "allgrades.dat";
int i;
char lastname[MAXNAME], lettergrade;
float exam1, exam2, homework, finalexam, finalgrade;
ofstream outFile;
outFile.open(firstname);
if (outFile.fail())
{
cout << "\nNot successful opening " << firstname << endl;
exit(l);
}
outFile << setiosflags(ios::fixed)
<< setiosflags(ios::showpoint)
<< setprecision(2);
for (i = 1; 1 <= MAXSTUDENTS; i++)
{
cout << "\nEnter the student's last name: ";
cin >> lastname;
cout << "\nEnter exam 1's grade: ";
cin >> exam1;
cout << "\nEnter exam 2's grade: ";
cin >> exam2;
cout << "\nEnter the student's homework grade: ";
cin >> homework;
cout << "\nEnter the final exam grade: ";
cin >> finalexam;
finalgrade = 0.20 * examl + 0.20 * exam2 + 0.35 * homework + 0.25 * finalexam;
if(finalgrade >= 90) lettergrade = 'A';
else if (finalgrade >= 80) lettergrade = 'B';
else if (finalgrade >= 70) lettergrade = 'C';
else if (finalgrade >= 60) lettergrade = 'D';
else finalgrade = 'F';
cout << lastname << " " << examl << " " << exam2 << " " homework
<< " " << finalexam << " " << finalgrade << " " << lettergrade << endl;
outFile << lastname << " " << examl << " " << exam2 << " " homework
<< " " << finalexam << " " << finalgrade << " " << lettergrade << endl;
}
return 0
}