Not sure What I did wrong

I'm trying to code a program for class that basically functions as a search program for a dictionary file. While debugging I managed to get rid of the entire list of errors and warnings that Unix was giving me, and it's apparently compiling, but when I go to run it, I get a Bus Error (which I have never encountered before, have never even heard of before now, and searching through the internet hasn't helped me understand what's going on any).


There's three files for the code. A class definition file, a class implementation file, and the main program. If anyone could even just point me in the direction of how to make this work, I would be grateful.

~~~pgm1-dictionary.h~~~

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
#include <iostream>
#include <string>

#ifndef DICTIONARY_H
#define DICTIONARY_H

using namespace std;

class Dictionary
{
private:

	struct Word
	{
		string word;
		string meaning;
	};
	Word words[30];


public:

	int length;

	Dictionary();
	void addWord(string, string);
	string getWord(int);
	string getMeaning(int);
	int searchSta(string);
	int searchMid(string);
	int searchEnd(string);
};

#endif


~~~pgm1-dictionary.cpp~~~


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
#include "pgm1-dictionary.h"
#include <iostream>
#include <string>

using namespace std;

//default constructor
Dictionary::Dictionary()
{
	length = 0;
}

//Word mutator
void Dictionary::addWord(string wor, string meanin)
{
	words[length].word = wor;
	words[length].meaning = meanin;
	length = length + 1;
}


//name accessor
string Dictionary::getWord(int index)
{
	string theWord;
	theWord = words[index].word;
	return theWord;
}

//Meaning accessor
string Dictionary::getMeaning(int index)
{
	string theMeaning;
	theMeaning = words[index].meaning;
	return theMeaning;
}

//search Beginning of word
int Dictionary::searchSta(string pattern)
{
	int located;
	int size;
	
	size = pattern.length();
	
	for (int k = 0; k < length; k++)
	{
		located = words[k].word.compare(0, size, pattern);
		
		if (located != 0)
		{
			return k;
		}
	}
	
	cout << "Pattern not found" << endl << endl;
	return located;
}

//search Content of word
int Dictionary::searchMid(string pattern)
{
	int located;
	for (int k = 0; k < length; k++ )
	{
		located = words[k].word.find(pattern);
		
			
		if (located != -1)
		{
			return k;
		}
	}
	
	
	cout << "Pattern not found" << endl << endl;
	return located;
}
		
//search End of word
int Dictionary::searchEnd(string pattern)
{
	int located;
	int size;
	int ending;
	
	size = pattern.length();
	
	
	for (int k = 0; k < length; k++ )
	{
		ending = words[k].word.length();
		located = words[k].word.find(pattern, (ending - size));
		
			
	if (located != -1)
		{
			return k;
		}
	}
	
	
	cout << "Pattern not found" << endl << endl;

	return located;
}


	



~~~pgm1.cpp~~~

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
#include <iostream>
#include <string>
#include <fstream>
#include "pgm1-dictionary.h"

using namespace std;

void ReadFromFile(Dictionary);
void PrintDictionary(Dictionary);

int main()
{

// Create Dictionary object
Dictionary wordList;

// Loop Control and search
string userControl = "blank";
string search;

//Function Call to open file and read data from file
ReadFromFile(wordList);

//Function Call to print a formatted list of words
PrintDictionary(wordList);

//Loop asking for user to choose to search
	while (userControl != "q")
	{
		cout << "Choose function: Search [s]tart pattern, search [e]nd pattern, search [c]ontaining pattern, or [q]uit" << endl;
		cin >> userControl;
		cout << "Enter pattern to search for: ";
		cin >> search;
		
		
		if ( userControl == "s" )
			wordList.searchSta(search);
		else if ( userControl == "c" )
			wordList.searchMid(search);
		else if ( userControl == "e" )
			wordList.searchEnd(search);
		else if ( userControl == "q" )
			cout << "Program now shutting down" << endl;
		else
			cout << "Please only enter [s], [e], [c], or, [q]. Loop resetting." << endl;
	}


return 0;
}

// Function Reads in Data from file and places it in a array.
void ReadFromFile(Dictionary wordList)
{
	// File Data holders
	ifstream inFile;
	string curWord;
	string curDef;
	
	// file open
	inFile.open("lib/pgm1.txt");
	//check file open
	if (!inFile)
	{
		cout << "Error opening file." << endl;
	}
	else
	{
		while(inFile)
		{
			inFile >> curWord;
			inFile >> curDef;
			
			wordList.addWord(curWord, curDef);
		}
	}

}

// Function that prints the word list
void PrintDictionary(Dictionary wordList)
{
	int listLength = wordList.length;
	
	for (int k = 0; k < listLength; k++ )
	{
		cout << k+1 << ". " << wordList.getWord(k) << endl << wordList.getMeaning(k) << endl << endl;
	}
}


wordList is passed by value to ReadFromFile so changes to wordList in ReadFromFile will not affect wordList in main. Pass the Dictionary by reference instead.

When you are adding words in the loop you will add one word too many. The stream will not be in an error state until a read operation has failed which will be checked after the word has been added to the list. Something like this should work better:
1
2
3
4
while(inFile >> curWord >> curDef)
{
	wordList.addWord(curWord, curDef);
}

Last edited on
Thanks kindof. I implemented both of those changes, but now I'm getting a segmentation fault, and I still can't seem to find the problem...

(Edit)
Wait, I think I might know what I did wrong, though I don't know how to do it. There's an array of struct objects in the class, and admittedly, I have no idea how to access that.
Last edited on
Topic archived. No new replies allowed.