Segmentation Fault

My program compiles fine, but when it runs it will give me a segmentation fault.
I have tried to get it to collect the strings for the message1 vector, but I can't seem to get it to work. I think that's where the problem is. Any ideas? Also I put in bold where I think the error may be in.





#include <string>
#include <vector>
#include <iostream>
using namespace std;
class Code
{
public:

Code(); // Default constructor - loads and uses morse code

string decode(vector< string> message); // decodes a message

private:

vector<string> codewords; // this is a codeword vector parallel to A-Z
vector<char> alpha; // this is the vector for A-Z

vector<char> alphacode(); // function builds the vector alpha - A B C etc.

vector<string> morsecode(); // function builds the vector codewords containing morse code

char decode(string c); //returns the character for the codeword c.

};

Code::Code()
{
alpha = alphacode();
codewords = morsecode();
}

string Code::decode(vector<string> message)
{
string temp;
for (int i = 0; i < message.size(); i++)
{
temp += decode(message[i]);
}
}

char Code::decode (string c)
{
for (int i = 0; i < codewords.size(); i++)
{
if (c == codewords[i])
{
return alpha[i];
}
}
}

vector<string> Code::morsecode()
{ // This function returns a vector containing the morse code
vector<string> temp(28);
temp[0] =".-";
temp[1] ="-...";
temp[2] ="-.-.";
temp[3] ="-..";
temp[4] =".";
temp[5] ="..-.";
temp[6] ="--.";
temp[7] ="....";
temp[8] ="..";
temp[9] =".---";
temp[10] ="-.-";
temp[11] =".-..";
temp[12] ="--";
temp[13] ="-.";
temp[14] ="---";
temp[15] =".--.";
temp[16] ="--.--";
temp[17] =".-.";
temp[18] ="...";
temp[19] ="-";
temp[20] ="..-";
temp[21] ="...-";
temp[22] =".--";
temp[23] ="-..-";
temp[24] ="-.--";
temp[25] ="--..";
temp[26] =".......";
temp[27] ="x";
return temp;
}

vector<char> Code::alphacode()
{// This returns a vector containing the alphabet a-z and " "
vector<char> temp;
for (char c='A'; c<='Z'; c++)
temp.push_back(c);
temp.push_back(' ');
temp.push_back('.');
return temp;
}

int main()
{
vector<string> message1(26);
while (cin.good())
{
for(int i=0; i < 26; i++)
{
cin >> message1(i);
}
}
Code morse;
cout << morse.decode(message1);
}
Last edited on
You never declare the variable 'i' in main().
You also declare the variable 'message1' in main() but then refer to it as 'message', meaning that you have yet another undeclared variable being referenced in main().
Sorry entered wrong version: however, it still gives me the segmentation fault error
Is that you new code? Please edit it with code tags - the <> button on the right.

IF it is your new code, the same advice from Moeljbcp still applies.

Edit :

1
2
3
4
5
6
7
8
9
10
int main() {
vector<string> message1(26);
while (cin.good()) {
    int i;
    for(i=0; i < 26; i++)
       { cin >> message1(i);}
}
Code morse;
cout << morse.decode(message1);
}
Last edited on
1
2
3
4
5
6
7
8
string Code::decode(vector<string> message)
{
	string temp;
	for (int i = 0; i < message.size(); i++)
	{
		temp += decode(message[i]);
	}
}


This function is supposed to return a string and doesn't.

1
2
3
4
5
6
7
8
9
10
11
char Code::decode (string c)
{
	for (int i = 0; i < codewords.size(); i++)
	{
		if (c == codewords[i])
		{
			return alpha[i];
		}
	}
	return 0; // A failure value of some kind.
}


This function needs to return something even if it can't find it. Return a default value outside the loop if you have to, or a failure, just return *something*. You can't not return anything.

1
2
3
4
5
6
7
8
9
10
11
12
13
int main()
{
	vector<string> message1(26);
	while (cin.good())
	{
		for(int i=0; i < 26; i++)
		{
			cin >> message1[i]; //Was message1(i) before.
		}
	}
	Code morse;
	cout << morse.decode(message1);
}


To access the elements of a vector you should either access it as an array, with square brackets, or you could use the function "at", e.g. "message1.at(i);"

I would very, very, very highly recommend you learn to read compiler errors, and interpret them accordingly. All of this information I gleaned by just trying to compile your code in VC++ 2010 and reading the errors it gave me. This is an absolutely crucial skill for any coder, and I would highly recommend you try to cultivate it.

Also, re: the above: Declaring a variable inside the for loop as he did is technically valid, so that was not the problem in this case. Either way works though.
Last edited on
Thanks for your help I will go.back.and.continue to fix my code.
It finally worked thanks though I took out the while loop.
Topic archived. No new replies allowed.