GOTOs ARE EVIL!!
Or at least that's what my professors have told me for four years... The reason we have loops and conditionals is so we don't need gotos.
Anyway, this is how I do programs like this. First of all, since the problem asked for up to twenty students, I would use a while loop, although you could use a for loop and break out of it if they want to quit before 20 students.
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;
char quit; // I'll steal gcn_zelda's variable name!
int count; // Number of students so far
outFile.open(firstname);
if (outFile.fail())
{
cout << "\nNot successful opening " << firstname << endl;
exit(l);
}
outFile << setiosflags(ios::fixed)
<< setiosflags(ios::showpoint)
<< setprecision(2);
count = 0;
quit = 'n';
while ((count < 20) && (quit == 'n'))
{
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;
// Before exiting the loop, prompt the user if they want to continue.
// To avoid making the user enter 'y' a lot of times, you could say
// "Enter student's first name or 'N' to quit", or something like that.
// (Of course then it would make more sense to have them enter 'Q'!)
cout << "Continue? (y or n): ";
cin >> quit;
cout << endl;
++count; // Don't forget to increment count!
}
outFile.close(); // ALWAYS close your files when you're done!!!
return 0;
}
You would also have to make the user enter a lowercase 'n' or you could convert their entry to lowercase (or uppercase, whatever you like) so you don't have to check both possibilities.
(Edited for formatting)