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
|
#include <iostream>
#include <fstream>
#include <string>
// Struct to fold the information of the student.
struct Student
{
std::string name;
std::string SSN;
std::string address;
std::string telephone;
unsigned int age;
unsigned int numOfYears;
};
void readFile(std::ifstream&, Student&, std::string, double&);
char determineGrade(double);
void writeFile(std::ofstream&, Student, std::string, char);
int main()
{
Student student{};
double percentage{};
char letterGrade{};
std::string inFileName{ "project2_input.txt" };
std::string outFileName{ "project2_output.txt" };
std::ifstream inFile;
std::ofstream outFile;
readFile(inFile, student, inFileName, percentage);
letterGrade = determineGrade(percentage);
writeFile(outFile, student, outFileName, letterGrade);
}
void readFile(std::ifstream& info, Student& student, std::string fileName, double& percentage)
{
// An array to hold the weighted score, based on the type of exam.
double weightedScore[]{ .10, .15, .15, .20, .40 };
double testScores{};
// A counter to be used to determine the weighted score
// in the while loop.
int countTestScores{ 0 };
info.open(fileName);
if (!info)
{
std::cout << "File [" << fileName << "] could not open.\n";
return;
}
else
{
std::cout << "File [" << fileName << "] open.\n";
std::getline(info, student.name);
info >> student.age;
info.ignore();
std::getline(info, student.address);
info >> student.numOfYears;
info.ignore();
std::getline(info, student.telephone);
std::getline(info, student.SSN);
// While is reading the scores, compute the percentage
// by multiplying the test score by the proper weighted score.
// Example: Score 98.5 * .10[aray index 0] = percentage.
// Example: Score 87.5 * .15[array index 1] = percentage.
// The percentage is to be used as a running total to
// determine later the letter grade.
while (info >> testScores)
{
percentage += (testScores * weightedScore[countTestScores]);
countTestScores++;
}
}
info.close();
std::cout << "File [" << fileName << "] closed.\n";
}
// This function obtains the percentage
// obtain by reading the file to obtain
// the letter grade, which would be
// returned as a character data type.
char determineGrade(double percentage)
{
char letterGrade{};
if (percentage >= 90)
letterGrade = 'A';
else if (percentage >= 80)
letterGrade = 'B';
else if (percentage >= 70)
letterGrade = 'C';
else if (percentage >= 65)
letterGrade = 'D';
else
letterGrade = 'F';
return letterGrade;
}
// This function writes a file with the name of the student
// and the final grade, based on the test scores from the input file.
void writeFile(std::ofstream& outFile, Student student, std::string fileName, char letterGrade)
{
outFile.open(fileName);
std::cout << "File [" <<fileName << "] open.\n";
outFile << "Student Name\t\tFinalGrade\n";
outFile << student.name << "\t\t" << letterGrade;
outFile.close();
std::cout << "File [" << fileName << "] closed.\n";
}
|