WHY IS MY PROGRAM SEGFAULTING

I’m writing a program in my free time (nerd much?) using C++ and the Qt libraries. Eventually, it’ll display user-created flashcards, and be able to judge whether the user is answering the questions correctly. But I haven’t gotten as far as implementing the GUI with Qt yet, because I CAN’T GET THE MOST RUDIMENTARY CODE TO STOP SEGFAULTING. gdb (a debugger) tells me that the problem is in a function called mountFile.

First, some background: the flashcards are accessed via a class called Stack, which is a stack of flashcards. It has, among other things, an array of fifty Cards, as follows:

 Card _cards[50];

which the constructor initializes to blank cards. Each card has an array of twenty Answers:

 Answer _answers[20];

which are also initially blank. Class Stack is a friend of class Card and of class Answer, and class Card is a friend of class Answer.

The mountFile function reads in information from a text file containing the name of the stack, the number of cards in the stack, and the contents of each card in the stack. It is prototyped in Stack.h as:

 bool mountFile(const char*);

The boolean it returns is a status value that will eventually represent whether or not the file was mounted successfully, although it hasn’t been implemented yet. Now, here’s mountFile as written in Stack.cpp (wall 'o code coming your way):

[code]bool Stack :: mountFile(const char* filename)
{
//Variables
char line[100]; //Stores a whole line of the input file
int i, j, k, //Loop control variables
groupNum; //The current ambiguity group number
bool secondLine = false, //Boolean determining whether a given line is the second line of a card
inGroup = false, //Boolean determining whether or not to write to an amgiguity group
success = true; //Boolean returning true if the file was successfully mounted; false otherwise

//Initialize line[]
for(i = 0; i < 100; ++i) {
	line[i] = '\0';
}

i = -1;	//Initialize i for later use
j = 0;	//Initialize j
groupNum = 0;	//Initialize

//Use line to copy the filename, then declare and open the input file
strcpy(line, filename);
ifstream infile(line, ifstream :: in);

//Clean line
for(i = 0; i < 100; ++i) {
	line[i] = '\0';
}

//Read the first line of the input file. It contains the name of the stack
infile.getline(_name, 50);

//Read the second line of the input file. It contains the number of cards
infile.getline(line, 100);
_numCards = atoi(line);	//Convert the number read to an int
line[0] = '\0';			//Clean the line array
line[1] = '\0';

//Begin reading in the rest of the file
while (infile.eof() != true) {
	//Read in the next line of data
	infile.getline(line, 100);

	//Determine what to do with this line
	//If the line reads "#NEWCARD", this signifies the start of a new card
	//i is incremented for access to this card's place in _cards
	if (strcmp(line, "#NEWCARD") == 0) {	
		++i;
		j = 0;		//Reset j (the number of answers on each card)

/DEBUG/ _cards[i]._cardNumber = i + 1;
//_cards[i].setCardNumber(i + 1); //Number the new card
secondLine = true; //The next line will be the second line of the card
//The second line contains the question on the card
} else if (secondLine == true) {
_cards[i].setQuestion(line); //Write the question on the card
secondLine = false; //The next line will not be the second line
//The remaining lines of the card contain questions, some grouped into ambiguity groups
} else {
//The “@” symbol signifies the start of an ambiguity group
if (line[0] == ‘@’) {
inGroup = true;
++groupNum; //Increment the group number
}
//The following members of an ambiguity group are preceded by “~”
//When “~” is absent, the group should no longer be written to
//If the next line begins with “@”, this signifies the start of a consecutive ambiguity group, and inGroup should not be set to false
if (line[0] != ‘~’ && line[0] != ‘@’) {
inGroup = false; //Stop writing to an ambiguity group
}
//Start writing to the card’s _answers array
//Write the answer as a member of an ambiguity group
if (inGroup == true) {
_cards[i].getAnswer(j).setIsAmbiguous(true);
_cards[i].getAnswer(j).setAmbGroup(groupNum);
_cards[i].getAnswer(j).setText(line); //Place the text from the file in the answer array
//Write the answer as not being a member of an ambiguity group
} else {
_cards[i].getAnswer(j).setText(line); //Place the text from the file in the answer array
}

		_cards[i].setNumAnswers(_cards[i].getNumAnswers() + 1);	//Increment the number of answers on this card by 1
		++j;	//Increment j for access to the next answer space
	}

	//Clean the line array
	for(k = 0; k < 100; ++k) {
		line[k] = '\0';
	}

	
}

return success;

}[/code]

All of the relevant libraries for string functions and filestream functions have been included. I’m compiling the code with g++ on a machine running Fedora, and it isn’t generating any compiler errors or warnings. And I can’t for the life of me figure out what’s wrong with this function.

urdoinitrong bscly

Also

Put it inside [code ][ /code] tags

you clearly aren’t a geek of all trades.

You might want to check if the file is open or not after the open method is called.

Founded in 2004, Leakfree.org became one of the first online communities dedicated to Valve’s Source engine development. It is more famously known for the formation of Black Mesa: Source under the 'Leakfree Modification Team' handle in September 2004.