Vector Problem

I am given a file named intercepted.txt that has the following inside it on one line:
:mmZ\dxZmx]Zpgy

I am supposed to read in this encrypted line of code from a .txt file and then by using ASCII values the scrambled encrypted word will be a message.

The code I have so far, which is correct is:

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

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 ()
{
	int ekey = 0;
	vector<char> encrypted = read_file("intercepted.txt");
	string decoder = 0;
	// 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;
	char x;

	while(input_file >> x)
	{
		encrypted.push_back(x);
		cout << x;
	}

	input_file.close();
	return encrypted;
}

string decode(vector<char> encrypted, int key)
{
	for(int i = 0; i < encrypted.size(); i++)
	{
		
	}

	return 0;
}

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


I know the string decode function is incorrect, which is the main part of the program. I have no idea how to start this function or what I must call in the main function to make it work correctly. The functions arguments are correct, they were given to me.

Please help me start this function or give me an idea as to what I need to do to decode this!!

THANKS =]
But there must be some algo to decrypt the string??
It may be useful to consider the encryption file given to me, sorry I didn't list this before.

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

using namespace std;

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

//Function calls to other functions to encrypt message
int main ()
{
	string message;
	int key;
	get_input(message, key);
	vector<char> encrypted = encrypt(message, key);
	display(encrypted);
	save_file(encrypted);
	return 0;
}

//Asks the user for input to encrypt the user-entered input
void get_input(string& message, int& key)
{
	//User enters the message they want to be encrypted
	cout << "Please enter the message to be encrypted on a single line: " << endl;
	//String function that includes the whitespaces on a single line
	getline(cin, message);
	//User enters a number used to encrypt their message
	cout << "Please enter a key between 1 and 100 for use in encrypting your message: ";
	cin >> key;
}

//This function encrypts the message and returns the encrypted value
vector<char> encrypt(string message, int key)
{
	//Creates new vector
	vector<char> encrypted;
	for (int i = 0; i < message.length(); i++){

		//Using ASCII, pushes the character back or farther back depending on the characters
		//in the message, and the number entered.
		if (int(message[i]) + key > 126){
			encrypted.push_back(char(32 + ((int(message[i]) + key) - 127)));
		}else{
			encrypted.push_back(char(int(message[i]) + key));
		}
	}
	cout << endl;
	//Returns the encrypted value above
	return encrypted;
}

//Displays the encrypted value from function vector<char>encrypt
void display(vector<char> encrypted){
	cout << "After encrypting your message reads:" << endl;

	//Displays the encrypted message one character at a time until it reaches null or
	//i becomes > than encrypted.size
	for (int i = 0; i < encrypted.size(); i++){
		cout << encrypted[i];
	}
	cout << endl;
}

//Outputs the encryption to a .txt file
void save_file(vector<char> encrypted){
	ofstream output_file;
	output_file.open("encrypted.txt");

	//Displays the encrypted message one character at a time until it reaches null or
	//i becomes > than encrypted.size
	for (int i = 0; i < encrypted.size(); i++){
		output_file << encrypted[i];
	}
	output_file << endl;
	output_file.close();
}
it could be something like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
string decrypt(string message, int key)
{
	//Creates new vector
	string decrypted = "";
	for (int i = 0; i < message.length(); i++){
		
		if (int(message[i]) - key > 126)
		{
			decrypted += char(((int(message[i]) - key) + 127) - 32);
		}
		else
		{
			decrypted += char(int(message[i]) - key);
		}
	}
	cout << endl;
	
	return decrypted;
}


check the output, i haven't checked it. and its returning a string and not a vector. so change it accordingly. i hope it will work fine.
I have to have it be a vector and not a string =/
Well, then you can just store the decrypted char into a temp char and push_back() it into the vector.
I am very new to C++ and really don't understand, could you explain in great detail or possibly show me and comment the code? Thanks
its quite easy.. you are not ready to try it out..

its a string of char, take each char and push it in vector of type vector<char>.
try a couple of lines which will give you some confidence.
Topic archived. No new replies allowed.