I have this code:
#include <iostream>
#include <fstream>
#include <iomanip>
#include <string>
using namespace std;
void readStuData(ifstream& rss, int scores[],
int id[], int& count, bool& tooMany);
float mean(int scores[], int count);
void printTable(int score[], int ID[], int count);
void printGrade(int oneScore, float average);
int main() {
const int MAX_SIZE = 10;
ifstream rss;
int numStuds = 0;
int studScores[MAX_SIZE];
int studIDs[MAX_SIZE];
bool tooMany = false;
int oneScore;
float average;
readStuData(rss, studScores, studIDs, numStuds, tooMany);
printTable(studScores, studIDs, numStuds);
for (int i = 0; i < MAX_SIZE; ++i) {
oneScore = studScores[i];
}
average = mean(studScores, numStuds);
printGrade(oneScore, average);
return 0;
}
void readStuData(ifstream& rss, int scores[],
int id[], int& count, bool& tooMany) {
int studScore;
int studID;
rss.open("StudentScores.txt");
if (!rss.is_open()) {
cout << "ERROR: Could not open file." << endl;
}
tooMany = false;
count = 0;
for (int i = 0; (i < 10) && (!tooMany) && (!rss.eof()); ++i) {
rss >> studID;
id[i] = studID;
rss >> studScore;
scores[i] = studScore;
++count;
}
if (count > 10) {
tooMany = true;
}
if (tooMany) {
cout << "ERROR: Too many inputs from file." << endl;
}
rss.close();
}
float mean(int scores[], int count) {
float sum = 0.0;
int i;
for (i = 0; i < count; ++i) {
sum += scores[i];
}
return sum / count;
}
void printGrade(int oneScore, float average) {
if (oneScore > (average + 10)) {
cout << setw(15) << left << "Outstanding" << endl;
}
else if ((oneScore > (average - 10)) && (oneScore < (average + 10))) {
cout << setw(15) << left << "Satisfactory" << endl;
}
else if (oneScore < (average - 10)) {
cout << setw(15) << left << "Unsatisfactory" << endl;
}
}
void printTable(int score[], int ID[], int count) {
cout << "Student ID" << '|';
cout << setw(5) << "Score" << '|';
cout << setw(10) << "Grade" << endl;
cout << setfill('-') << setw(33) << "" << endl;
cout << setfill(' ');
for (int i = 0; i < count; ++i) {
cout << setw(10) << left << ID[i] << '|';
cout << setw(5) << left << score[i] << '|';
printGrade(score[i], mean(score, count));
}
}
Here is the output:
Student ID|Score| Grade
---------------------------------
68194805 |73 |Satisfactory
45991296 |80 |Satisfactory
32948569 |82 |Satisfactory
22399466 |93 |Outstanding
97139677 |70 |Satisfactory
83812257 |77 |Satisfactory
30544477 |89 |Outstanding
97859404 |43 |Unsatisfactory
40787045 |71 |Satisfactory
73234827 |95 |Outstanding
Oustanding
The point of this program is to read student IDs and scores from a text file, take the average of all the scores, use the average to determine whether the score deserves an "Outstanding" grade, an "Unsatisfactory" grade, or a "Satisfactory" grade, and organize them into a three-column table. I have accomplished this except for the last "Outstanding" at the bottom, which is a repeat of the row above. This is the only issue I have and I cannot figure out why it's doing this. The "Outstanding" at the very bottom won't go away even when I deleted a part of the for loop.
Edit: I don't know how to make it all into plain text while still keeping the formatting readable.