rayjk
rayjk

Reputation: 1

How to fix "expected a declaration"?

I finished my code for class, but I keep getting an error on the brackets under my functions.I did it in class, and this wasn't a problem. Do you guys know what I am doing wrong? I tried looking, but I can't really find the specific solution to my exact problem.

#include <iostream>

using namespace std;

int getGrades(int[], int);

void calcStats(int[], int, int&, int&, double&);
int main()
{
    const int size = 20;
    int grades[size];

    int count = getGrades(grades, size);

    int highGrade = 0;
    int lowGrade = 100;
    double average = 0;

    calcStats(grades, count, highGrade, lowGrade, average);

    cout << "Lowest Grade: " << lowGrade << endl;
    cout << "Highest Grade: " << highGrade << endl;
    cout << "Average Grade: " << average << endl;

    system("pause");
    return 0;
}

int getGrades(int Grades[], int size); 
{ // error "expected a declaration"
    int count = 0;
    int grade;
    cout << "Please enter up to 20 grades followed by a -1 when done." << endl;
    cin >> grade;
    while (grade != -1)
    {
        grades[count] = grade;
        count++;

        if (count == 20) {
            break;
        }
        cin >> grade;
    }
    return count;
}


void calcStats(int grades[], int size,int& highGrade, int& lowGrade, double& average);
{ // error "expected a declaration"
    int total = 0;
    highGrade;
    lowGrade;

    for (int i = 0; i < size; i++) {
        total += grades[i];
        if (grades[i] > highGrades) {
            highGrades = grades[i];
        }
        if (grades[i] < lowGrades) {
            lowGrades = grades[i];
        }
    }
    average = static_cast<double>(total) / size;
}

Upvotes: 0

Views: 15355

Answers (1)

Tas
Tas

Reputation: 7111

Your forward declarations are correct in having semi colons after them:

int getGrades(int[], int);
void calcStats(int[], int, int&, int&, double&);

As pointed out in the comments above, the definitions themselves should not have semi colons, only braces:

int getGrades(int Grades[], int size) // no semi colon
{ // error "expected a declaration"
}

A decent rule of thumb is if you use braces, you don't need to use a semi-colon.

Your getGrades function also appears to have a typo with Grades instead of grades, but I assume that was a transcript error from your IDE to the question here, not related to your issue.

Upvotes: 2

Related Questions