Hi all, need some suggestions. This code looks like a jumbled mess to me. I am missing something and am not sure what.

-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.

#include <iostream>

    using namespace std;

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

    int main()// Determine letter grade of the average.
    {
        int score;
        int totalPoints;
        int average;
        int num;
        int grade;

        GetGrades(score,grade);

        while (score >= 0)
        {
            totalPoints += score;   // Accumulate the running total.
            if (totalPoints >= 90) // 90 and above
                grade = 'A';
            else if (totalPoints >= 80) //80-89
                grade = 'B';
            else if (totalPoints >= 70) //70-79
                grade = 'C';
            else if (totalPoints >= 60) //60-69
                grade = 'D';
            else if (totalPoints >= 0)
                grade = 'F';

        cout << "Total Score: " << totalPoints << endl;
        total = FindAverage(average,num);
        cout << "Grade: " << grade << endl;
        }

        return 0;
    }

    void GetGrades(int num,int grade)
    {
        cout << "How many grades would you like to enter? ";
        cin >> num;

        while (num >> 0)
        {
            cout << "Please enter a numeric grade: ";
            cin >> grade;
            // Call Function.
            total = FindAverage(average, num)
            // Display Answer.
            cout << "Your grade average is: ";
        }

    }
    void FindAverage(int average,int totalPoints,int num)
    {
        // Display the average.
        average = (totalPoints)/num;
       cout << "The average of the numeric grades you entered is: " << average << endl;
    }

Recommended Answers

All 7 Replies

Let's start with the function getGrades.

-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.

The code you have for that function does this:
Gets the number of grades.
Uses the right shift operator in the while condition, which makes no sense.
Fetches a single grade.
Tries to find the average of that single grade, which makes no sense.

Just do what the requirement says:

GetGrades()
{

  // GET NUMBER OF GRADES
  int numberOfGrades;
  cout << "Enter number of grades" << endl;
  cin >> numberOfGrades;

  // read the grades entered
  vector<int> grades; // to store the input numbers
  grades.resize(numberOfGrades);
  for (int i=0; i<numberOfGrades; i++)
  {
    cout << "Enter grade " << i << endl;
    cin >> grades[i];
  }

  // And so on
  ...

Just code what the requirements say, one line at a time.

It says numberOfGrades, grade, and average are uninitialized in the main function.
My new code:

#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, grade;
    int totalPoints;
    int average;

    GetGrades(numberOfGrades,grade,average); // Call GetGrades
    average = FindAverage(grade,numberOfGrades); // Call FindAverage

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


    return 0;
}

int GetGrades(int numberOfGrades,int grade, int average)
{
    cout << "How many grades would you like to enter? ";
    cin >> numberOfGrades;

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

}
int FindAverage(int grade,int numberOfGrades)
{
    // Display the average.
    return (grade)/numberOfGrades;
}

Why are you creating the variable numberOfGrades in main? Why does your main function contain some kind of running total? If the requirements you stated earlier are what you have to do, just do that. You're only confusing yourself with extra junk that wasn't asked for.

I haven't gotten to vectors yet.

vector<int> grades; // to store the input numbers
grades.resize(numberOfGrades);

Have you got as far as new and arrays?

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.
90–100 A
80–89 B
70–79 C
60–69 D
0–59 F

Thank you for your help. Functions are tricky beasts and I got variables confused.

#include <iostream>

using namespace std;

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

int main()// Determine letter grade of the average.
{
    int averageGrade = 0; // Accumulator

    averageGrade = GetGrades(); // Call GetGrades

    if (averageGrade >= 90 && averageGrade <=100) // 90 and above
        cout << "Your Average Grade is: " << averageGrade << "\tYour Letter Grade is A." << endl;
    else if (averageGrade >= 80 && averageGrade <= 89) //80-89
        cout << "Your Average Grade is: " << averageGrade << "\tYour Letter Grade is B." << endl;
    else if (averageGrade >= 70 && averageGrade <= 79) //70-79
        cout << "Your Average Grade is: " << averageGrade << "\tYour Letter Grade is C." << endl;
    else if (averageGrade >= 60 && averageGrade<= 69) //60-69
        cout << "Your Average Grade is: " << averageGrade << "\tYour Letter Grade is D." << endl;
    else if (averageGrade >= 0 && averageGrade <= 59) //0-59
        cout << "Your Average Grade is: " << averageGrade << "\tYour Letter Grade is F." << endl;
    else
        cout << "Invalid Entry." << endl;

    return 0;
}
int GetGrades()
{
    int numberOfGrades;
    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)
{
    // Display the average.
    return (total)/number;
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.