0

I am trying to use a function to calculate a grade average and store the letter grade into "letter". However, whenever I try to call the function, I get an error saying "no matching for call to "findAvg". I do not completely understand references and pointers. Is that the issue here? Any help and information is appreciated, thank you.

#include <iostream>
#include <fstream>

using namespace std;

class Student{
 public:
  double grades[4];
  float avgGrade;
  string letter;
  string name;
};

void findAvg(Student f[]);

int main(){
  Student students[3];
  int i = 0;
  fstream fin;
  fin.open("input1.txt");
  while(!fin.eof()){
    fin >> students[i].name;
    for (int j =0; j < 4;++j){
      fin >> students[i].grades[j];
    }
    i += 1;
  }
  fin. close();
  findAvg(students[3]);
  cout << students[1].letter;
}

void findAvg(Student f[]){
  for(int i = 0;i<3;++i){
    f[i].avgGrade = ((f[i].grades[0] + f[i].grades[1] + f[i].grades[2] + f[i].grades[3]) /4);
    if (f[i].avgGrade>=90){
      f[i].letter = "A";
    } else if (89>f[i].avgGrade && f[i].avgGrade<=80){
      f[i].letter = "B";
    } else if (79>f[i].avgGrade && f[i].avgGrade<=70){
      f[i].letter = "C";
    } else if (69>f[i].avgGrade && f[i].avgGrade<=60){
      f[i].letter = "D";
    } else {
      f[i].letter = "F";
    }
  }
}
5
  • frankly, the problem is that you do not understand c-arrays. Thats no shame, they are horrible. Use std::vector for dynamic arrays and std::array for fixed size. What do you think this line is doing findAvg(students[3]); ? Commented Sep 15, 2020 at 8:11
  • I thought I was calling the function to find the average of grades and assign each student a letter grade. Is that not whats happening? Commented Sep 15, 2020 at 8:15
  • @ZachSal You are not calling the function with the correct syntax. Specifically your understanding of arrays in expressions is wrong. Commented Sep 15, 2020 at 8:18
  • As an aside, if your student has an avgGrade of 89.5, then your code will assign that as an F. Commented Sep 15, 2020 at 8:21
  • 1
    Don't edit your question to say "SOLVED". Instead accept one of the answers. If none of them is good enough, write your own answer and accept that. Commented Sep 15, 2020 at 8:26

4 Answers 4

2
findAvg(students[3]);

should be

findAvg(students);

The idea that you reference the whole array, by using array[SIZE] (where SIZE is the size of the array) is a common newbie error. I guess it comes from a confusion between the array declaration and an expression. But declarations and expressions are not the same thing and different rules apply. In an expression array[n] always references an element of the array. And furthermore if n is the same as the size of the array then you are referencing an element that does not exist.

The strange thing is that you handle the arrays perfectly correctly in every other part of your code, students[i].grades[j]; for instance. But for some reason when you are calling the findAvg function you think different rules apply.

Sign up to request clarification or add additional context in comments.

Comments

1

Try findAvg (students) instead of findAvg (students[3]). students[3] would give you the 4th student, which does not exist.

Comments

1

Your call findAvg(students[3]); is incorrect. You've defined a function that takes an array, but students[3] is a single Student object, not an array. It is also an error because it is trying to access an element outside the boundaries of the array

Try calling it as

findAvg(students);

Comments

1

Here you need to paas the object array So use this

findAvg(students);

instead of using

findAvg(students[3]);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.