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
|
#include <iostream>
#include <fstream>
#include <iomanip>
#include <string>
using namespace std;
void printGrade(int oneScore, float average);
const int MAX_SIZE = 21; //declared by professor
void readStudentData(ifstream &rss, int scores[], int id[], int &count, bool &tooMany)
{
rss.open("studentData.txt",ios::in,ios::binary);
count = 0;
int oneScore = 0;
float average = 0;
string grade;
if(rss.fail())
{
cout << "Cannot Open File. If test file has been \n"
<< "created rename it studentScoreData." << endl << endl;
exit(1);
}
else
{
while((rss >> id[count] && count < MAX_SIZE) && (rss >> scores[count] && count < MAX_SIZE))
{
count++;
}
}
}
float computeAverage(int scores[], int count[])
{
float average = 0;
int sum = 0;
for(int i = 0; i < MAX_SIZE; i++)
sum += scores[i];
average = sum / MAX_SIZE;
return 0;
}
void printTable(int scores[], int id[], int count)
{
int oneScore = 0;
float average = 0;
string grade;
cout << left << setw(9) << "ID#s" << setw(9) << "Scores" << setw(9) << "Grades" << endl << endl;
printGrade(oneScore, average);
for(int i = 0; i < MAX_SIZE; i++)
{
cout << left << setw(9) << id[i] << setw(9) << scores[i] << setw(9) << grade << endl;
}
}
void printGrade(int oneScore, float average) //can't figure out how to get this to work right.
{
oneScore = 0;
average = 0;
int count[MAX_SIZE];
int id[MAX_SIZE];
int scores[MAX_SIZE];
string grade;
computeAverage(scores,count);
for(oneScore = 0; oneScore < MAX_SIZE; oneScore++)
{
if(scores[oneScore] > average + 10)
{
grade = "outstanding";
}
else if(scores[oneScore] < average - 10)
{
grade = "unsatisfactory";
}
else
{
grade = "satisfactory";
}
// if I take this out of comment all it does is print a bunch of negative numbers and all say unsatisfactory.
// I know that this is setup right because if I hardcode an array it will do it properly. However I can't get
// this to work. Any suggestions?
// cout << left << setw(9) << id[oneScore] << setw(9) << scores[oneScore] << setw(9) << grade << endl;
}
}
int main()
{
ifstream rss;
string line;
int scores[MAX_SIZE];
int id[MAX_SIZE];
int count;
bool tooMany;
readStudentData(rss, scores, id, count, tooMany);
if (tooMany=true)
{
printTable(scores,id,count);
exit(1);
}
else
{
printTable(scores,id,count);
}
return 0;
}
| |