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 219 220 221 222 223 224
|
#include<iostream>
#include<string>
#include<iomanip>
using namespace std;
//------------------------------------------------------------------------------
int main()
{
const int MAX_STUDENTS = 32;
/* TODO - Step 1
Declare 2 arrays, with the data types listed below - each
holding as many values as determined by the constant declared above:
- array of string, for the student names
- array of int, for the final exam scores
*/
// Parallel array declarations
string STUDENT_NAME[MAX_STUDENTS];
int FINAL_SCORES[MAX_STUDENTS];
// These variables will hold array index values
int currentStudentIndex = 0; // used when user enters data to retain current array index
int lowestScoreIndex = 0; // array index that points to the student with the lowest score
// Loop control variable, holds 'y' if user wants to add another student
char again;
/* TODO - Step 2
Complete the DO...WHILE loop that has been started here (below this comment).
You will need to scroll DOWN, after STEP 8, where the following line is commented out:
//} while (expression);
Uncomment the line and change "expression" so the loop repeats if:
- the user indicated they want to add another student, AND
- fewer than MAX_STUDENTS have been added
*/
do
{
/* TODO - Step 3
Output prompt to tell user which student they are entering a final exam score for
*/
cout << "Enter Student #" << (currentStudentIndex + 1) << " Info" << endl;
/* TODO - Step 4
- Prompt the user for the student's name
- Read the user's answer and store to the correct array in the correct location
*/
cout << "Student Name: ";
cin >> STUDENT_NAME[currentStudentIndex];
/* TODO - Step 5
- Prompt the user for this student's final exam score
- Read the user's answer and store to the correct array in the correct location
*/
cout << "Final Exam Score: ";
cin >> FINAL_SCORES[currentStudentIndex];
/* TODO - Step 6
Write a WHILE loop to validate the user's entry. If the user's entry for the final
exam score was less than 0 or higher than 100, then:
- Display an error message (example in 2nd and 3rd student Sample Output below)
- Read the user's answer again and store to the correct array in the same location
*/
while (FINAL_SCORES[currentStudentIndex] < 0 || FINAL_SCORES[currentStudentIndex] > 100);
{
cout << "Exam score must be 0-100. Re-enter: ";
cin >> FINAL_SCORES[currentStudentIndex];
}
/* TODO - Step 7
Increment the index that is being used for the parallel arrays, readying it for
the next time around loop
*/
currentStudentIndex++;
/* TODO - Step 8
If the parallel array index is still less than the max number of students allowed
(as identified by the MAX_STUDENTS constant), then
- ask the user if they want to enter another student
- read the user's answer from the keyboard
- convert the user's answer to lower case and store it back to the loop control variable (again)
*/
if (currentStudentIndex < MAX_STUDENTS)
{
cout << "Do you want to enter another student (y/n): ";
cin >> again;
again = tolower(again);
}
cin.ignore(100, '\n');
cout << endl;
} while (again == 'y' && currentStudentIndex < MAX_STUDENTS);
// END OF THE DO...WHILE LOOP
// This is the spot that Step 2 told you to look for and change
// Display the header for the chart
cout << endl;
cout << "Student Exam Grade" << endl;
cout << "=========================" << endl;
cout << fixed;
/* TODO - Step 9
Write a for loop that will loop based on the number of students entered above.
*/
for (int i = 0; i < currentStudentIndex; i++)
{
/* TODO - Step 10
Write an if...else if...else statement to determine the current student's
final exam grade (90+ = A, 80+ = B, etc.)
NOTE: You will need to declare a variable here, with an appropriate data
type, to hold the current student's grade for their final exam score.
*/
char grade;
if (FINAL_SCORES[currentStudentIndex] >= 90)
grade = 'A';
else if (FINAL_SCORES[currentStudentIndex] >= 80)
grade = 'B';
else if (FINAL_SCORES[currentStudentIndex] >= 70)
grade = 'C';
else if (FINAL_SCORES[currentStudentIndex] >= 60)
grade = 'D';
else
grade = 'F';
/* TODO - Step 11
Output the student's name, final exam score, and final exam grade as a single line to the console
Use setw and any necessary output manipulator(s) to exactly match the Sample Output.
DO NOT include any string literals to force spacing to work!
*/
cout << left << setw(18) << STUDENT_NAME[i];
cout << right << setw(2) << FINAL_SCORES[i] << endl;
cout << right << setw(3) << grade << endl;
/* TODO - Step 12
If the score for the current student is lower than the final exam
score pointed to by lowestScoreIndex, then
- save the current loop index as the new lowestScoreIndex
*/
if (FINAL_SCORES[i] < FINAL_SCORES[lowestScoreIndex])
lowestScoreIndex = i;
// End of the for loop
}
/* TODO - Step 13
Display the line identifying the student with the lowest score
(as determined in the loop above)
*/
cout << endl;
cout << "The student with the lowest Final Exam score was: " << STUDENT_NAME[lowestScoreIndex] << endl;
cout << endl;
return 0;
}
/* Sample Output
Enter Student #1 Info
Name: Mickey
Final Exam Score: 98
Do you want to enter another student (y/n): y
Enter Student #2 Info
Name: Goofy
Final Exam Score: -20
Exam score must be 0-100. Re-enter: 53
Do you want to enter another student (y/n): y
Enter Student #3 Info
Name: Donald
Final Exam Score: 850
Exam score must be 0-100. Re-enter: 85
Do you want to enter another student (y/n): y
Enter Student #4 Info
Name: Pluto
Final Exam Score: 77
Do you want to enter another student (y/n): y
Enter Student #5 Info
Name: Gaston
Final Exam Score: 60
Do you want to enter another student (y/n): n
Student Exam Grade
=========================
Mickey 98 A
Goofy 53 F
Donald 85 B
Pluto 77 C
Gaston 60 D
The student with the lowest Final Exam score was: Goofy
Press any key to continue . . .
*/
|