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
|
// Program purpose:
// 1) Program reads each student's names followed by their test score from
// a text file "T3DebugData.txt"
// 2) Program should output each student's name followed by test scores
// and relevant grade to a file.
// 3) Program should also find and print the highest test score and the
// names of all the students that got that score.
// 4) Program output file name is "T3DebugOut.txt".
// 5) The program must also include struct for the students name and tests
// 6) Assume 20 students max.
#include <iostream>
#include <string>
#include <fstream>
using namespace std;
const int NO_OR_STUDENTS = 20;
struct studentType
{
string studentFName;
string studentLName;
int testScore;
char grade;
};
void getData(ifstream& inFile, studentType sList[], int listSize);
void calculateGrade(studentType sList[], int listSize);
void printResult(ofstream& outFile, const studentType sList[],
int listSize);
void holdscreen( string exitMessage );
int main()
{
ifstream inData;
ofstream outData;
studentType studentList[NO_OR_STUDENTS];
cout << "\nThis program gets student names and test scores from one file "
<< "and writes a report to another file. \n\n";
inData.open("T3dbeugdata.txt");
if (!inData)
{
cout << "The input file does not exist. Program terminates!"
<< endl;
holdscreen ("Error Exit - Input file error!");
return 1;
}
outData.open("T3DebugOut.txt");
if (!outData)
{
cout << "Cannot open the output file. Program terminates!"
<< endl;
holdscreen ("Error Exit - Output file error!");
return 1;
}
getData(inData, studentList, NO_OR_STUDENTS);
calculateGrade(studentList, NO_OR_STUDENTS);
printResult(outData, studentList, NO_OR_STUDENTS);
// Holdscreen function with all processed message
holdscreen ("All the scores have been processed!");
return 0;
}
void getData(ifstream& inFile, studentType sList[], int listSize)
{
for (int i = 0; i < listSize; i++)
inFile >> sList[i].studentFName >> sList[i].studentLName
>> sList[i].testScore;
return;
}
void calculateGrade(studentType sList[], int listSize)
{
for (int i = 0; i < listSize; i++)
{
switch (sList[i].testScore / 10)
{
case 10:
case 9:
sList[i].grade = 'A';
break;
case 8:
sList[i].grade = 'B';
break;
case 7:
sList[i].grade = 'C';
break;
case 6:
sList[i].grade = 'D';
break;
case 5:
case 4:
case 3:
case 2:
case 1:
case 0:
sList[i].grade = 'F';
}
}
return;
}
int highestScore(const studentType sList[], int listSize)
{
int hScore = sList[0].testScore;
for (int i = 1; i < listSize; i++)
if (hScore < sList[i].testScore)
hScore = sList[i].testScore;
return hScore;
}
void printResult(ofstream& outFile, const studentType sList[])
{
int maxScore = highestScore(sList, listSize);
int i;
outFile << setw(15) << "Student Name "
<< setw(10) << "Test Score"
<< setw(7) << "Grade" << endl;
for (i = 0; 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;
outFile << endl << "Highest Test Score: " << maxScore << endl;
outFile << "Students having the highest test score:" << endl;
for (i = 0; i < listSize; i++)
if (sList[i].testScore == maxScore)
outFile << sList[i].studentLName + ", " + sList[i].studentFName
<< endl;
return;
}
void holdscreen( string exitMessage )
{
char holdscr = 0;
cout << "\n\n\t * * * " << exitMessage << " * * *"
<< "\n\n\t Enter a character and return to exit the program! ";
cin >> holdscr;
return;
}
|