I need help with decrypting a 12 character message located in a file named encrypted.txt then printing the decrypted message to the screen and a new file named decrypted.txt. This is what i have so far... and don't understand how to print the char to the screen as a letter instead of the ASCII value when i subtract 3 to decrypt it. I am new to this so please try and keep it simple for me..also I'm using VS 2008 Express C++. Here is the encrypted text im using for an example...“UHWXUQWRURPH” which when decrypted is..“RETURNTOROME”, but I only get the ASCII values.

#include <iostream>

#include <fstream>

using namespace std;

int main()
{
	char ch;
	ifstream infile;
	ofstream outfile;

	infile.open("encrypted.txt");
	outfile.open("decrypted.txt");

	//test if file opens.
	if (!infile)
	{
		cout << "Error: Can't open file \n";

		return 1;
	}
	
infile >> ch;
cout << ch - 3;

infile >> ch;
cout << ch - 3;

infile >> ch;
cout << ch - 3;

infile >> ch;
cout << ch -3;

infile >> ch;
cout << ch -3;

infile >> ch;
cout << ch -3;

infile >> ch;
cout << ch - 3;

infile >> ch;
cout << ch - 3;

infile >> ch;
cout << ch - 3;

infile >> ch;
cout << ch - 3;

infile >> ch;
cout << ch - 3;

infile >> ch;
cout << ch - 3;

infile.close();
outfile.close();

return 0;

}

Recommended Answers

All 3 Replies

cout << char( ch - 3 );

Why are you copying that input/output statement pair like that? Have you not gotten to loops yet?

No i have not yet and my prof wanted us to write it out one char at a time.

I need help with decrypting a 12 character message located in a file named encrypted.txt then printing the decrypted message to the screen and a new file named decrypted.txt. This is what i have so far... and don't understand how to print the char to the screen as a letter instead of the ASCII value when i subtract 3 to decrypt it. I am new to this so please try and keep it simple for me..also I'm using VS 2008 Express C++. Here is the encrypted text im using for an example...“UHWXUQWRURPH” which when decrypted is..“RETURNTOROME”, but I only get the ASCII values.

#include <iostream>

#include <fstream>

using namespace std;

int main()
{
	char ch;
	ifstream infile;
	ofstream outfile;

	infile.open("encrypted.txt");
	outfile.open("decrypted.txt");

	//test if file opens.
	if (!infile)
	{
		cout << "Error: Can't open file \n";

		return 1;
	}
	
infile >> ch;
cout << ch - 3;

infile >> ch;
cout << ch - 3;

infile >> ch;
cout << ch - 3;

infile >> ch;
cout << ch -3;

infile >> ch;
cout << ch -3;

infile >> ch;
cout << ch -3;

infile >> ch;
cout << ch - 3;

infile >> ch;
cout << ch - 3;

infile >> ch;
cout << ch - 3;

infile >> ch;
cout << ch - 3;

infile >> ch;
cout << ch - 3;

infile >> ch;
cout << ch - 3;

infile.close();
outfile.close();

return 0;

}

I believe i figured it out... Here is my new code... thanks for the help.

#include <iostream>

#include <fstream>

using namespace std;

int main()
{
	char ch;
	ifstream infile;
	ofstream outfile;

	infile.open("encrypted.txt");
	outfile.open("decrypted.txt");

	//test if file opens.
	if (!infile)
	{
		cout << "Error: Can't open file \n";

		return 1;
	}
	
infile >> ch;
cout << char (ch - 3);

infile >> ch;
cout << char (ch - 3);

infile >> ch;
cout << char (ch - 3);

infile >> ch;
cout << char (ch - 3);

infile >> ch;
cout << char (ch - 3);

infile >> ch;
cout << char (ch - 3);

infile >> ch;
cout << char (ch - 3);

infile >> ch;
cout << char (ch - 3);

infile >> ch;
cout << char (ch - 3);

infile >> ch;
cout << char (ch - 3);

infile >> ch;
cout << char (ch - 3);

infile >> ch;
cout << char (ch - 3);

infile.close();
outfile.close();

return 0;

}

all i did was add " char ( ch - 3)"

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.