Grading Program Using Functions

I do not think I am understanding the instructions given to me in the first place. The instructions look convoluted and messy to me. My teacher makes no sense and allows no questions.

-Write a program that prompts a user for their grades, finds the average and prints out a letter grade.
-The program calls a function called GetGrades that will read in grades from the keyboard, the number of grades should also be input by the user.
-GetGrades will find the sum of those grades and pass the sum and number of grades to another function called FindAverage.
-FindAverage will find the average of the grades and return this average to GetGrades.
-GetGrades will return this average to main.
-The main function will then determine the letter grade of that average based on a 10-point scale and print out this grade.

-Main should determain the letter grade of an average based on a 10-point scale and print out this grade.

90–100 A
80–89 B
70–79 C
60–69 D
0–59 F

-GetGrades function should get number of grades, read the grades entered, find the sum of those grades and pass the sum and number to FindAverage.
-FindAverage will get the average of the grades and return the average to GetGrades.
-GetGrades will return this average to main.


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

using namespace std;

// Function Prototypes.
int GetGrades(int,int,int);
int FindAverage(int,int);

int main()// Determine letter grade of the average.
{
    int numberOfGrades, letterGrade;
    int totalPoints, average;

    GetGrades(numberOfGrades,numericGrade,average); // Call GetGrades

    while (totalPoints >= 0)
	{
	    if (totalPoints >= 90 && totalPoints ==100) // 90 and above
            letterGrade = 'A';
        else if (totalPoints >= 80 && totalPoints== 100) //80-89
            letterGrade = 'B';
        else if (totalPoints >= 70 && totalPoints== 79) //70-79
            letterGrade = 'C';
        else if (totalPoints >= 60 && totalPoints==69) //60-69
            letterGrade = 'D';
        else if (totalPoints >= 0 && totalPoints== 59) //0-59
            letterGrade = 'F';
            cout << "Total Score: " << totalPoints << endl;
            cout << "Grade: " << letterGrade << endl;
    }
    return 0;
}

int GetGrades(int numberOfGrades,int numericGrade, int average)
{
    // Get number of grades.
    cout << "How many grades would you like to enter? " << endl;
    cin >> numberOfGrades;

    // Read the grades entered by user.
    for (int i = 0;i > numberOfGrades; i++)
    {
        cout << "Please enter a numeric grade: "<< endl;
        cin >> numericGrade;
    }
    // Call Function.
    return FindAverage(numericGrade, numberOfGrades);
    cout << "The average of the numeric grades you entered is: " << average << endl;

}
int FindAverage(int numericGrade,int numberOfGrades)
{
    // Display the average.
    return (numericGrade)/numberOfGrades;
}
Last edited on
A few things I noticed...

You don't need to pass parameters to GetGrades - right now you're passing uninitialized variables with garbage values to the function.

Why are you using a while loop to print out the grades? It's only going to be done once I think? If totalPoints never gets decremented, this will create an infinite loop.

Check your conditions - do you mean that to be <=100?
if (totalPoints >= 90 && totalPoints ==100)

Don't think you're going to see this print out if you put it after the Return.
cout << "The average of the numeric grades you entered is: " << average << endl;


The user input keeps overwriting the previous grade they entered. I think you want to keep a running total of the grades? Is that what you meant to store in totalPoints?

1
2
3
4
5
6
// Read the grades entered by user.
    for (int i = 0;i > numberOfGrades; i++)
    {
        cout << "Please enter a numeric grade: "<< endl;
        cin >> numericGrade;
    }
Last edited on
Reworked the code slightly.

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

// Function Prototypes.
int GetGrades();
int FindAverage(int,int);

int main()
{
	int averageGrade = 0;
	
	averageGrade = GetGrades(); // Call GetGrades

	if (averageGrade >= 90)
		cout << "Average grade is: " << averageGrade << "\tLetter grade is A." << endl;
	else if (averageGrade >= 80)
		cout << "Average grade is: " << averageGrade << "\tLetter grade is B." << endl;
	else if (averageGrade >= 70)
		cout << "Average grade is: " << averageGrade << "\tLetter grade is C." << endl;
	else if (averageGrade >= 60)
		cout << "Average grade is: " << averageGrade << "\tLetter grade is D." << endl;
	else 
		cout << "Average grade is: " << averageGrade << "\tLetter grade is F." << endl;

	return 0;
}

int GetGrades()
{
	int numberOfGrades = 1;
	int grade = 0;
	int totalGrades = 0;
	int average = 0;

	// Get number of grades.
	cout << "How many grades would you like to enter? " << endl;
	cin >> numberOfGrades;

	// Read the grades entered by user.
	for (int i = 1;i <= numberOfGrades; i++)
	{
		cout << "Please enter a numeric grade: "<< endl;
		cin >> grade;
		totalGrades += grade;
	}

	// Call Function.
	average = FindAverage(totalGrades, numberOfGrades);
	
	return average;

}

int FindAverage(int total,int number)
{
	return (total/number);
}
Last edited on
Thank you for your help, I was so close. I did not realize you could list variables inside a function, I thought they had to be a parameter. I wonder if it would be easier to just write the program first, then separate it into functions. I think that is why i got lost, I was trying to separate them into functions first. I hope one day I will be good at programming as you seem to be.
Topic archived. No new replies allowed.