Here is my code:
#include <iostream>
#include <cstdlib>
#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));
}
}
This is the output I keep getting:
How do I get rid of the last "Outstanding"? I don't know what I am doing wrong, but when I get rid of the for loop in the printTable() function and put in 8 for i in the printGrade() function, I still get the same thing. I know there is a problem, but I cannot figure out what it is and what I must do to remove it. Can someone please help me debug this?
Edit: I need to point out that the arrays need to be populated. Also, the unwanted output at the end is the only issue I have.