I have been working on this for a very long time. I have also searched for examples but they do not help.
Here is what i need to do...I need to take the students score and compare it to average and print out the corresponding grade. I am having a hard time doing that. I would appreciate any help or direction, please. The out put is also included
Here is my code.
#include <iostream>
#include <iomanip>
#include <cmath>
#include <fstream>
#include <string>
using namespace std;
int openFile(ifstream &rss, string file);
void displayHeader();
void readStudentData(ifstream &rss, int scores[], int id[], int &count, bool &tooMany);
float computeAverage(int scores[], int count);
void printGrade(int oneScore, float average);
void printTable(int id[], int scores[], int count);
const int MAX_SIZE = 21;
int main()
{
ifstream rss;
string file;
string grades;
int scores[MAX_SIZE];
int id[MAX_SIZE];
int i , count = 0, oneScore = 0;
float avg = 0, average = 0;
bool tooMany;
//Open file
cout << "\nEnter input filename: ";
cin >> file;
openFile(rss,file);
displayHeader();
readStudentData(rss,scores,id,count,tooMany);
computeAverage(scores,count);
printGrade(oneScore,average);
printTable(id,scores,count);
if (tooMany = true)
{
cout << "\nAverage score is: " << computeAverage(scores,count) << "%" << endl;
cout << "\n***Array is filled! Remaining scores will not be stored.***";
cout << endl;
}
else
printTable(id,scores,count);
}
void printTable(int id[], int scores[], int count)
{
int i;
int oneScore;
float average;
string grades;
for (int i = 0; i < MAX_SIZE; i++)
{
printGrade(oneScore,average);
cout << id[i] << setw(17) << scores[i] << setw(17) << grades << endl;
}
}
//This section is whats giving me a hard time.
void printGrade(int oneScore, float average)
{
string grades, file;
ifstream rss;
int scores[MAX_SIZE];
rss >> scores[oneScore];
oneScore = 0;
while (!rss.eof() && oneScore < MAX_SIZE)
{
if (oneScore > average + 10)
{
grades = "OUTSTANDING";
}
else if (oneScore < average - 10)
{
grades = "UNSATISFACTORY";
}
else
{
grades = "SATISFACTORY";
}
oneScore++;
rss >> scores[oneScore];
}
}
float computeAverage(int scores[], int count)
{
int id[MAX_SIZE];
int sum = 0;
float average;
int i;
for(int i = 0; i < MAX_SIZE; i++)
{
sum += scores[i];
count++;
}
average = sum / MAX_SIZE;
return average;
}
void displayHeader()
{
//Output header
cout << "\nStudent ID" << setw(13) << "Scores" << setw(13) << "Grades"<< endl;
}
void readStudentData(ifstream &rss, int scores[], int id[], int &count, bool &tooMany)
{
int tempID;
int tempScore;
//Get input
rss >> tempID >> tempScore;
while(!rss.eof() && (count < MAX_SIZE))
{
id[count] = tempID;
scores[count] = tempScore;
count++;
rss >> tempID >> tempScore;
if (count > MAX_SIZE)
{
tooMany = true;
}
else
tooMany = false;
}
}
int openFile(ifstream &rss, string file)
{
rss.open(file.c_str());
if (rss.fail())
{
cerr << "***!!! ERROR: Cannot open file for input !!!***" << endl;
return EXIT_FAILURE; // failure return
}
}
This is an example of my output
student ID Scores Grades
1001 89
1008 85
1024 98
1029 76
1053 0
1189 38
again, any help would be appreciated.