encryption

I need to read text from a file character by character process it and put it into a file. It doesn't show any errors or warnings but still doesn't work. Any ideas?
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
 
#include <cstdlib>
#include <iostream>
#include <fstream>
#include <cctype>


using namespace std;



/****************************************************************************/
// get the value of the key, that the user wants to shift the text
int getKey()
{
    int key;

    cout << "Please enter the value of the key between 1 and 95" << endl;
    cin >> key;
    // check that the value of the key must be between 1 and 95
    while (key < 1 || key > 95)
    {
        cout << "Please enter the value of the key between 1 and 95" << endl;
        cin >> key;
    } // end while
    return key;
} // end function getKey

/****************************************************************************/
//get character from the plaintext.txt, ignore if the character is less than 32
//or greater than 126, return characters between 32 and  126
char getCharacter()
{
    char c;


    ifstream infile;
    infile.open("plaintext.txt");

    if ( !infile.is_open() )
    {
        cout << "ERROR: Unable to open file!" << endl;
        system("PAUSE");
        exit(1);
    }

    while (!infile.eof())
    {
        infile >> c;
        if ( c > 32 || c < 126)
            ;
        else
        infile.clear();
        infile.open("plaintext.txt");
        infile >> c;
    }


    return c;

}// end function getCharacter

/****************************************************************************/

char caesarsCipher (char &c, int &key)
{

    ofstream outfile;
    outfile.open("ciphertext.txt");

    if ( !outfile.is_open() )
    {
        cout << "ERROR; Unable to open file!" << endl;
        system("PAUSE");
        exit(1);
    }

    int encrypted;

    encrypted = c + key;

    if ( encrypted > 126 )

        encrypted = encrypted - 95;

    return encrypted;
}



int main()
{
    int key;
    char c;

    int getKey();
    char getCharacter();
    char caesarsCipher(char &, int &);

    getKey();
    getCharacter();
    caesarsCipher(c, key);




    cout << endl << "Done!" << endl;

    system("PAUSE");
    return EXIT_SUCCESS;

}
Last edited on
So what doesn't work?

A tip: Providing a relative path like "ciphertext.txt" might lead to a situation where the program searches in the wrong directory. Use an absolute path instead
Use an absolute path instead


For debugging purposes maybe, but generally paths should be relative (except in situations where you can reasonable assume that a file will never be moved).
Hello Nino,
your code is:

1
2
3
4
5
6
7
8
9
10
while (!infile.eof())
    {
        infile >> c;
        if ( c > 32 || c < 126)
            ;
        else
        infile.clear();
        infile.open("plaintext.txt");
        infile >> c;
    }


this is bad approach IMO.

first line should be like this:

1
2
3
4
while (infile.good())
{
     ///....
}


that way you know that while loop will break for any kind of error, not just EOF

next this code:

1
2
3
4
        else
        infile.clear();
        infile.open("plaintext.txt");
        infile >> c;


not sure but it looks like a bad boy.

else statement should be enclosed with brackets I think?

1
2
3
4
5
6
        else
        {
              infile.clear();
              infile.open("plaintext.txt"); // file handle should be closed before reopening ?
              infile >> c;
         }


Not sure if that was your intention but you're overriding c variable.
so when while loop finishes your function returns what?

only last character, not all of them sorted!

according to your comment in the code the function is supposted to return a const char*

But in that case your c varaible should be defined outside the function or you should create a heap pointer to the char* and return that.

also in your main function,

int getKey();
are you declaring a function inside a main() ?
Last edited on
thanks for replying you guys,
I'm not familiar with absolute path (you know, beginners :D ),
Codekiddy,
yes I'm decaring all of the functions inside the main, because all the functions are called by main only, I tried both of your suggestions but still no luck, when while loop finishes it should return single next character with ascii value between 32 and 126 only.
Topic archived. No new replies allowed.