You are on page 1of 8

// // Bryan Smith // G03275597 // Final Exam // Version 1 // Team: Bryan Smith // #include <iostream> #include <vector> #include <fstream>

#include <string> #include <sstream> #include <algorithm> #include <cctype> using namespace std; typedef struct { string lastName; string firstName; long int gNumber; int grades[12]; } student; string getInput(); string getOption(); string getOutfileName(); ifstream * openFile(); ofstream * openOutFile(); bool loadStudentData(vector<student> &studentRecords, ifstream *inFile); void loadClassData(vector<student> &studentRecords, ifstream *inFile); string capitalize(string lowerCase); void editClassData(vector<student> &studentRecords); void editStudentData(vector<student> &studentRecords); char assignGrade(int points); void menu(); void writeReport(vector<student> &studentRecords, ofstream *outFile); void notLoaded(); void notOption(string lowerCase); int calculatePoints(vector<student> &studentRecords, int idx); void loadStudentMenu(); void loadClassMenu(); int main() { vector<student> studentRecords; student tempStudent; string choice, lowerCase; bool isLoaded = false; ifstream *inFile; ofstream *outFile; do { menu(); lowerCase = getOption(); choice = capitalize(lowerCase); if (choice.compare("LOAD_STUDENT_DATA") == 0 || choice.compare("1") == 0) { loadStudentMenu(); inFile = openFile(); isLoaded = loadStudentData(studentRecords, inFile); inFile->clear(); inFile->close(); } if ((choice.compare("LOAD_CLASS_DATA") == 0 || choice.compare("2") == 0) && isLoaded) {

loadClassMenu(); inFile = openFile(); loadClassData(studentRecords, inFile); inFile->clear(); inFile->close(); } if ((choice.compare("EDIT_STUDENT_DATA") == 0 || choice.compare("3") == 0) && isLoaded) { editStudentData(studentRecords); } if ((choice.compare("EDIT_CLASS_DATA") == 0 || choice.compare("4") == 0) && isLoaded) { editClassData(studentRecords); } if ((choice.compare("WRITE_GRADE_REPORT") == 0 || choice.compare("5") == 0) && isLoaded) { outFile = openOutFile(); writeReport(studentRecords, outFile); outFile->flush(); outFile->close(); } if (choice.compare("QUIT") == 0 || choice.compare("6") == 0) { break; } if (!isLoaded && (choice.compare("LOAD_CLASS_DATA") == 0 || choice.compare("2") == 0 || choice.compare("EDIT_STUDENT_DATA") == 0 || choice.compare("3") == 0 || choice.compare("EDIT_CLASS_DATA") == 0 || choice.compare("4") == 0 || choice.compare("WRITE_GRADE_REPORT") == 0 || choice.compare("5") == 0)) { notLoaded(); } if (choice.compare("LOAD_STUDENT_DATA") != 0 && choice.compare("1") != 0 && choice.compare("LOAD_CLASS_DATA") != 0 && choice.compare("2") != 0 && choice.compare("EDIT_STUDENT_DATA") != 0 && choice.compare("3") != 0 && choice.compare("EDIT_CLASS_DATA") != 0 && choice.compare("4") != 0 && choice.compare("WRITE_GRADE_REPORT") != 0 && choice.compare("5") != 0 && !isLoaded) { notOption(lowerCase); } } while (choice.compare("QUIT") != 0 || choice.compare("6") != 0); return 0; } string getOption() { string lowerCase; getline(cin, lowerCase); return lowerCase; } void notOption(string lowerCase) { cout << lowerCase << " is an unrecognized option!" << endl << endl; return; } string getInput() { string fileName; cout << "Enter an input file name: ";

getline(cin, fileName); return fileName; } ifstream *openFile() { ifstream *inFile = new ifstream; string fileName; do { fileName = getInput(); inFile->open(fileName.c_str()); if (inFile->fail()) { cout << "Couldn't open file! Try Again" << endl; } } while (!inFile->good()); cout << "File Open!" << endl; return inFile; } string getOutfileName() { string outfileName; cout << "Enter output file name: "; getline(cin, outfileName); return outfileName; } ofstream *openOutFile() { ofstream *outFile = new ofstream; ofstream tempOutFile; string outFileName; bool isGood = false; do { outFileName = getOutfileName(); tempOutFile.clear(); tempOutFile.open(outFileName.c_str(), ios_base::in); if (tempOutFile.good()) { cout << "The file already exists!" << endl << endl; tempOutFile.clear(); tempOutFile.close(); } else { isGood = true; tempOutFile.clear(); tempOutFile.close(); } } while (!isGood); if (isGood) { outFile->open(outFileName.c_str()); if (outFile->fail()) { cout << "Couldn't open file!" << endl << endl; } } cout << "File Created!" << endl; return outFile; } bool loadStudentData(vector<student> &studentRecords, ifstream *inFile) { int idx = 0; stringstream fileLine; student tempStudent; string line; bool isLoaded = true; char letterInGNumber;

while (getline(*inFile, line)) { fileLine << line; getline(fileLine, tempStudent.lastName, ','); getline(fileLine, tempStudent.firstName, ','); fileLine >> letterInGNumber; fileLine >> tempStudent.gNumber; for (idx = 0; idx <= 11; idx++) { tempStudent.grades[idx] = 0; } studentRecords.push_back(tempStudent); fileLine.clear(); } cout << "Data loaded successfully!" << endl; return isLoaded; } void loadClassData(vector<student> &studentRecords, ifstream *inFile) { int idx = 0, idxTwo = 0, tempGrade = 0; long int tempGNumber = 0; stringstream *fileLine; bool numberFound = false; char letterInGNumber, trash; string line; while (getline(*inFile, line)) { numberFound = false; stringstream *fileLine = new stringstream; *fileLine << line; *fileLine >> letterInGNumber; *fileLine >> tempGNumber; *fileLine >> trash; for (idx = 0; idx <= studentRecords.size() - 1; idx++) { if (studentRecords[idx].gNumber == tempGNumber) { numberFound = true; break; } } if (numberFound) { for (idxTwo = 0; idxTwo <= 11; idxTwo++) { *fileLine >> tempGrade; *fileLine >> trash; studentRecords[idx].grades[idxTwo] = tempGrade; } } else { cout << "G Number could not be found!" << endl << endl; studentRecords.resize(studentRecords.size() - 1); } fileLine->clear(); } cout << "Data loaded successfully!" << endl; return; } string capitalize(string lowerCase) { string upperCase; upperCase.resize(lowerCase.length()); transform(lowerCase.begin(), lowerCase.end(), upperCase.begin(), ::toupper); return upperCase; } void editStudentData(vector<student> &studentRecords) { long int tempGNumber = 0, newGNumber = 0; int idx = 0, idxTwo = 0, choice = 0;

bool numberFound = false, goodInput = false; string lastName, firstName, trash, option; student newStudent; do { cout << "E D I T S T U D E N T D A T A" << endl << "===============================" << endl << endl; cout << "Enter a student G Number (without the letter G): G"; cin >> tempGNumber; getline(cin, trash); for (idx = 0; idx <= studentRecords.size() - 1; idx++) { if (studentRecords[idx].gNumber == tempGNumber) { numberFound = true; cout << "Student " << idx + 1 << " is a match!" << endl; cout << "1. Last name: " << studentRecords[idx].lastName << endl << "2. First name: " << studentRecords[idx].firstName << endl << "3. G Number: G" << studentRecords[idx].gNumber << endl << "4. Keep settings" << endl << "Enter an option \ index (1, 2, 3, or 4) to edit student data: "; cin >> choice; getline(cin, trash); if (choice == 1 || choice == 2 || choice == 3 || choice == 4) { if (choice == 1) { cout << endl << "Enter a new last name: "; getline(cin, lastName); cout << endl; studentRecords[idx].lastName = lastName; } if (choice == 2) { cout << endl << "Enter a new first name: "; getline(cin, firstName); cout << endl; studentRecords[idx].firstName = firstName; } if (choice == 3) { cout << endl << "Enter a new G Number (Without the \ letter G): G"; cin >> newGNumber; getline(cin, trash); cout << endl; studentRecords[idx].gNumber = newGNumber; } if (choice == 4) { break; } } else { cout << endl << choice << " is an unrecognized option!" << endl; } break; } else { cout << endl << "Student " << idx + 1 << " G Number is not a match! "<< endl << endl; if (idx == studentRecords.size() - 1) { do { cout << "Do you want to add the student? (Enter 'Y' or \ 'N')" << endl; getline(cin, option);

if (option.compare("y") == 0 || option.compare("Y") == 0) { newStudent.gNumber = tempGNumber; cout << "Enter the student's name: "; getline(cin, newStudent.firstName); cout << endl << "Enter the students's last name: "; getline(cin, newStudent.lastName); cout << endl; for (idxTwo = 0; idxTwo <= 11; idxTwo++) { newStudent.grades[idxTwo] = 0; } cout << "Student added!" << endl; studentRecords.push_back(newStudent); goodInput = true; } else if (option.compare("n") == 0 || option.compare("N") == 0) { goodInput = true; } else { cout << option << " is unrecognized." << endl; } } while (!goodInput); numberFound = true; } } } } while (!numberFound); return; } void editClassData(vector<student> &studentRecords) { long int tempGNumber = 0; int idx = 0, idxTwo = 0, gradeChange = 0, entryNumber = 0; bool numberFound = false; string trash; do { cout << "E D I T C L A S S D A T A" << endl << "===========================" << endl << endl; cout << "Enter a student G Number (Without the letter G) \ or '0' to exit to menu : G"; cin >> tempGNumber; getline(cin, trash); if (tempGNumber == 0) { break; } for (idx = 0; idx <= studentRecords.size(); idx++) { if (studentRecords[idx].gNumber == tempGNumber) { numberFound = true; cout << "Student " << idx + 1 << " is a match!" << studentRecords[idx].firstName << "'s grades by entry: " << endl; for (idxTwo = 0; idxTwo <= 11; idxTwo++) { cout << "Entry " << idxTwo + 1 << ": " << studentRecords[idx].grades[idxTwo] << endl; } cout << "Enter an entry number to change: "; cin >> entryNumber; getline(cin, trash);

cout << endl << "Enter the new grade(an integer): "; cin >> gradeChange; getline(cin, trash); studentRecords[idx].grades[entryNumber - 1] = gradeChange; return; } else { cout << "Student " << idx + 1 << " is not a match!" << endl << endl; } } } while (!numberFound); } int calculatePoints (vector<student> &studentRecords, int idx) { int points = 0, idxTwo = 0; for (idxTwo = 0; idxTwo <= 11; idxTwo++) { points += studentRecords[idx].grades[idxTwo]; } return points; } char assignGrade(int points) { char grade; switch (static_cast<int>(points / 100)) { case 10: grade = 'A'; break; case 9: grade = 'A'; break; case 8: grade = 'B'; break; case 7: grade = 'C'; break; case 6: grade = 'D'; break; default: grade = 'F'; break; } return grade; } void menu() { cout << "C L A S S R O O M P R O G R A M" << endl << "==================================" << endl << endl << "Available options: " << endl << " 1. LOAD_STUDENT_DATA" << endl << " 2. LOAD_CLASS_DATA" << endl << " 3. EDIT_STUDENT_DATA" << endl << " 4. EDIT_CLASS_DATA" << endl << " 5. WRITE_GRADE_REPORT" << endl << " 6. QUIT" << endl << " Enter a keyword or option index: "; return; } void writeReport(vector<student> &studentRecords, ofstream *outFile) { int idx = 0, points = 0; for (idx = 0; idx <= studentRecords.size() - 1; idx++) { *outFile << 'G' << studentRecords[idx].gNumber;

*outFile *outFile points = *outFile *outFile

<< ',' << " " << studentRecords[idx].lastName << ','; << studentRecords[idx].firstName << ',' << " "; calculatePoints(studentRecords, idx); << points << ',' << " "; << assignGrade(points) << '\n';

} cout << "Report written!" << endl; return; } void notLoaded(){ cout << "Student data must be loaded first!" << endl << endl; return; } void loadStudentMenu() { cout << "L O A D S T U D E N T D A T A" << endl << "===============================" << endl << endl; return; } void loadClassMenu() { cout << "L O A D C L A S S D A T A" << endl << "===========================" << endl << endl; return; }

You might also like