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
|
#include<iomanip>
#include<fstream>
#include<iostream>
using namespace std;
// Global Named Constants - No changes allowed
const int MAX_STUDENTS = 20; // Maximum number of students in file
const int EXAM_COUNT = 4; // Number of exams in the file
const char FILE_NAME[20] = "ExamScores.txt"; // Name of input file
// Function Prototypes - No changes allowed
int ReadExams(char[][21], int[], int[][EXAM_COUNT]);
int DisplayMenu();
void DisplayReport(int, char[][21], int[][EXAM_COUNT]);
int findStudent(int[], int);
void DisplayScores(int, int[][EXAM_COUNT]);
// Make no changes in the main function
int main()
{
int studentCount = 0;
char studentNames[MAX_STUDENTS][21];
int studentNumbers[MAX_STUDENTS];
int exams[MAX_STUDENTS][EXAM_COUNT];
int studentSub;
int choice;
// Read file information into arrays and retrieve the number of
// students in the file
studentCount = ReadExams(studentNames, studentNumbers, exams);
// If the file contained students then display a menu
if(studentCount > 0)
{
do
{
choice = DisplayMenu();
if(choice == 1)
{
DisplayReport(studentCount, studentNames, exams);
system("pause");
}
else if(choice == 2)
{
studentSub = findStudent(studentNumbers, studentCount);
if(studentSub != -1)
{
cout << "\n\nScores for " << studentNames[studentSub] << " ";
DisplayScores(studentSub, exams);
cout << "\n\n";
}
else
{
cout << "\n\nStudent number not found\n\n";
}
system("pause");
}
else if(choice != 3)
{
cout << "\n\nInvalid choice\n\n";
}
}while(choice != 3);
}
else
{
cout << "\n\nExam Scores is empty or Error Processing the File\n\n";
}
system ("pause");
return 0;
}
// Read student numbers and names and scores into the 3 arrays
int ReadExams(char studentNames[][21], int studentNumbers[], int grades[][EXAM_COUNT])
{
int count = 1;
ifstream studentFile;
studentFile.open(FILE_NAME);
while(!studentFile.eof())
{
for(int i = 0; i < 8; i++)
{
studentFile >> studentNumbers[count];
studentFile >> studentNames[count];
for(int i = 0; i < EXAM_COUNT; i++)
{
studentFile >> grades[count][EXAM_COUNT];
}
count++;
cout << studentNumbers[count] << studentNames[count][21] << grades[count][EXAM_COUNT] << endl;
}
studentFile.close();
cout << endl;
return count;
}
}
// Display a menu and return the user's choice
int DisplayMenu()
{
int choice;
cout << "Choose an option:\n\n"
<< "1 = Display report for all students\n"
<< "2 = Select a student to display\n"
<< "3 = Quit\n\n -----> ";
do
{
if(!(cin >> choice))
{
cout << "Please choose one of the options!\n" << endl;
cin.clear();
cin.ignore(INT_MAX, '\n');
choice = 0;
continue;
}
else if(choice > 3 || choice < 1)
{
cout << "Please choose one of the options!\n" << endl;
choice = 0;
continue;
}
else
{
return choice;
}
}while(!(choice = 0));
}
// Retrieve a student number from user and search the studentNumbers array
// for that student. Return the subscript if found, otherwise return -1
int findStudent(int studentNumbers[], int count)
{
int choice;
cout << "Choose a students number to view: ";
for(count = 0; count < 8; count++)
{
cout << studentNumbers[count];
}
for(int i = 0; i < 8; i++)
{
if(!(cin >> choice))
{
cout << " Student not found! ";
cin.clear();
cin.ignore(INT_MAX, '\n');
choice = -1;
}
else if(choice = studentNumbers[count])
{
i = 8;
return studentNumbers[count];
}
}
}
// Display a report for all students
// Display the student name and call DisplayScores to display the scores and average
void DisplayReport(int studentCount, char studentNames[][21], int exams[][EXAM_COUNT])
{
int i = 0;
for(i; i < 8; i++)
{
cout << studentNames[i][21] << setw(21) << exams[i][EXAM_COUNT] << endl;
}
}
// Display one student's scores, drop lowest score and display student average
void DisplayScores(int student, int exams[][EXAM_COUNT])
{
int averageScore;
for(student = 0; student < 8; student++)
{
for(exams[student][EXAM_COUNT]; exams[student][EXAM_COUNT] >= exams[student][0]; exams[student][EXAM_COUNT]--)
{
if(exams[student][EXAM_COUNT] < exams[student][EXAM_COUNT]--)
{
averageScore = exams[student][EXAM_COUNT];
}
}
}
}
|