Decoding Problem

I have a problem with my decoding program that I'm trying to figure out. It's supposed to read in a encrypted line of code from a .txt file and then by using ASCII values, convert that encrypted code to a decoded version. Example:

Encrypted code:

Fcjjm~rfcpc'

Decoded code: (after adding 2 to every ASCII value - or a key of 2, within ASCII # 32 - 127)

Hello there.


Here is the outline:

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
#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 ()
{
	vector<char> encrypted = read_file("intercepted.txt");

	//Function calls
	
	return 0;
}

vector<char> read_file(string file_name)
{
	ifstream input_file;
	input_file.open(file_name.c_str());
	vector<char> encrypted;

	// Code to read file

	input_file.close();
	return encrypted;
}

string decode(vector<char> encrypted, int key)
{
	// Code to decode
}

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


If anyone can kinda walk me through this, I would be grateful for all the help! Thanks a lot!


chars are actually numbers in disguise, you can simply do the addition/subtraction on the characters themselves.

For getting the line from the file, read:
http://www.cplusplus.com/doc/tutorial/files/
ok...I do not understand what you posted firedraco. if you would, why doesn't this work in this case:

1
2
3
4
5
6
7
8
9
10
11
vector <char> read_file(string file_name)
{
	ifstream input_file;
	input_file.open(file_name.c_str());
	if (input_file.fail())
	   cout << "Fail";
	vector <char> encrypted;
    for (int i = 0; i < encrypted.size(); i++)
        cout << encrypted[i];
    return encrypted;
}

operator<< has a specific overload for char that treats the input as an ASCII character rather than a number. It was a decision that the designer/programmer of the IO streams had to make up front.
what do you recommend to fix that?
or the better question...how do I initialize vector<char> encrypted to be what the string is in intercepted.txt?!
this is what I have so far, can anyone help me fix this. right now, my program compiles but it isn't outputting what it should be reading from the .txt file. thanks for the help!!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
vector <char> read_file(string file_name) 
{
       ifstream input_file; 
       input_file.open(file_name.c_str()); 
       if (input_file.fail()) 
       {
          cout << "Failed to open file!" << endl; 
          exit(1); 
       }
       vector <char> encrypted; 
       int next(0);
       while (input_file >> encrypted[next])
       {
             cout << encrypted[next];
             next++;
             if (next == MAX_SIZE)
                break;
       }
       return encrypted; 
} 
This should fix the problem, variable 'n' reads is extracted from the input_file and yadayada...

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
vector<char> read_file(string file_name)
{
	ifstream input_file;
	input_file.open(file_name.c_str());
	vector<char> encrypted;
	char n;

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

	input_file.close();
	return encrypted;
}
i think what bradgizzie said will work.
alright, so that works, but it terms of decoding the actual code, here is what I have. why doesn't this work and what can I do to fix it:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
string decode(vector <char> encrypted, int key) 
{
       string decode; 
       int size = encrypted.size(), i = 0;
       for (i; i < size; i++)//Scans the message from message[0] to message[99] to store character
       {
           for (key = 0; key < 100; key++)
           {
               
               if (int(encrypted[i]) + key > 126)
               {
                  encrypted.push_back(char(32 + ((int(encrypted[i]) + key) - 127))); //It starts the ASCII table over when it reaches the end. 
               }
               else
               {
                   encrypted.push_back(char(int(encrypted[i]) + key)); //Turns the user input ASCII character to the new character, pushing all of the characters forward on the table 'key' ASCII values
               }
           }
       }
} 
this is the same problem of bradgizzie and i've replied to him, check his post.

are you both the same?? how is this possible that two people on the forum have the same problem!!!!???
i am very confused that who is writing which question and whom im replying what!!!
no we are not the same, he probably just took my post and put it on his for people to help out.
oh...

so have you seen what i posted to him..is it working fine for you??
yeah, i think we're in the same class, but we dont know it, haha. it's working alright i s'pose. I think I have a good enough grasp on this to finish it. Thanks for all the help!
hahahahaha... ok...
Topic archived. No new replies allowed.