Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <pch.h>
- #include <iostream>
- #include <string>
- #include <fstream>
- #include <iomanip>
- using namespace std;
- const int NO_OF_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);
- int highestScore(const studentType sList[], int listSize);
- void printResult(ofstream& outFile, const studentType sList[], int listSize);
- int main()
- {
- ifstream inData;
- ofstream outData;
- studentType studentList[NO_OF_STUDENTS];
- // Open input file
- inData.open("Ch9_Ex2Data.txt");
- if (!inData)
- {
- cout << "The input file does not exist. Program terminates!"
- << endl;
- return 1;
- }
- // Open output file
- outData.open("Ch9_Ex2Out.txt");
- if (!outData)
- {
- cout << "Cannot open the output file. Program terminates!"
- << endl;
- return 1;
- }
- // TODO
- getData(inData, studentList, 20);
- calculateGrade(studentList, 20);
- printResult(outData, studentList, 20);
- 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;
- }
- void calculateGrade(studentType sList[], int listSize)
- {
- //Done
- {
- for (int i = 0;i < NO_OF_STUDENTS; i++)
- if (sList[i].testScore < 60)
- sList[i].grade = 'F';
- else if (sList[i].testScore < 70)
- sList[i].grade = 'D';
- else if (sList[i].testScore < 80)
- sList[i].grade = 'C';
- else if (sList[i].testScore < 90)
- sList[i].grade = 'B';
- else
- sList[i].grade = 'A';
- }
- }
- int highestScore(const studentType sList[], int listSize)
- {
- int hScore = sList[0].testScore;
- // TODO
- for (int i = 0;i < 20;i++)
- {
- if (hScore < sList[i].testScore)
- hScore = sList[i].testScore;
- }
- return hScore;
- }
- void printResult(ofstream& outFile, const studentType sList[], int listSize)
- {
- cout << left << setw(30) << "Student Name" << right << setw(10) << "TestScore" << right << setw(7) << "Grade" << endl;
- string name;
- int high, i;
- for (i = 0; i < listSize; i++)
- {
- name = sList[i].studentLName + ", " + sList[i].studentFName;
- cout << left << setw(30) << name << right << setw(10) << sList[i].testScore << right << setw(7) << sList[i].grade << endl;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement