Segmentation Fault

I am writing this program that runs in XCode but when I compile it in g++ and execute it I get a segmentation error. After an extensive search I have found the function where the error is happening but I cannot figure out what is happening.

This is my header File

//
// record.h
// Program 8
//
// Created by Alain Davila on 7/3/13.
// Copyright (c) 2013 Alain Davila. All rights reserved.
//

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
//libraries

class ReadRecord {
    
public:
    
    ReadRecord(string);
    
    void CountFile(string);
    void ReadFile (string);
    void SortNames ();
    void Summary();
    
    int CountPie();
    int CountCake();
    
    
private:
    
    struct Roster {
        string LName;
        string FName;
	char Cheesecake;
    };
    
    Roster * MyRoster;
    
    int TotalCount;
    
};

//structure definition

#endif /* defined(_RECORD__) */ 



And this is the function causing the Problem:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
void ReadRecord::ReadFile(string filename)
{
    MyRoster = new Roster [TotalCount];
    
    ifstream in;
    in.open (filename.c_str());
    for (int q = 0; q < TotalCount; q++)
    {
        in >> MyRoster[q].LName;
        in >> MyRoster[q].FName;
        in >> MyRoster[q].Cheesecake;
    }
    in.close();
}
Have you initialized TotalCount, and to what value?
That was exactly it. I thought I had tested for that so it did not occurred to me to check. Thank you !
Last edited on
Topic archived. No new replies allowed.