Issue Calling a Member Function

Basically, I have a class defined as such:
1
2
3
4
5
6
7
8
9
10
11
class StudentType {
  public:
    StudentType();
    void getData(std::ifstream & fin);
    void computeAverage();
    void printData();
  private:
    string name;
    int grades[NUM_GRADES];
    float average;
};


The function member function giving me trouble is getData. I have it defined like this:
1
2
3
4
5
6
void StudentType::getData(std::ifstream & fin) {
  fin >> name;
  for (int i = 0; i < NUM_GRADES; ++i) {
    fin >> grades[i];
  }
}


In main, I am trying to call the getData function, and I keep receiving a compiler error about "segmentation fault 11".
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
int main()
{
  string filename;
  ifstream fin;
  StudentType students[NUM_STUDENTS];
  
  cout << "Enter filename: ";
  cin >> filename;
  fin.open(filename.c_str());
  
  for (int i = 0; i < NUM_STUDENTS; ++i) {
    students[i].getData(fin);
    students[i].computeAverage();
  }
  
  for (int i = 0; i < NUM_STUDENTS; ++i) {
    students[i].printData();
  }
  
  return 0;
}


I think I am addressing something wrong syntactically whenever I call the function.
Segfault is a runtime error. It is usually caused by pointers that don't point to anything. I can't say what is the problem here, though. You need to find at which point your program breaks. The way to do that sort of depends on the tools you have. The most simple one is to add cerr << "something\n"; at several places in your code and see which of them don't get executed. If you can't see the console when segfault happens, do the same with a file.
I exited Terminal and started a new session. That seemed to fix the problem. I still appreciate your advice and help though!
Topic archived. No new replies allowed.