Grade Conversion

Hey guys,

This is homework but i'm not asking for an entire solution just a little help. This program is intended to ask the user for number grades, convert them to letter grades, and print the results.

The problem I am running into is with printing and I can't even get a consistent output. Some grades don't print, and the grades that do print are incorrect.

Any help would be extremely helpful, I am on the verge of a nervous breakdown. Thank you!

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
#include <iostream>
#include <string>
using namespace std;

int main(void)
{
	int i;
	const string GRADE_LETTERS[] = { "A", "A-", "B+", "B", "B-", "C+", "C", "C-", "D+", "D", "D-", "F" };
	const double GRADE_BOUNDS[] = { 92.0, 90.0, 87.0, 82.0, 80.0, 77.0, 72.0, 70.0, 67.0, 62.0, 60.0, 0.0 };

	const int GRADE_COUNT = sizeof(GRADE_BOUNDS) / sizeof(GRADE_BOUNDS[0]);
	const double SENTINEL = -1.0;
	const int SIZE = 4;

	double inScore;		

	double studentScores[SIZE];
	string studentGrades[SIZE];

	cout << "Enter the scores, etc., " << SENTINEL << " to end input..." << endl;
	cin >> inScore;	

	i = 0;

	while (inScore != SENTINEL)
	{
		studentScores[i] = inScore;
		++i;
		cin >> inScore;
	}

		for (i = 0; i < SIZE; ++i)
		{
			if ( studentScores[i] >= GRADE_BOUNDS[i])
			{
				cout << GRADE_LETTERS[i];
			}
		}

	system("pause");
	return 0;
}
Last edited on
1
2
3
4
5
6
7
for (i = 0; i < SIZE; ++i)
		{
			if ( studentScores[i] >= GRADE_BOUNDS[i])
			{
				cout << GRADE_LETTERS[i];
			}
		}


The first grade I entered was 88, so in the first iteration of the for loop, studentScores[0] = 88. GRADE_BOUNDS[0] is 92, so the if comparison fails and the cout of the letter grade is skipped. Then you move on to the second grade entered and nothing gets output for the first grade since you just make one comparison.

Another thing to consider is that you set the size of the studentScores array to 4, but there's nothing stopping me from entering more than 4 grades before putting in the sentinel - this will cause the program to try to go out of bounds on the array.
First, in your input loop, you should add a check for when i gets larger than 3, to prevent accidentally going over the limit of size for your array.Then, in your loop, you should instead loop on a different variable and loop until i - otherwise you will go through and access even the values that haven't been set. Finally, you may want to add a newline after outputting the grade letter, otherwise they will all merge together. I don't really get what you are doing in the for loop, either: You probably want to add in another loop so that you check all of the grade bounds and break from the loop when you find it.
Thank you for taking the time to respond. I'm still so lost. I tried rewriting a much simpler code and dumped the sentinel altogether but nothing really helped.

This one takes the input but then nothing prints out at all.

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
#include <iostream>
#include <string>

using namespace std;

int main(void)
{
	int i;
	double studentGrades[7];
	string studentLetter[7];

	const string GRADE_LETTERS[] = { "A", "A-", "B+", "B", "B-", "C+", "C", "C-", "D+", "D", "D-", "F" };
	const double GRADE_BOUNDS[] = { 92.0, 90.0, 87.0, 82.0, 80.0, 77.0, 72.0, 70.0, 67.0, 62.0, 60.0, 0.0 };
	const int GRADE_COUNT = sizeof(GRADE_BOUNDS) / sizeof(GRADE_BOUNDS[0]);
	
	for(i=0; i<7; i++)
	{
		cout << "Grade. " << endl; 
		cin >> studentGrades[i];
	}

	for(i=0; i<7; i++)
	{
		if (studentGrades[i] >= GRADE_BOUNDS[i])
		{
			studentLetter[i] = GRADE_LETTERS[i];
			break;
		}
		cout << studentLetter[i];
	}

	return 0;
}
You're still just comparing the grade entered against the element in the Grade Bounds with the same subscript. You need to be checking the grade to see if it's > 92, if no, then if it's greater than 90 etc.
So an else loop for every bound?
No, an inner for loop is required:
22
23
24
25
26
27
28
29
30
31
for (i = 0; i < 7; ++i) {
    for (int j = 0; j < GRADE_COUNT; ++j) {
        if (studentGrades[i] >= GRADE_BOUNDS[j]) {
            studentLetter[i] = GRADE_LETTERS[j];
            break;
        }
    }
    // just making the output be sensical
    cout << i+1 << "). " << studentLetter[i] << '\n';
}
Last edited on
You are kind and wonderful.
Topic archived. No new replies allowed.