segmentation fault

I'm working on a program for my class and I've hit a wall

the header file I created
1
2
3
4
5
6
7
8
enum Hogwarts {GRYFFINDOR,SLYTHERIN,RAVENCLAW,HUFFLEPUFF};
enum Ministry {INTERNATIONAL_MAGICAL_COOPERATION,MAGICAL_LAW_ENFORCEMENT,MAGICAL_TRANSPORTATION,REGULATIONS_AND_CONTROL_OF_MAGICAL_CREATURES,MYSTERIES,
				MAGICAL_GAMES_AND_SPORTS,MAGICAL_ACCIDENTS_AND_CATASTROPHIES};
enum HWRank {STUDENT,FACULTY,STAFF};
enum MinistryRank {OFFICIAL,AUROR,UNSPEAKABLE,DEPARTMENT_CHAIR,MINISTER};
enum DeathEaterRank {INITIATE,APPRENTICE,MASTER,LORD};

enum Organization{HOGWARTS,DEATH_EATERS,MINISTRY_OF_MAGIC};


and the .cpp file
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
#include <iostream>
#include <fstream>
#include <string>
#include <iomanip>
#include "Prog1.h"

using namespace std;

ifstream inFile;

const int NUM_ROWS = 5;
const int NUM_COLS = 2;

struct Wizard
{
	string wizName;
	int bDay;
	int bMonth;
	int bYear;
	Hogwarts house;
	Ministry department;
	int determination;
	char gender;
	HWRank rankH;
	MinistryRank rankM;
	DeathEaterRank rankD;
};

Wizard wizData[NUM_ROWS][NUM_COLS];


string team1string;
string team2string;
string spellstring[NUM_ROWS][NUM_COLS];
string dstring[NUM_ROWS][NUM_COLS];


Organization orgName;

void PrintWizards();
void ReadTeamNames();
void ReadTeamMembers();

int main()
{
	
	
	string fileName;

	cout << "Enter the name of the file (something.in): ";
	cin >> fileName;
	inFile.open(fileName.c_str());

	ReadTeamNames();
	ReadTeamMembers();

	cout << team1string << " & " << team2string << endl;

	PrintWizards();



	return 0;
}


void ReadTeamNames()
{

char chIn;
inFile.get(chIn);


	
while (chIn != '#')
{
   team1string = team1string + chIn;
  inFile.get(chIn);
  }
//cout << team1string << endl;
inFile.get(chIn);
while (chIn != '#')
{
   team2string = team2string + chIn;
  inFile.get(chIn);
  }
//cout << team2string << endl;

}
void ReadTeamMembers()
{

	char chIn;
inFile.get(chIn);
int rowNum;
int colNum;

for(colNum = 0; colNum < NUM_COLS; colNum++)
	for(rowNum = 0; rowNum < NUM_ROWS; rowNum++)
	{
while (chIn != '#')
{
	wizData[rowNum][colNum].wizName = wizData[rowNum][colNum].wizName + chIn;
	inFile.get(chIn);
}


inFile.get(chIn);
while (chIn != '#')
{
dstring[rowNum][colNum] = dstring[rowNum][colNum] + chIn;
inFile.get(chIn);
}
if (dstring[rowNum][colNum] == "HOGWARTS")
	orgName = HOGWARTS;
else if (dstring[rowNum][colNum] == "MINISTRY_OF_MAGIC")
	orgName = MINISTRY_OF_MAGIC;
	else 
	orgName = DEATH_EATERS;
	

inFile.get(chIn);
while (chIn != '#')
{
	spellstring[rowNum][colNum] = spellstring[rowNum][colNum] + chIn;
	inFile.get(chIn);
}}



return;
}


void PrintWizards()
{
	int rowNum;
	int colNum;

	for(rowNum = 0; rowNum < NUM_ROWS; colNum++)
	{
	for(colNum = 0; colNum < NUM_COLS; rowNum++)
	{
		cout << wizData[rowNum][colNum].wizName << dstring[rowNum][colNum] << spellstring[rowNum][colNum] << endl;
	}}
}


oh, and the input file teamsrun1.in
1
2
3
4
5
6
7
8
9
10
11
ORDER_OF_PHOENIX#YOU_KNOW_WHO#
Albus Dumbledore#HOGWARTS#expecto patronus#
Kingsley Shacklebolt#MINISTRY_OF_MAGIC#crucio#
Harry Potter#HOGWARTS#expelliarmus#
Nymphadora Tonks#MINISTRY_OF_MAGIC#sectumsempra#
Remus Lupin#HOGWARTS#petrificus totalus#
Severus Snape#HOGWARTS#avada kedavra#
Bellatrix Lestrange#DEATH_EATERS#crucio#
Lord Voldemort#DEATH_EATERS#imperio#
Lucius Malfoy#DEATH_EATERS#avada kedavra#
Draco Malfoy#HOGWARTS#stupefy# 





when it tries to print out the fifth person in the file it says Segmentation Fault. It may be that it's saving in the position (1,1) first instead of (0,0).
A segmentation fault is when you try to access a variable outside its own limits.

So, have you tried debugging the code?
Hey i found the error.
In for loop, you must first increase row. You wrote in wrong order :)

for( rowNum = 0 ; rowNum < NUM_ROWS ; colNum++ )

for ( colNum = 0 ; colNum < NUM_COLS ; rowNum++ )
Last edited on
ah, thank you. I had been playing with the code for a while and had switched the order a couple times and forgot to check to make sure I switched all of them.
Topic archived. No new replies allowed.