Need help on decrypting/not sure how to start.

Assignment:

Write a C++ program to decrypt a 12 character message located in a file named “encrypted.txt” then print the decrypted message to the screen and a new file named “decrypted”.
You will be decrypting not encrypting. Also, the file “decrypted” does not have a file extension.
Our shift can include spaces and punctuation. We will also not worry about what happens when we are at the end of the alphabet. Our “Z” will not be encrypted as a “B”. We will instead blindly encrypt the “Z” as 4 characters beyond the alphabet (the ASCII Table shows either “^” or “~” depending on the case).

Any help would be appreciated,
Thanks.
Have a read through this. It should be easy afterwards:
http://en.wikipedia.org/wiki/Caesar_cipher

(:
Am I doing this right?


This is the input file and I already created the output 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
#include <iostream>
#include <fstream>

using namespace std;

int main()
{
    ifstream inFile;

    inFile.open( "encrypted.txt" );
    cout << "Please enter Encrypted Character:" <<endl;

    inFile >> ch;
    cout << R << endl;

    inFile >> ch;
    cout << E << endl;

    inFile >> ch;
    cout << T << endl;

    inFile >> ch;
    cout << U << endl;

    inFile >> ch;
    cout << R << endl;

    inFile >> ch;
    cout << N << endl;

    inFile >> ch;
    cout << T << endl;

    inFile >> ch;
    cout << O << endl;

    inFile >> ch;
    cout << R << endl;

    inFile >> ch;
    cout << O << endl;

    inFile >> ch;
    cout << M << endl;

    inFile >> ch;
    cout << E << endl;

    inFile.close();
    cout << "\nDone. \n";
    return 0;
}
What if the file to be read is not there?
You need to be able to catch this error:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
ifstream inFile;

inFile.open( "encrypted.txt" );

if( inFile.isopen() )
{
	//Code...
	
	inFile.close();
}
else
{
	cout << "Unable to open file...\n";
}


Also. Why don't you just read the file in to a string?

Asuming the encrypted data will only consume one line of the text file:
1
2
3
4
string input = "";

// read from the file. Save to input. until a newline character is found.
getline( inFile, input, '\n' );

i.e.
From the following, only the first line will be saved to input:
Ford, 210bhp
TVR, 320bhp
...And so on.


If you need to get more than one line, just use getline( ... ) in a loop, saving
data from input each time.
Topic archived. No new replies allowed.