Calculating the average

Can anyone help steer me in the right direction?? My assignment is to create a simple program with 3 functions. The main function prompts for student name and getting 3 tests scores. The second function is to calculate the average of the test scores, and the third function is to display the result in a,b,c etc...grades. I'm still new at C++, so can anyone tell me if my code looks ok up to calculating the average? How would I add in the third function for the grading scale?? Thank you in advance!

Here's my code:

#include <iostream>
#include <string>
using namespace std;

string studentName;
float calcAverage(int, int, int);
double getTestScore();

int main()

{
cout << "Enter the student name: ";
getline(cin, studentName);

//declare variables
int test1 = 0;
int test2 = 0;
int test3 = 0;
double average = 0.0;

//get input items
cout << "" << endl;
cout << "Enter test scores in the range 1-100: " << endl;
cout << "" << endl;
cout << "First test score: ";
cin >> test1;
while (test1 < 1 || test1 > 100)
{
cout << "ERROR. Enter a value in the range 1-100: ";
cin >> test1;
}
cout << "Second test score: ";
cin >> test2;
while (test2 < 1 || test2 > 100)
{
cout << "ERROR. Enter a value in the range 1-100: ";
cin >> test2;
}
cout << "Third test score: ";
cin >> test3;
cout << "" << endl;
while (test3 < 1 || test3 > 100)
{
cout << "ERROR. Enter a value in the range 1-100: ";
cin >> test3;
}

//call function to calculate payments
average = calcAverage(test1, test2, test3);
cout << "Average: " << average << endl;

return 0;

}
//end of main function

//function definitions
float calcAverage(int num1, int num2, int num3)

{
double avg = 0.0;
avg = double(num1 + num2 + num3) / 3.0;
return avg;
} //end of calcAverage function

char displayGrade(double avg)
{
if (avg >= 90 && avg <= 100)
return 'A';
else if (avg >= 80 && avg <= 89)
return 'B';
if (avg >= 70 && avg <= 79)
return 'C';
if (avg >= 60 && avg <= 69)
return 'D';
if (avg >= 50 && avg <= 59)
return 'F';
}
Hello tdknapp,

PLEASE ALWAYS USE CODE TAGS (the <> formatting button), to the right of this box, when posting code.

It makes it easier to read your code and also easier to respond to your post.

http://www.cplusplus.com/articles/jEywvCM9/
http://www.cplusplus.com/articles/z13hAqkS/

Hint: You can edit your post, highlight your code and press the <> formatting button.
You can use the preview button at the bottom to see how it looks.

I found the second link to be the most help.

You should compile your program before you post it here and try to fix any errors and list any errors you do not understand.

It is best not to use global variables. Define them in main and pass them to the functions that need them.

In "main" you have three while loops doing the same thing. this is a good candidate for a function.

The proto type and function definition for "average" says it returns a "float", but the function is returning a "double". This may work, but you will loose some precision going from a "double" to a "float".

In the "calcAverage" function you do not have to type case the "int"s. The fact that "avg" is a double and you are dividing by "3.0" the compiler will promote the "int"s to "double"s before the calculation.

The "displayGrade" function looks a bit suspicious. At first glance it looks like you are comparing a "double" to an "int". In this case the "int" may not be promoted to a "double before the comparison. This may work, but it is something I would like to test.

Over all the "displayGrade" function looks good, but you need to call it drom "main" to print the return value.

Hope that helps,

Andy
This code works, but it is pretty redundant and can be broken by the user if they enter a string for the test scores or something; this is okay since you are just learning however. I would have an if statement that does something like this this:

1
2
3
4
5
if(cin.fail())
{
     cin.clear();
     cin.ignore();
}


as for a grading scale, all you did to calculate what letter grade they got was use if greater than or equal to 90, return A. So you could replace 90 with a variable then have the user input those variables at the beginning. you would need 5 variables then you could check if the avg is greater than those and less than the one above it. Say b is equal to 82 and a is equal to 92, you could do:

1
2
3
4
5
6
int a = 92;
int b = 82;

if(avg >= a) return 'A';
else if(avg >= b) return 'B';
...


I noticed you were using cout twice since you wanted two line spaces when they were entering the test scores, you can just do << endl << endl. and it will give you another space without having to use two couts.

Another thing, in the calcAverage function, you had the return type as a float but made avg a double and casted the answer to a double, then returned a double. You should probably decide which one you want based on how many decimal places you need, there's no point in making all of the math a double and then returning a float. It won't round up or down if you do, or make the math more accurate, it just chops off decimal places and uses more memory than you need.

This was actually a really interesting post for me to read because i made a program exactly like that when I first started learning C++. The biggest problem I had was overthinking things that had simple fixes, i would write like 50 lines to fix something or redo my entire program over something really easy that could have been done in 2 lines.

A few tips:
Once your program is finished, refine it, ask yourself a few questions, "Can the user easily crash my program?" "Does the program do what i want it to do?" "how can i add more useful functionality?"
Google is your friend, when you have problems go searching for solutions, if you have a question, especially just starting out, chances are it's been asked before and has answers. Making posts about your code and asking people to review it like this is good, it will help you improve.
Make programs that challenge you, but aren't so above your head that you could never do them with your current knowledge. Try to make programs that you don't initially know how to make but have an idea, then fill in the gaps as you do it.

Once you get better at C++ you can do a lot of really interesting projects. Hope this was helpful.
Last edited on
Thank you for the responses. I'm flipping through my book and other online sources and I'm getting it slowly. I really suck at this computer programming stuff. I appreciate your help.
Hello tdknapp,

Most of what I have to say ended up in the program as comments.

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
#include <iostream>
#include <iomanip>  // <--- Added. For fixed, showpoint and setprecision.
#include <string>
using namespace std;

string studentName;  // Should be in "main".

float calcAverage(int, int, int);  // <--- should return a "double"
double getTestScore();
char displayGrade(double average);  // <--- Added this one.

int main()

{
	cout << "Enter the student name: ";
	getline(cin, studentName);

	//declare variables
	int test1 = 0;
	int test2 = 0;
	int test3 = 0;
	double average = 0.0;

	std::cout << std::fixed << std::showpoint << std::setprecision(2);  // <--- Only needs done once. Affects all output after.

	//get input items
	cout << "" << endl;
	cout << "Enter test scores in the range 1-100: " << endl;
	cout << "" << endl;
	cout << "First test score: ";
	cin >> test1;

	//// <--- Could be written as Kevin211 mentioned:
	//cout << "\nEnter test scores in the range 1-100: " << endl;  // <--- Notice the "\n".
	//cout << "\nFirst test score: ";
	//cin >> test1;

	while (test1 < 1 || test1 > 100)  // <--- Could be made into a function and not repeated two more times.
	{
		cout << "ERROR. Enter a value in the range 1-100: ";
		cin >> test1;
	}

	cout << "Second test score: ";
	cin >> test2;

	while (test2 < 1 || test2 > 100)
	{
		cout << "ERROR. Enter a value in the range 1-100: ";
		cin >> test2;
	}

	cout << "Third test score: ";
	cin >> test3;
	cout << "" << endl;

	while (test3 < 1 || test3 > 100)
	{
		cout << "ERROR. Enter a value in the range 1-100: ";
		cin >> test3;
	}

	//call function to calculate payments
	average = calcAverage(test1, test2, test3);
	//cout << "Average: " << average << endl;

	// <--- An alternative using "studentName".
	std::cout << studentName << " has an average of: " << average << " and a letter grade of: " << displayGrade(average) << std::endl;

	// 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;

}  //end of main function

//function definitions
double calcAverage(int num1, int num2, int num3)  // <--- Changed return type to "double"

{
	double avg = 0.0;
	avg = double(num1 + num2 + num3) / 3.0;
	return avg;
} //end of calcAverage function

char displayGrade(double avg)
{
        // <--- Could be written as:
        // if (avg >= 90.0 && avg <= 100.0)

	if (avg >= 90 && avg <= 100)
		return 'A';
	else if (avg >= 80 && avg <= 89)
		return 'B';
	if (avg >= 70 && avg <= 79)
		return 'C';
	if (avg >= 60 && avg <= 69)
		return 'D';
	if (avg >= 50 && avg <= 59)
		return 'F';
}  // End of "displayGrade" 

Give it a read and try and see what you think.

Hope that helps,

Andy
Topic archived. No new replies allowed.