Help with grade calculator using arrays

I am writing a program that is supposed to (set a max possible points and find the number that refers to A, B, C D, F) compare an entered grade to a number that in turn corresponds to a letter grade and print it out. Here is what I have so far. Whenever I run the program, it doesn't print out any letter grades and I'm not sure if my if statement is right or not.

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

int main() {
	
	double possiblePoints = 0.0;
	double recievedPoints = 0.0;
	double gradePoints[5] = { 0 };
	char letterGrade[5] = { 'A', 'B', 'C', 'D', 'F' };
	double percent = .9;
	bool found = false;
	
	cout << "How many possible points are there? : " << endl;
	cin >> possiblePoints;

	cout << "How many points did the student receive?" << endl;
	cin >> recievedPoints;

	cout << endl;

	for (int grade = 0; grade < 5; grade++) {

		gradePoints[grade] = possiblePoints * percent;

		percent = percent - .1;
	}

	for (int final = 0; final < 5 && found == false; final++) {
		
		if (gradePoints[final] == recievedPoints) {
			cout << letterGrade[final];
			found = true;
		}
	}

	if (found == false) {
		cout << "Invalid" << endl;
	}

	system("pause");
	return 0;
}
Last edited on
You only ask the user to input one score. So even though your loop iterates 5 times, it's going to calculate the same value each time.
I guess I am still a little confused. The user enters the total possible points for example 400. Then the points the student received, 350. The programs take 400 and multiplies it by .9, .8, etc. Then I need it to compare the received points with the total points array and compare that to the letter grade array to print out my letter grade.
A, sorry, I misunderstood. If the intention is just to grade one result, then that's not a problem.

I'd recommend using your debugger to step through the code to see exactly what it's doing, and why it's not behaving as you'd expect.
Last edited on
Hello brownt898,

Looking over your program I feel that there are some parts that you are not understanding. This is mostly your program with a few upgrades:

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

//using namespace std;  // <--- Best not to use.
// A recent post that is worth reading. http://www.cplusplus.com/forum/beginner/258335/

int main()
{
	constexpr size_t MAX_ARRAY_SIZE{ 5 };  // <--- Can be moved above "main"if needed.

	double possiblePoints{ 400.0 };
	double recievedPoints{350.0};
	double gradePoints[MAX_ARRAY_SIZE]{};
	char letterGrade[MAX_ARRAY_SIZE] = { 'A', 'B', 'C', 'D', 'F' };
	double percent{ 0.9 };
	bool found{};  // <--- The same as saying = false.

	std::cout << "\n How many possible points are there?: " << possiblePoints << '\n'; // <--- Used for testing. Reverse comments or remove when finished.
	//std::cout << "How many possible points are there?: ";
	//std::cin >> possiblePoints;

	while (!std::cin)
	{
		std::cout << "\n    Invalid entry! try again: ";

		std::cin.clear(); // <--- Clears and resets the state bits on the stream.
		std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');  // <--- Requires header file <limits>. Clears the input buffer.

	std::cout << "How many possible points are there?: ";
	std::cin >> possiblePoints;
	} // <--- The same thing should follow the next "std::cin".

	std::cout << " How many points did the student receive? " << recievedPoints << '\n'; // <--- Used for testing. Reverse comments or remove when finished.
	//std::cout << "How many points did the student receive? ";
	//std::cin >> recievedPoints;

	std::cout << std::endl; // <--- Used for testing. Reverse comments or remove when finished.

	for (size_t grade = 0; grade < MAX_ARRAY_SIZE; grade++)
	{
		gradePoints[grade] = possiblePoints * percent;

		percent -= 0.1;
	}

	// <--- Used for testing. Comment or remove when finished.
	std::cout << "\n The elements of the array are:\n";

	for (size_t index = 0; index < MAX_ARRAY_SIZE; index++)
		std::cout << "\n " << index << ". " << gradePoints[index];
	
	std::cout << std::endl;

	gradePoints[1] = 350; // <--- Used for testing. Comment or remove when finished.

// *******************************************************************************************************

	for (size_t final = 0; final < MAX_ARRAY_SIZE && !found; final++)
	{
		if (gradePoints[final] == recievedPoints)
		{
			std::cout << "\n The letter grade is: " << letterGrade[final] << std::endl;
			found = true;
		}
	}

	if (!found)
	{
		 // <--- Could use a better statement here.
		std::cout << "\n    Invalid" << std::endl;
	}

	// A fair C++ replacement for "system("pause")".
	// The next line may not be needed. If you have to press enter to see the prompt it is not needed.
	//std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');  // <--- Requires header file <limits>.
	std::cout << "\n\n Press Enter to continue: ";
	std::cin.get();

	return 0;
	
	//system("pause");
	//return 0;
}

I am not saying that you have to do all of this, but the concepts are worth remembering.

The section of code in bold will show you what is in the array "gradePoints" which should help you to understand why the if statement in the next for loop will be "false" most of the time. You are comparing two individual numbers when you should be comparing a range to "recievedPoints". Line 54 helps to demonstrate this problem.

Hope that helps,

Andy
At line 31 you are comparing for equality. What you really want to express is that any grade above 90% is an A, any grade between 80% and 90% is a B, etc. So you need a greater-than comparison instead:
if (gradePoints[final] < recievedPoints) {

You may wonder why I'm only checking one side of the range. The loop ensures that you don't have to check other side. For example, suppose receivedPoints is 85/100. The first time through the loop you check if 90 < receivedPoints and that is false. The next time through you check if 80 < receivedPoints. At this point, you already know that receivedPoints <= 90 because if it were greater than 90, you would have exited the loop on the first iteration.
Wow, I really appreciate the details that everyone has provided to me on this topic. Thank you so much for helping with the things I overlooked and teaching me new concepts. It helps a lot to have a set of fresh eyes on something like this.
Topic archived. No new replies allowed.