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
|
#include <iostream>
#include <string>
#include <fstream>
#include <iomanip>
using namespace std;
struct studentType {
string studentFname;
string studentLname;
int testScore;
char grade;
};
const int numstudents = 20;
void getData(ifstream&,studentType, int);
void calculateGrade(studentType, const int);
void printResult(ofstream&, const studentType, const int);
int main() {
ifstream infile;
infile.open("names.txt");
studentType sList[20];
if (infile.fail())
{
cout << "Error: Cannot open input file. Exiting...";
system("pause");
}
ofstream outfile;
outfile.open("grades.txt");
getData(infile, sList[numstudents], numstudents);
calculateGrade(sList[numstudents], numstudents);
printResult(outfile, sList[numstudents], numstudents);
infile.close();
outfile.close();
system("pause");
return 0;
}
void getData(ifstream& infile, studentType sList[], int &listCount)
{
while (!infile.eof())
{
for (int i = 0; i < listCount; i++)
infile >> sList[i].studentFname >> sList[i].studentLname
>> sList[i].testScore;
}
}
void calculateGrade(studentType sList[], const int listSize)
{
int score = 0;
for (int i = 0; i < listSize; i++){
if (score >= 90)
sList[i].testScore = 'A';
else if (score >= 80)
sList[i].testScore = 'B';
else if (score >= 70)
sList[i].testScore = 'C';
else if (score >= 60)
sList[i].testScore = 'D';
else
sList[i].testScore = 'F';
}
}
void printResult(ofstream& outfile, const studentType sList[], const int listSize)
{
int i;
outfile << setw(15) << "Student Name " << setw(10) << "Test Score" << setw(7) << "Grade" << endl;
for (i = 1; i < listSize; i++)
outfile << left << setw(25)
<< sList[i].studentLname + ", " + sList[i].studentFname
<< right << " " << setw(5) << sList[i].testScore
<< setw(6) << " " << sList[i].grade << endl;
}
|