Decodying from int to string message

Hello, I am making a program that asks you to choose a file to encrypt a message to and then encrypts that message by changing each letter to the ascii and then adding one. I am trying to write the code to decrypt that message into the file to the original string message but the output is close but not quite there. Here is my code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
string decrypt(string fileToDecode)
{
	string message;
	int encodedChar;
	ifstream encryptedCode;
	encryptedCode.open(fileToDecode.c_str());
	if(!encryptedCode.good()) throw "IO Error!";
	
	while(encryptedCode >> encodedChar)
	{		
		char decodedMessage = (char)(encodedChar - 1);	
		cout << decodedMessage << endl;
		message += decodedMessage;
		cout << message << endl;
	}

	encryptedCode.close();
}


Please advise thank you.
Last edited on
Hello mikaylaelena,

PLEASE ALWAYS USE CODE TAGS (the <> formatting button) when posting code.
It makes it easier to read your code and also easier to respond to your post.
http://www.cplusplus.com/articles/jEywvCM9/
http://www.cplusplus.com/articles/z13hAqkS/
Hint: You can edit your post, highlight your code and press the <> formatting button.
You can use the preview button at the bottom to see how it looks.
It will take me a little while to work up something to test this function.

I am wondering what the "encript" function looks like because the "decript" function should be mostly the opposite of the "encript" function.

I am wondering why you made "encodedChar" an int when a char would work.

I will put something together and see what I can figure out.

Hope that helps,

Andy
Hello Andy,

Below is my code for the encryption.


int encrypt(string file, string message)
{
	ofstream textToEncode;
	textToEncode.open(file.c_str());
	if(!textToEncode.good()) throw "IO Error!";
	
	for(int i = 0; i < message.length(); i++)
	{	
		int charToInt = message[i];
		int finalAnswer = (int)charToInt + 1; 
		textToEncode << finalAnswer << endl;
	}
	textToEncode.close();
	
}


I made encodedChar and int because it is encoded in the text file as the int value for each char.

Thanks for the help!
Hello mikaylaelena,

From C++11 on you do not need to use ".cstr()" in encryptedCode.open(fileToDecode.c_str());. a "std::string" will work just fine.

The line if (!encryptedCode.good()) throw "IO Error!";, but you never do anything with it and it is never seen or delt with. It just gives you an unhandled exception at the while loop start because it can not read the file. I do not use "throw" very often, so I am not sure if there is a header file to work with the "throw", but I have used a try/catch for something like this.

What you might want to use is:
1
2
3
4
5
6
if (!encryptedCode)
{
	std::cout << "\n Error message. Your choice." << std::endl;
	std::this_thread::sleep_for(std::chrono::seconds(3));  // Requires header files "chrono" and "thread"
	exit(1);  // <--- if the file is not open, there is no point to continue.
}


char encodedChar;. Changing this from "int" to "char" made a big difference.

The code ran until I started changing things like moving std::cout << message << std::endl; outside the while loop rather than print it everytime. and I commented out the cout statement inside the while loop to just let it build "message" and print it once.

Then I started having a problem with closing the input file. Not a problem for testing the code, but annoying.

Hope that helps,

Andy
Hello mikaylaelena,

The "encrypt" function is not what I was thinking of.

The variable "charToInt" should be a "char" not an int. Although "char" is a type of "int" it is not as interchangeable as you might think.

If this works for you I would ask how old your compiler is? Mine did not give me any errors to stop the compile, but it did not work when I ran the program.

Again the use of "throw" may do something for you, but it did nothing when I tested the "decrypt" function.

Hope that helps,

Andy
Thank you so much, yes that did help!
Topic archived. No new replies allowed.