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 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270
|
#include <iostream>
#include <fstream>
#include <iomanip>
using namespace std;
//Prototyping all functions so they will work written after main
void printHeading(ofstream& outFile);
bool validData(inputType input);
float avg(inputType input, float& totalAvg);
void printAvgAndGrade(ofstream& outFile, float avg, inputType input);
void printInvalidDataMsg(ofstream& outFile, inputType input, string& outCome);
void printStat(ofstream& outFile, float meanOfAvg, int passed, int failed, int males, int females,
float examMax, float examMin);
void readRec(ifstream& inFile, inputType& input);
void grade(float examAvg, char& examGrade, int& aCount, int& bCount, int& cCount, int& dCount,
int& fCount);
void countGender(int& males, int& females, inputType input);
void printPercentage(ofstream& outFile, int valid, int aCount, int bCount, int cCount,
int dCount, int fCount);
const float MAX = 100.0;
const float MIN = 0;
int main()
{
ifstream inFile;
ofstream outFile;
inFile.open("p10in1.data");
outFile.open("out.data");
outFile.setf(ios::fixed);
outFile.precision(2);
struct inputType
{
string studName;
int id;
int exam1;
int exam2;
int exam3;
char gender;
char examGrade;
};
inputType input;
string outCome;
float examAvg, totalAvg, meanOfAvg, examMax, examMin;
int count, valid, invalid, males, females, aCount, bCount, cCount, dCount, fCount;
bool validTest;
count = 1;
aCount = 0;
bCount = 0;
cCount = 0;
dCount = 0;
fCount = 0;
invalid = 0;
valid = 0;
males = 0;
females = 0;
totalAvg = 0;
if (!inFile)
outFile << "File read error, Goodbye!" << endl;
else
{
printHeading(outFile);
//Reads the data from the input file and stores in specified variables
readRec(inFile, input);
while (inFile) //Creates the loop to read unknown number of records
{
validTest = validData(input);
if (validTest)
{
valid++;
countGender(males, females, input);
examAvg = avg(input, totalAvg);
if (count == 1)
{
examMax = examAvg;
examMin = examAvg;
count++;
}
if (examMax < examAvg)
examMax = examAvg;
if (examMin > examAvg)
examMin = examAvg;
grade(examAvg, examGrade, aCount, bCount, cCount, dCount, fCount);
printAvgAndGrade(outFile, examAvg, input);
}
else
{
invalid++;
printInvalidDataMsg(outFile, input, outCome);
}
//Read back in new data into the variables to process every record
readRec(inFile, input);
}
meanOfAvg = (totalAvg / valid); //To calculate mean average
printStat(outFile, meanOfAvg, females, males, valid, invalid, examMax, examMin);
printPercentage(outFile, valid, aCount, bCount, cCount, dCount, fCount);
}
inFile.close(); //These close the files we have open
outFile.close();
return 0; //Returns 0 on successful completion
}
void printHeading(ofstream& outFile/*out*/)
{
//These next statements output the information to output file
outFile << endl;
outFile << "*~~< Student Exam Report >~~*\n\n";
outFile << setw(15) << left << "Name" << setw(10) << "Stud Id" << setw(10) << "Gender" << setw(10) << "Exam1"
<< setw(10) << "Exam2" << setw(10)
<< "Exam3" << setw(15) << "AVG of Exams" << setw(15) << "Grade" << endl;
outFile << setw(15) << left << "----" << setw(10) << "-------" << setw(10) << "------" << setw(10) << "-----" $
<< "-----" << setw(10) << "-----" << setw(15) << "------------" << setw(15) << "-----" << endl;
return;
}
bool validData(inputType input/*in*/)
{
//The if-then-else structure to test if all data is valid and output according to if it is or not
if ((input.id >= 1111 && input.id <=9999) && (input.exam1 >= MIN && inputexam1 <= MAX) && (input.exam2 >=
MIN && input.exam2 <= MAX) && (input.exam3 >= MIN && input.exam3 <= MAX))
return true;
else
return false;
}
float avg(inputType input/*in*/, float& totalAvg/*inout*/)
{
float examAvg;
examAvg = (input.exam1 + input.exam2 + input.exam3)/3; //Calculation for exam averages
totalAvg += examAvg; //To add up all averages for mean average
return examAvg;
}
void printAvgAndGrade(ofstream& outFile/*out*/, float avg/*in*/, inputType input/*in*/)
{
//Output this way if everything goes along according to plan
outFile << setw(15) << left << input.studName << setw(10) << input.id << setw(10) << input.gender <<
setw(10) << input.exam1 << setw(10) << input.exam2 << setw(10) << input.exam3 << setw(15)
<< input.avg << setw(20) << input.examGrade << endl;
return;
}
void printInvalidDataMsg(ofstream& outFile/*out*/, inputType input/*in*/, string& outCome/*out*/)
{
//To determine which piece of data is invalid
if (!(id >= 1111 && id <=9999))
outCome = "~~ Invalid student ID data ~~";
else if (!(exam1 >= MIN && exam1 <= MAX))
outCome = "~~ Invalid Exam1 data ~~";
else if (!(exam2 >= MIN && exam2 <= MAX))
outCome = "~~ Invalid Exam2 data ~~";
else if (!(exam3 >= MIN && exam3 <= MAX))
outCome = "~~ Invalid Exam3 data ~~";
//Output this way if other conditions not met
outFile << setw(15) << input.studName << setw(10) << input.id << setw(10) << input.gender << setw(10) <<
input.exam1 << setw(10) << input.exam2 << setw(10) << input.exam3 << setw(15) << outCome << endl;
return;
}
void printStat(ofstream& outFile/*out*/, float meanOfAvg/*in*/, int females/*in*/, int males/*in*/, int
valid/*in*/, int invalid/*in*/, float examMax/*in*/, float examMin/*in*/)
{
//More output lines to finish off the writing of the file
outFile << endl << "Exams AVG = " << meanOfAvg << endl;
outFile << "Exams MAX: " << examMax << endl;
outFile << "Exams MIN: " << examMin << endl << endl;
outFile << "Number of Female Student(s): " << females << endl;
outFile << "Number of Male Student(s): " << males << endl << endl;
outFile << "Number of valid record(s): " << valid << endl;
outFile << "Number of invalid records): " << invalid << endl << endl;
return;
}
void readRec(ifstream& inFile, inputType& input)
{
inFile >> input.studName >> input.id >> input.gender >> input.exam1 >> input.exam2 >> input.exam3;
return;
}
void grade(float examAvg/*in*/, char& examGrade/*out*/, int& aCount/*inout*/, int& bCount/*inout*/,
int& cCount/*inout*/, int& dCount/*inout*/, int& fCount/*inout*/)
{
if (examAvg >= 90)
{
examGrade = 'A';
aCount++;
}
else if (examAvg >= 80)
{
examGrade = 'B';
bCount++;
}
else if (examAvg >= 70)
{
examGrade = 'C';
cCount++;
}
else if (examAvg >= 60)
{
examGrade = 'D';
dCount++;
}
else
{
examGrade = 'F';
fCount++;
}
return;
}
void countGender(int& males/*inout*/, int& females/*inout*/, inputType input/*in*/)
{
if (input.gender == 'M')
males++;
else if (input.gender == 'F')
females++;
return;
}
void printPercentage(ofstream& outFile/*out*/, int valid /*in*/, int aCount/*in*/, int bCount/*in*/, int
cCount/*in*/, int dCount/*in*/, int fCount/*in*/)
{
outFile.setf(ios::fixed);
outFile.precision(0);
float aPercent, bPercent, cPercent, dPercent, fPercent;
aPercent = ((aCount * 100) / valid);
bPercent = ((bCount * 100) / valid);
cPercent = ((cCount * 100) / valid);
dPercent = ((dCount * 100) / valid);
fPercent = ((fCount * 100) / valid);
outFile << "~* Grade Distribution *~" << right << endl;
outFile << setw(7) << "As:" << setw(5) << aCount << setw(5) << aPercent << "%" << endl;
outFile << setw(7) << "Bs:" << setw(5) << bCount << setw(5) << bPercent << "%" << endl;
outFile << setw(7) << "Cs:" << setw(5) << cCount << setw(5) << cPercent << "%" << endl;
outFile << setw(7) << "Ds:" << setw(5) << dCount << setw(5) << dPercent << "%" << endl;
outFile << setw(7) << "Fs:" << setw(5) << fCount << setw(5) << fPercent << "%" << endl;
outFile << endl << "*~~< end >~~*\n\n";
}
|