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 205 206 207 208 209 210 211 212 213 214 215 216 217 218
|
#include <iostream> // cout, cin
#include <cstdlib> // exit()
#include <string> // strings
#include <fstream> // file processing
#include <iomanip> // stream manipulation
using namespace std;
const int MAX_STUDENTS = 25; // We will not process more than 25 students even if the file contains more
const int MAX_GRADES = 5; // Each student has exactly 5 grades
const string FILENAME = "NamesGrades.txt"; // The name of the file that you will read
int loadStudentNamesGrades(string students[], int grades[][MAX_GRADES], string fileName, int maxStudents);
void displayAverages(string students[], int grades[][MAX_GRADES], int studentCount);
void displayMax(string students[], int grades[][MAX_GRADES], int studentCount);
void displayMin(string students[], int grades[][MAX_GRADES], int studentCount);
char getLetterGrade(double grade);
int getLongestNameLength(string students[], int studentCount);
int main()
{
int studentCount = 0; // number of students processing
string students[MAX_STUDENTS]; // Name of student
int grades[MAX_STUDENTS][MAX_GRADES]; // table of grades
char choice; // choice from user
studentCount = loadStudentNamesGrades(students, grades, FILENAME, MAX_STUDENTS);
do
{
cout << "\nGrade Report Program\n\n";
cout << "\t1. Display Average Grade\n";
cout << "\t2. Display Maximum Grade\n";
cout << "\t3. Display Manimum Grade\n";
cout << "\t4. Quit Program\n";
cout << "Enter your choice (1-4): ";
cin >> choice;
while (getchar() != '\n'); // flushing the line
// Process the choice
switch (choice)
{
case '1': // average
displayAverages(students, grades, studentCount);
break;
case '2': // maximum
displayMax(students, grades, studentCount);
break;
case '3': // minimum
displayMin(students, grades, studentCount);
break;
case '4': // quit
break;
default:
cout << "Invalid option; please try again.\n\n";
}
} while (choice != '4');
cout << endl;
system("PAUSE");
cout << "Press any key to continue";
cin.get();
return 0;
}
int loadStudentNamesGrades(string students[], int grades[][MAX_GRADES], string fileName, int maxStudents)
{
ifstream inFile; // input file stream
string firstName; // first name of student
string lastName; // last name of student
int actualGrades; // number of grades read
inFile.open(fileName.c_str()); // *** CHANGED TO C-STRING RATHER THAN STRING
if (!inFile)
{
cout << "Could not open file" << endl;
system("PAUSE");
exit(1);
}
// Loop through each row
for (int i = 0; i < maxStudents && (inFile >> firstName >> lastName);
i++, actualGrades++)
{
for (int j = 0; j < MAX_GRADES; j++)
{
inFile >> grades[i][j];
}
// combine first and last name
students[i] = firstName + " " + lastName;
}
inFile.close();
return actualGrades;
}
void displayAverages(string students[], int grades[][MAX_GRADES], int studentCount)
{
double average; // average of grades for each student
int total; // total of all grades
// get length of the longest first name
int maxLength = getLongestNameLength(students, studentCount);
// set up table header
cout << setprecision(1) << fixed << showpoint;
cout << "\n\n Grade Averages\n";
cout << setw(maxLength + 1) << left << "Student Name"
<< setw(4) << right << "Average"
<< setw(3) << left << "Letter Grade" << endl;
for (int i = 0; i < studentCount; i++)
{
cout << setw(maxLength + 1) << left << students[i];
total = 0; // accumulator set to 0
for (int j = 0; j < MAX_GRADES; j++)
{
total += grades[i][j];
}
average = static_cast<double>(total) / MAX_GRADES;
cout << setw(4) << right << average
<< setw(3) << getLetterGrade(static_cast<int>(average))
<< endl;
}
}
void displayMax(string students[], int grades[][MAX_GRADES], int studentCount)
{
int maxGrade; //max grade so far
int maxLength; // getLongestNameLength(firstName, studentCount);
// set up header
cout << "\n\nMax Grades\n";
cout << setw(maxLength + 1) << left << "Student Name"
<< setw(4) << right << "Max"
<< setw(3) << "Letter Grade" << endl;
for (int i = 0; i < studentCount; i++)
{
cout << setw(maxLength + 1) << left << students[i];
maxGrade = grades[i][0];
for (int j = 1; j < MAX_GRADES; j++)
{
if (maxGrade < grades[i][j])
{
maxGrade = grades[i][j];
}
}
cout << setw(4) << right << maxGrade
<< setw(3) << getLetterGrade(maxGrade) << endl;
}
}
void displayMin(string students[], int grades[][MAX_GRADES], int studentCount)
{
int minGrade; //min grade so far
int maxLength; // getLongestNameLength(firstName, studentCount);
cout << "\n\nMin Grades\n";
cout << setw(maxLength + 1) << left << "Student Name"
<< setw(4) << right << "Min"
<< setw(3) << "Letter Grade" << endl;
for (int i = 0; i < studentCount; i++)
{
cout << setw(maxLength + 1) << left << students[i];
minGrade = grades[i][0];
for (int j = 1; j > MAX_GRADES; j++)
{
if (minGrade < grades[i][j])
{
minGrade = grades[i][j];
}
}
cout << setw(4) << right << minGrade
<< setw(3) << getLetterGrade(minGrade) << endl;
}
} // *** ADDED END OF FUNCTION
char getLetterGrade(double grade)
{
if (grade > 90)
return 'A'; // *** USE ' FOR CHAR, NOT "
else if (grade > 80)
return 'B';
else if (grade > 70)
return 'C';
else if (grade > 60)
return 'D';
else
return 'F';
}
int getLongestNameLength(string students[], int studentCount)
{
int maxLength = 0; // max length so far
for (int i = 0; i < studentCount; i++)
{
// is it longer?
if (students[i].length() > maxLength) // *** CHANGED to students
{
maxLength = students[i].length(); // *** CHANGED to students[i]
}
}
return maxLength;
}
|