Letter Grades Counter does not work

Hi everyone,

This program is supposed to count how many A B C D F, get the highest, lowest and average. I haven't figure out the highest and lowest yet but the LetterGradeCounter does not work. Is my code okay ?
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
  #include <iostream>
#include <iomanip>
#include <fstream>
#include <string>
#include <cstring>

using namespace std;
typedef char Str[10];

float Average(int grades[], int count)
{
	float total = 0;
	for (int i = 0; i < count; i++)
		total += grades[i];
	return total / count;
}
void Print(Str names[], int grades[], int count)
{
	for (int i = 0; i < count; i++)
	{
		cout << left << setw(20) << names[i] <<
			right << setw(4) << grades[i] << endl;
	}
	cout << endl;
}

void BubbleSortByGrade(Str names[], int grades[], int count)
{
	for (int i = 0; i < count - 1; i++)
		for (int j = 0; j < count - 1; j++)
			if (grades[j + 1] > grades[j])
			{
				Str tmpname;
				strcpy(tmpname, names[j]);
				strcpy(names[j], names[j + 1]);
				strcpy(names[j + 1], tmpname);

				int tmpgrade = grades[j];
				grades[j] = grades[j + 1];
				grades[j + 1] = tmpgrade;
			}

}

void BubbleSortByName(Str names[], int grades[], int count)
{
	for (int j = 0; j < count - 1; j++)
		for (int j = 0; j < count - 1; j++)
		{
			if (strcmp(names[j + 1], names[j]) < 0)
			{
				Str tmp;
				strcpy(tmp, names[j]);
				strcpy(names[j], names[j + 1]);
				strcpy(names[j + 1], tmp);

				int tmpgrade = grades[j];
				grades[j] = grades[j + 1];
				grades[j + 1] = tmpgrade;
			}
		}

}
int CountLetterGrades(int grades[], int count, int high, int low)
{
	int c = 0;
	for (int i = 0; i < count; i++)
	{
		if (grades[i] >= low && grades[i] <= high) c++;
	}
	return c;
}
void PrintGrades(int grades[], int count)
{
	cout << "Number of As: " << CountLetterGrades(grades, count, 90, 100) << endl;
	cout << "Number of Bs: " << CountLetterGrades(grades, count, 80, 89) << endl;
	cout << "Number of Cs: " << CountLetterGrades(grades, count, 70, 79) << endl;
	cout << "Number of Ds: " << CountLetterGrades(grades, count, 60, 69) << endl;
	cout << "Number of Fs: " << CountLetterGrades(grades, count, 0, 59) << endl;
	cout << fixed << setprecision(2) << "Average: " << Average(grades, count);
}
int main()
{
	Str names[20];
	int grades[20];
	int count = 0;

	char fn[30];
	cout << "enter file name: ";
	cin >> fn;
	ifstream infile(fn);
	if (!infile)
	{
		cout << "bad file" << endl;
		return -1;
	}
	for (; infile >> names[count] >> grades[count]; count++); //
	infile.close();
	Print(names, grades, count);
	BubbleSortByGrade(names, grades, count);
	Print(names, grades, count);
	BubbleSortByName(names, grades, count);
	Print(names, grades, count);
	PrintGrades(grades, count);
}
You're passing high and low in the wrong order (or maybe you should change the function signature so that low comes first).
Thank you very much Dutch, you are right. how silly me haha
In general, when something seems impossible, you're missing some detail or making an incorrect assumption. It can be hard to spot an error like that. A second pair of eyes can be useful. :-)
A second pair of eyes can be useful.

Far more useful is your debugger. Learn to use it to step through your code, and examine the values of the different variables to see what's happening, and why.

You'd have been able to find the problem pretty quickly on your own if you'd done this, without having to wait for other people to look at it.
Topic archived. No new replies allowed.