#include #include #include #include using namespace std; /* BEGIN Structs */ struct courses { string studentName[7]; string studentID[7]; int score[7]; }; struct results { string studentID[7]; int average[7]; }; /* END Structs */ /* BEGIN Function Prototypes */ int main(void); void menuStart(void); int menuChoice(void); void initializeStructs(courses course[], results& resultSet); void calcAverages(courses course[], results& resultSet, int& bestStudent); void writeDatFiles(courses course[], results& resultSet, int& bestStudent); int translateStudent(int student); void displayCourseStruct(courses course[]); void displayResultSet(results& resultSet); void displayBestStudent(courses course[], results& resultSet, int& bestStudent); void displayDatFile(string filename); /* END Function Prototypes */ /** * Main function */ int main(void) { menuStart(); } /** * Starts the menu while loop and switches based on the user's choice */ void menuStart(void) { /* Variable declarations */ int choice; int bestStudent; courses course[2]; results resultSet; /* Menu while and switch statements */ while (choice != 7) { choice = menuChoice(); switch(choice) { case 1: initializeStructs(course, resultSet); calcAverages(course, resultSet, bestStudent); writeDatFiles(course, resultSet, bestStudent); break; case 2: displayCourseStruct(course); break; case 3: displayResultSet(resultSet); break; case 4: displayBestStudent(course, resultSet, bestStudent); break; case 5: displayDatFile("pass.dat"); break; case 6: displayDatFile("fail.dat"); break; case 7: ; break; default: cout << "Invalid choice. Please choose a valid menu item.\n\n"; break; }; } exit(0); } /** * Displays the menu for the program * * @return choice the user's chosen menu item */ int menuChoice(void) { int choice; cout << "1. Create and initialize the COURSE1 and COURSE2 arrays\n"; cout << "2. Display the current contents of all the arrays in COURSE1 and COURSE2\n"; cout << "3. Display the Result set\n"; cout << "4. View the details of the Best Student\n"; cout << "5. View PASS.DAT\n"; cout << "6. View FAIL.DAT\n"; cout << "7. Quit\n\n"; cout << "Choose 1-7: "; cin >> choice; cout << endl; return choice; } /** * Initializes course and resultSet structs * * @param course[] course struct to initialize class arrays in * @param resultSet results struct to initialize */ void initializeStructs(courses course[], results& resultSet) { course[0].studentName[0]="Alice"; course[0].studentName[1]="Bob"; course[0].studentName[2]="Chris"; course[0].studentName[3]="David"; course[0].studentName[4]="Leo"; course[0].studentName[5]="Mike"; course[0].studentName[6]="Stephen"; course[0].studentID[0] = "001221"; course[0].studentID[1] = "001265"; course[0].studentID[2] = "001283"; course[0].studentID[3] = "001241"; course[0].studentID[4] = "001298"; course[0].studentID[5] = "001237"; course[0].studentID[6] = "001243"; course[0].score[0] = 38; course[0].score[1] = 47; course[0].score[2] = 85; course[0].score[3] = 72; course[0].score[4] = 40; course[0].score[5] = 64; course[0].score[6] = 96; course[1].studentName[0] = "Alice"; course[1].studentName[1] = "Mike"; course[1].studentName[2] = "David"; course[1].studentName[3] = "Stephen"; course[1].studentName[4] = "Bob"; course[1].studentName[5] = "Chris"; course[1].studentName[6] = "Leo"; course[1].studentID[0] = "001221"; course[1].studentID[1] = "001237"; course[1].studentID[2] = "001241"; course[1].studentID[3] = "001243"; course[1].studentID[4] = "001265"; course[1].studentID[5] = "001283"; course[1].studentID[6] = "001298"; course[1].score[0] = 42; course[1].score[1] = 55; course[1].score[2] = 78; course[1].score[3] = 98; course[1].score[4] = 45; course[1].score[5] = 99; course[1].score[6] = 35; resultSet.studentID[0] = "001221"; resultSet.studentID[1] = "001237"; resultSet.studentID[2] = "001241"; resultSet.studentID[3] = "001243"; resultSet.studentID[4] = "001265"; resultSet.studentID[5] = "001283"; resultSet.studentID[6] = "001298"; cout << "Arrays initialized.\n\n"; return; } /** * Calculates average scores and sets the bestStudent variable to the array * element of the best student. * * @param course[] course struct to calculate average scores in * @param resultSet results struct to place averages in * @param bestStudent integer to represent the bestStudent element */ void calcAverages(courses course[], results& resultSet, int& bestStudent) { // Calculates the average for each student for (int i = 0; i < 7; i++) resultSet.average[i] = ((course[0].score[translateStudent(i)] + course[1].score[i]) / 2); // Determines the best student bestStudent = 0; for (int i = 0; i < 7; i++) { if (resultSet.average[i] > resultSet.average[bestStudent]) bestStudent = i; } return; } /** * Writes the pass and fail dat files according to each student's grades * * @param course[] course struct from which to pull data from * @param resultSet results array from which to identify the best student * @param bestStudent integer to represent the bestStudent element */ void writeDatFiles(courses course[], results& resultSet, int& bestStudent) { // Two diffferent files for passing and failing students ofstream outPassFile; outPassFile.open("pass.dat"); if (!outPassFile) { cout << "Error: Cannot open \"pass.dat\" for output." << endl; return; } ofstream outFailFile; outFailFile.open("fail.dat"); if (!outFailFile) { cout << "Error: Cannot open \"fail.dat\" for output." << endl; return; } for (int i = 0; i < 7; i++) { // Different actions if passing or not if (resultSet.average[i] >= 45) { if (i == bestStudent) outPassFile << course[1].studentName[i] << " - The Best Student" << endl; else outPassFile << course[1].studentName[i] << endl; outPassFile << course[1].score[i] << endl; outPassFile << course[0].score[translateStudent(i)] << endl; outPassFile << course[1].score[i] << endl; outPassFile << resultSet.average[i] << "\n\n"; } else { outFailFile << course[1].studentName[i] << endl; outFailFile << course[1].studentID[i] << endl; outFailFile << course[0].score[translateStudent(i)] << endl; outFailFile << course[1].score[i] << endl; outFailFile << resultSet.average[i] << "\n\n"; } } // Close both files outPassFile.close(); outFailFile.close(); return; } /** * Translates the element value passed into the corresponding student's element * for the second struct. * * @param student student's element from the first struct to translate * @return the corresponding student's element from the second struct */ int translateStudent(int student) { switch(student) { case 0: return 0; break; case 1: return 5; break; case 2: return 3; break; case 3: return 6; break; case 4: return 1; break; case 5: return 2; break; case 6: return 4; break; }; } /** * Displays the arrays in the course structs * * @param course[] course struct to display arrays from */ void displayCourseStruct(courses course[]) { // Cycles through both course structs for (int i = 0; i < 2; i++) { cout << "Course #" << (i + 1) << endl; cout << "---------" << endl; // Displays each array in a single course struct for (int j = 0; j < 7; j++) { cout << course[i].studentName[j] << endl; cout << course[i].studentID[j] << endl; cout << course[i].score[j] << endl << endl; } } return; } /** * Displays the resultSet struct. * * @param resultSet resultSet array to display */ void displayResultSet(results& resultSet) { // Displays the elements of the arrays in the resultSet struct for (int i = 0; i < 7; i++) { cout << "ID: " << resultSet.studentID[i] << endl; cout << "Average: " << resultSet.average[i] << endl << endl; } return; } /** * Displays the details of The Best Student * * @param course[] course struct from which to pull data from * @param resultSet results struct from which to identify the best student * @param bestStudent integer to represent the bestStudent element */ void displayBestStudent(courses course[], results& resultSet, int& bestStudent) { cout << "The Best Student title goes to:" << endl << endl; cout << "Name: " << course[1].studentName[bestStudent] << endl; cout << "ID: " << course[1].studentID[bestStudent] << endl; cout << "Course 1 score: " << course[0].score[translateStudent(bestStudent)] << endl; cout << "Course 2 score: " << course[1].score[bestStudent] << endl; cout << "Average Score: " << resultSet.average[bestStudent] << "\n\n"; return; } /** * Displays the contents of the passed filename * * @param filename datfile to display */ void displayDatFile(string filename) { // The filename for reading is passed to the function ifstream inFile; string studentName, studentID; int scoreOne, scoreTwo, averageScore; inFile.open(filename.c_str()); if (!inFile) { cout << "Error: Cannot open " << filename << " for input." << endl; return; } while (inFile.good()) { inFile >> studentName; inFile >> studentID; if (studentID == "-") { inFile.ignore(30,'\n'); studentName = studentName + " - The Best Student"; inFile >> studentID; } inFile >> scoreOne; inFile >> scoreTwo; inFile >> averageScore; inFile.ignore(30,'\n'); // Checks for the stray endl at EOF before displaying any further data if (inFile.eof()) break; cout << "Student Name: " << studentName << endl; cout << "Sutudent ID: " << studentID << endl; cout << "Course 1 Score: " << scoreOne << endl; cout << "Course 2 Score: " << scoreTwo << endl; cout << "Average Score: " << averageScore << "\n\n"; }; inFile.close(); return; }