0

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:

output

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.

  • `for (int i = 0; (i < 10) && (!tooMany) && (!rss.eof()); ++i)` - You check if you've reached `eof` and then read from the file. That read may cause it to hit `eof` and fail so you get one bogus entry in the input. Instead do: `for (int i = 0; i < 10 && !tooMany && ss >> id[i] >> scores[i]; ++i)` - You don't need the two temporary variables either. – Ted Lyngmo Nov 07 '21 at 21:20
  • By ss do you mean rss? – Rhea Trickannad Nov 07 '21 at 21:56
  • Yeah, typo, sorry. The full loop could be: `for (int i = 0; i < 10 && !tooMany && rss >> id[i] >> scores[i]; ++i, ++count) {}` - or use `count` instead of `i`. They end up the same anyway: `for (count = 0; count < 10 && !tooMany && rss >> id[count] >> scores[count]; ++count) {}` should do it – Ted Lyngmo Nov 07 '21 at 21:56
  • Okay so I tried that and it didn't work. Did I have to change the body of the for loop for this change in the conditions? – Rhea Trickannad Nov 07 '21 at 22:12
  • Yes, the body should be empty. – Ted Lyngmo Nov 08 '21 at 05:27

0 Answers0