Average Score and Letter Grade

Hi I am new to coding and I am having a difficult time with my code. My code probably is hard to read due to the lack of comments and spacing but I would really like to understand why I can't get my entire program to work.

My assignment is to make a program that outputs the average score, letter grade, and three scores that you input at first. My program only reads the readScores even though I tried to reference all voids. Please someone explain what is wrong and I apologize for the terrible code. 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
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
#include <iostream>
#include <string> 
using namespace std;

void readScores(double& s1, double& s2, double& s3);
double getAverage(double s1, double s2, double s3, double& average);
void getGrade(double average);
void print(double s1, double s2, double s3, double average, char grade);

int main()
{
	
	double s1, s2, s3, average;
	char grade;

	readScores(s1, s2, s3); // call the function please 
	grade = getAverage(s1, s2, s3, average);
	getGrade(average);
	print(s1, s2, s3, average, grade);

	system("pause");
	return 0;
}

void readScores(double& s1, double& s2, double& s3)
{
	cout << "Please input three grades: " << endl;
	cin >> s1 >> s2 >> s3; 

	while (s1 < 0)
	{
		cout << "The grade must be from 0 to 100. Please re-enter your grade: " << endl;
		cin >> s1; 
		break;
	}
	
	while (s2 < 0)
	{
		cout << "The grade must be from 0 to 100. Please re-enter your grade: " << endl;
		cin >> s2;
		break;
	}
	
	while (s3 < 0)
	{
		cout << "The grade must be from 0 to 100. Please re-enter your grade: " << endl;
		cin >> s3;
		break;
	}
}

	double getAverage(double s1, double s2, double s3, double &average)
{
		char grade; 
		grade = s1 * s2 * s3;
		average = grade / 3.0; // redo this part 
		return grade;
}

	void getGrade(double average)
{
		cout << "your average please? " << endl; 
		cin >> average; 
		
		if (90 <= average <= 100)
		{
			cout << "Leter Grade is: A " << endl;
		}
		
		else if (80 <= average < 90)
		{
			cout << "Letter grade is: B " << endl;
		}
		
		else if (70 <= average < 80) 
		{
			cout << "Letter Grade is: C " << endl;
		}
		
		else if (60 <= average < 70)
		{
			cout << "Letter Grade is: D " << endl;
		}
		
		else (0 <= average < 60)
		{
			cout << "Letter Grade is: F " << endl;
		}

		
}

void print(double s1, double s2, double s3, double average, char grade)
{
	cout << "First Grade: " << s1 << endl; 
	cout << "Second Grade: " << s2 << endl; 
	cout << "Third Grade: " << s3 << endl;
	cout << "Average Score: " << average << endl;
	cout << "Letter Grade: " << grade << endl;


Would you mind telling us the exact error?
You’re not using grade properly. Your acting as if it is a int instead of a char.
Last edited on
Hello xcxave,

The first problem I see is using namespace std;. I know that you probably paid good money for a book or a class so that someone could teach you wrong, but you can keep using it until it becomes a big problem. These two links are worth reading:
http://www.cplusplus.com/forum/beginner/258335/
http://www.lonecpluspluscoder.com/2012/09/22/i-dont-want-to-see-another-using-namespace-xxx-in-a-header-file-ever-again/

As highwayman has pointed out grade = getAverage(s1, s2, s3, average);. "getAverage" is returning a "double" that you are trying to put in a "char". This does not work. Also you are sending "average" to the function by reference, so really there is no need to return anything. If you did it should be to "average".

In "getGrade" when it chooses a letter to print it is doing so in the function. Not sure where it will actually print out, but in the function "print" you are printing "grade", but does it have a value at this point? The function should either return the letter grade back to "main" and store it in "grade" or pass "grade" by reference to the function and make it a "void" function.

In your if/else if statements "90 <= average <= 100" may look good on paper, but may not work well as C++ code. Usually it would be written as if (90 <= average && average <=100).

That is what I see without loading up the code and testing.

Hope that helps,

Andy
Handy Andy thank you so much for the help I only have one part to tweak but I was really struggling with so much in my code. Thank you for the easy explanation in a kind way.
I still am though struggling to find a way for the average to be outputted. It adds the three inputted grades but doesn't divide by 3. It also seems that grade isn't used anywhere in my program or 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
double getAverage(double s1, double s2, double s3, double &average)
{
		average = s1 + s2 + s3 ; 
		average / 3; // redo this part 
		return average;
}

	void getGrade(double average)
{
		
		
		if (90 <= average && average <=100)
		{
			cout << "Letter Grade is: A " << endl;
		}
		
		else if (80 <= average && average < 90)
		{
			cout << "Letter grade is: B " << endl;
		}
		
		else if (70 <= average && average < 80) 
		{
			cout << "Letter Grade is: C " << endl;
		}
		
		else if (60 <= average && average < 70)
		{
			cout << "Letter Grade is: D " << endl;
		}
		
		else if (0 <= average && average < 60)
		{
			cout << "Letter Grade is: F " << endl;
		}



}

void print(double s1, double s2, double s3, double average, char grade)
{
	cout << "First Grade: " << s1 << endl; 
	cout << "Second Grade: " << s2 << endl; 
	cout << "Third Grade: " << s3 << endl;
	cout << "Average Score: " << average << endl;
	cout << "Letter Grade: " << grade << endl;[code]
[/code]
Last edited on
Hello xcxave,

You are welcome.

Now after testing your program here are some of the things I found that I did not see at first.

Your header files:
1
2
3
4
5
6
#include <iostream>
#include <limits>
//#include <string> // <--- Never used in this program.

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

Actually you could replace "string" with 'limits" and save some work.

Your prototypes I changed to this:
1
2
3
4
void readScores(double& score1, double& score2, double& score3);
void getAverage(const double score1, const double score2, const double score3, double& average);
void getGrade(const double average, char& grade);
void print(const double score1, const double score2, const double score3, const double average, const char grade);

The "const" is nor required, but it does mean that the function can not change the value of the variable. You could think of it more as a safe guard for your variables.

In "main"
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
double score1{}, score2{}, score3{}, average{};
char grade{};

readScores(score1, score2, score3); // call the function please

getAverage(score1, score2, score3, average);

getGrade(average, grade);

print(score1, score2, score3, average, grade);

// The next line may not be needid. 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;

On line 8 I added the "grade" to the parameters and passed it by reference so that the variable can have a value by the time you call the "print" function.

Also "Score1" is a much better name than "s1" At least it describes better what it is fore. Evan with the "1", "2" and "3" you should avoid a single letter variable name. Should you return to this program in the future you do not have to guess or take time to figure out what the single letter variable name is. And when your programs get bigger it helps to keep everything in straight and easier to understand.

The last bit of "main" is a replacement for "system("pause");" which you should avoid using. It can leave your program vulnerable to attack.

This function I changed around a little:
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
void readScores(double& score1, double& score2, double& score3)
{
	std::cout << "Please input three grades: " << std::endl;
	std::cout << " Score 1: ";
	std::cin >> score1;

	while (score1 < 0 || score1 > 100)
	{
		std::cout << "The grade must be from 0 to 100. Please re-enter your grade: ";
		std::cin >> score1;
		//break;
	}

	std::cout << " Score 2: ";
	std::cin >> score2;

	while (score2 < 0 || score2 > 100)
	{
		std::cout << "The grade must be from 0 to 100. Please re-enter your grade: " << std::endl;
		std::cin >> score2;
		//break;
	}

	std::cout << " Score 3: ";
	std::cin >> score3;

	while (score3 < 0 || score3 > 100)
	{
		std::cout << "The grade must be from 0 to 100. Please re-enter your grade: " << std::endl;
		std::cin >> score3;
		//break;
	}
}

I broke up the input into three separate sections, at least to help you understand how it works. In the first while loop I removed the "endl" which puts the input on the same line as the prompt. Maybe its just me, but I think it works out better this way. I left the other two for you to change if you want.

For the while loops: just checking for "< 0" does have its problems. Any number greater than (-1), say 200 or 1000, would be considered a good score. Not what you want. Also the "break" statement is not needed. If I wer to enter (-1) and enter the while and then enter (-1) again it would accept that as a good score. The "||" keeps it in the range of (0) to (100). Only when a score is within this range does the while loop fail and you go to the next entry.

It is also possible to put the while loop in a function and call the function to check if the entry is valid.

In the function "getAveraage()" to find the average you add the scores not multiply them. Otherwise the function is fine. If you pass "average" by reference there is no need to return anything and what you originally wrote is wrong to begin with.

Here is the first part of this function:
1
2
3
4
5
6
7
8
9
10
11
12
void getGrade(double average, char& grade)
{
	//cout << "your average please? " << endl;
	//cin >> average;

	if (90 <= average && average <= 100)
	{
		//cout << "Letter Grade is: A " << endl;
		grade = 'A';
	}

	else if (80 <= average && average < 90)

Lines 3 and 4 I am not sure what you had in mind here, but you are asking for the average that the user would have to figure out even though it is a value passed to the function that has already been calculated.

What you have for your if conditions may look good on paper, but does not work a C++ code. What I have done here work better. Which is not to say that a different compiler may allow what you did to work, but I d not think it would make any difference.

The "cout" statements are the wrong place to output the letter grade unless you put it there for testing. Setting "grade" is what is needed here.

I adjusted the "print" function like this:
1
2
3
4
5
std::cout << "\n  First Grade: " << score1 << std::endl;
std::cout << "  Second Grade: " << score2 << std::endl;
std::cout << "  Third Grade: " << score3 << std::endl;
std::cout << "\n  Average Score: " << average << std::endl;
std::cout << "\n  Letter Grade: " << grade << std::endl;

Part of this is a personal choice of mine. Starting the output one line off the top edge of the screen and one character from the left edge makes it easier to read. The other part comes from the years I have been working with programs and the use of the "\n"s and spaces just make for a nicer output.

Other than the syntax problems the rest is just a suggestion that you can take or leave as you choose.

Hope that helps,

Andy
Hello xcxave,

To use what you have:
1
2
3
4
5
6
double getAverage(double s1, double s2, double s3, double &average)
{
		average = s1 + s2 + s3 ; 
		average / 3; // redo this part 
		return average;
}

Line 4 should be average /= 3.0;. As is "average is being divided by (3), but the result is not stored anywhere. The (/=) is the same as average = average / 3.0;.

Since you are passing "average" by reference to the function there is no need to return anything. And since your original code was returning a "double' and trying to store it in a "char" it did not work.

It also seems that grade isn't used anywhere in my program or code.
Not entirely true. "grade" is used in the program it is that you are not using it correctly. My previous post should help to explain that.

Hope that helps,

Andy
Hello Handy Andy,

You have definitely saved me and made me understand the errors in my program and why they came to be. I surely will read the links you posted on the other replies because I want this to become a second language. Thank you so much for your help in this program, I am so grateful.

From, xcxave.
Hello xcxave,

You are welcome. Any time.

Andy
Topic archived. No new replies allowed.