Why am I getting a segmentation fault?

Okay, so I have a program that takes a single line of input and encrypts it, and outputs the encrypted line to a 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
/*
 * Author: Ima Student
 * Date: April 7th, 2009
 * Description: Program to encode messages using an ASCII substitution cypher
 */

#include<iostream>
#include<fstream>
#include<string>
#include<vector>

using namespace std;

void get_input(string& message, int& key);//declarations
vector<char> encrypt(string message, int key);
void display(vector<char> encrypted);
void save_file(vector<char> encrypted);

int main ()
{
	string message;
	int key;
	get_input(message, key);
	vector<char> encrypted = encrypt(message, key);
	display(encrypted);
	save_file(encrypted);
	return 0;//Is the driver for all of the functions
}

void get_input(string& message, int& key)
{
	cout << "Please enter the message to be encrypted on a single line: " << endl;
	getline(cin, message);
	cout << "Please enter a key between 1 and 100 for use in encrypting your message: ";
	cin >> key;//standard enough, read in the input of the message, including whitespace, and stores it in the string "message" also takes the key value
}

vector<char> encrypt(string message, int key)
{
	vector<char> encrypted;
	for (int i = 0; i < message.length(); i++){
		if (int(message[i]) + key > 126){//keeps the encoding in the regular ASCII keyset, also prevents the use of the DEL char
			encrypted.push_back(char(32 + ((int(message[i]) + key) - 127)));//adds a charecter to the vector "encrypted" and wraps the count back to 'space'
		}else{
			encrypted.push_back(char(int(message[i]) + key));//adds a charecter to the vector "encrypted"
		}
	}//uses the method push_back, which add a charecter at the end of the current vector, after the current last element
	cout << endl;
	return encrypted;
}

void display(vector<char> encrypted){
	cout << "After encrypting your message reads:" << endl;
	for (int i = 0; i < encrypted.size(); i++){
		cout << encrypted[i];
	}//uses a for loop to display all the elements of the vector, the loop is kept in bounds by the use of the built in function size()
	cout << endl;
}

void save_file(vector<char> encrypted){
	ofstream output_file;
	output_file.open("encrypted.txt");
	for (int i = 0; i < encrypted.size(); i++){
		output_file << encrypted[i];
	}//almost the same as above, excepet instead of outputting to the terminal, outputs a file, supposedly for "dillivery".
	output_file << endl;
	output_file.close();
}


I then have another program that opens the file created by the encrypt program. This is the one that is giving me a segmentation fault.
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
/*
 * Author: I'm still a student
 * Date: April 8th, 2009
 * Description: Program to decode intercepted encrypted messages
 */

#include<iostream>
#include<fstream>
#include<string>
#include<vector>
#include <cstdlib>

using namespace std;

vector<char> read_file(string file_name);
string decode(vector<char> encrypted, int key);
void display(string decrypted, int key);

int main ()
{
	vector<char> encrypted = read_file("encrypted.txt");
	for(int key = 1; key <=100; key++)
	{	
		string decrypted = decode(encrypted, key);
		cout<<"Does this decryption make sense?(1 for yes, 0 for no)";
		int choice;
		cin>>choice;
		if(choice == 1)
		{
			break;
		}
	}
	// Insert appropriate function calls to decrypt the message and display the results
	
	return 0;
}

vector<char> read_file(string file_name)
{
	ifstream input_file;
	input_file.open(file_name.c_str());
	vector<char> encrypted;
	if (input_file.fail())
    	{
    	    cout << "Failed to open file." << endl;
    	    exit(1);
    	}
	char * buffer;//magic begins here
	int size = 0;
	input_file.seekg (0, ios::end);
	size = input_file.tellg();
	input_file.seekg (0, ios::beg);
	buffer = new char [size];
	input_file.read (buffer, size);//and now the magic ends.Oooo, Pointers.
	for (int i = 0; i < size; i++)
	{
		encrypted.push_back(buffer[i]);
	}
	// Insert your code here to read the file into the encrypted vector;
	input_file.close();
	return encrypted;
}

string decode(vector<char> encrypted, int key)
{
	string decrypted;
	for (int i = 0; i < encrypted.size(); i++)
	{
		if (int(encrypted[i]) - key < 32)
		{
			decrypted.push_back(char(127 + ((int(decrypted[i]) - key) - 32)));
		}
		else
		{
			decrypted.push_back(char(int(encrypted[i]) - key));
		}
	}
	display(decrypted,key);
// Insert your code here to decode message
}

void display(string decrypted, int key){
	cout << "Key: " << key << " Text: " << decrypted << endl;
}


Any help would be greatly appreciated.
Last edited on
Line 71 references decrypted but probably should be encrypted.

Your read_file function would be much simpler if you just read
into the vector directly. (First size the vector to the right size,
then read into &encrypted[0]).

You should also consider using const references since passing
vectors to functions by value is very expensive.
I fixed that, and it's still segfaulting However, when it displays the decoded text, it now appends an 'h' that wasn't there before.

The first suggestion I can do.

The second one, not so much. I've been forced to use those exact declarations. It's rather unfortunate.
How far does the program get before it crashes?

Can you print out the size of encrypted to ensure it is the correct size (should be size of file)?
Can you print out the length of decrypted to ensure it is equal to the size of encrypted?
Never mind, I fixed it, I changed up the main program.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
int main ()
{
	vector<char> encrypted2 = read_file("encrypted.txt");
	for(int key2 = 1; key2 <=100; key2++)
	{	
        int key = key2;
        vector<char> encrypted = encrypted2;
		string decrypted = decode(encrypted, key);
		cout<<"Does this decryption make sense?(1 for yes, 0 for no)";
		int choice;
		key = key2;
		cin>>choice;
		if(choice == 1)
		{
			break;
		}
	}
	// Insert appropriate function calls to decrypt the message and display the results
	
	return 0;
}
Topic archived. No new replies allowed.