printing grade

closed account (42ApX9L8)
This program works EXCEPT for the fact that I can't get it to dipslay the grades in accordance with the scores.

This is the section I think I need to work with:
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
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";
		}
//		cout << left << setw(9) << id[oneScore] << setw(9) << scores[oneScore] << setw(9) << grade << endl;
	}
	
}

Though I'm not completely sure.

Here is the complete code.
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;
}
try this...

1
2
3
4
5
6
7
8
9
10
11
12
if(scores[oneScore] > (average + 10))
{
	grade = "outstanding";
}
else if(scores[oneScore] < (average - 10))
{
	grade = "unsatisfactory";
}
else
{
	grade = "satisfactory";
}
That's not the problem...

1. The while loop is very bad, it may attempt to write to out of bound data space, it also looks really ugly
2. computeAverage, you are returning 0...I think you meant return average;
3. printgrade():

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
void printGrade(int oneScore, float average)  //why are you passing these? You just
{
	oneScore = 0; //set them to 0
	average = 0;
	int count[MAX_SIZE]; //these aren't initialized, so they are garbage
	int id[MAX_SIZE];
	int scores[MAX_SIZE];
	string grade; //???

	computeAverage(scores,count); //giving compute average garbage data

	for(oneScore = 0; oneScore < MAX_SIZE; oneScore++)
	{
		if(scores[oneScore] > average + 10) //these will work, but scores is garbage
		{
			grade = "outstanding";
		}
		else if(scores[oneScore] < average - 10)
		{
			grade = "unsatisfactory";
		}
		else
		{
			grade = "satisfactory";
		}

	}
	
}
closed account (42ApX9L8)
Here is printGrade() slightly revised:

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
void printGrade(int oneScore, float average)
{
	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";
		}

//		cout << left << setw(9) << id[oneScore] << setw(9) << scores[oneScore] << setw(9) << grade << endl;
	}
	
}


Scores is still garbage. Each function has parameters that it passes because that is what my book requires. If it were up to me I wouldn't have parameters and I would just have one long and ugly looking main function that did everything. However, it is not up to me. How can I get scores to equal what it is supposed to instead of being garbage?
scores is an unitialized array within the printGrade function. printTable needs to pass the array to printGrade and assuming that the file i/o was successful it should work. Reread firedraco's post. The revision that you made didn't take all of his advice into consideration.
Topic archived. No new replies allowed.