Caesar Cipher Trouble Keeping letters lowercased and upper cased

Please help me correct this code.

3rd post is where the code is at
Last edited on
Can you please use the code format tags to format your code.

In this case, it would help if you posted code that will compile.
I'm sorry I didn't know I could do that. Here you go. I touched it up a little bit and got it to compile but I want the data in the files to stay lower cased and upper cased. When I encrypt they go into all caps. When I decrypt, they change to lowercase letters. Here's my new code.

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
#include <iostream> //For cout and cin etc.
#include <fstream> // To input and output files


class Caesar
{

public: void encrypt(char *inp,char *out,int key);
        void decrypt(char *inp,char *out,int key);
        void readText(char *inp);
};

void Caesar::encrypt(char *inp,char *out,int key) //Function for Encryption
{
        std::ifstream input;
        std::ofstream output;
        char buf;

        input.open(inp); //Reads in the file
        output.open(out); //Gets a new file ready for encryption

        buf = input.get();
        while(!input.eof())
        {
                if(buf >= 'a'&&buf <= 'z') //If it equals a through z
                {
                        buf -= 'a';
                        buf += key;
                        buf %= 26;
                        buf += 'A';
                }
                output.put(buf);
                buf = input.get();
        }

        input.close();
        output.close();
        readText(inp);
        readText(out);
}

void Caesar::decrypt(char *inp,char *out,int key) //Decrypt Function
{
        std::ifstream input;
        std::ofstream output;
        char buf;

        input.open(inp);
        output.open(out);

        buf = input.get();
        while(!input.eof())
        {
                if(buf >= 'A'&&buf <= 'Z')
                {
                        buf -= 'A';
                        buf += 26-key;
                        buf %= 26;
                        buf += 'a';
                }
                output.put(buf);
                buf = input.get();
        }

        input.close();

        output.close();

        readText(inp);
        readText(out);
}

void Caesar::readText(char *inp) //Reading in the text from a files
{
        std::ifstream input;
        char buf;

        input.open(inp);
  std::cout << "\n\n <--- " << inp <<" --->\n";
        buf = input.get();
        while(!input.eof())
        {
                std::cout << buf;
                buf = input.get();
        }
        input.close();
}
int main()
{
 Caesar a;
        int choice,key;
        char inp[30],out[30];
                std::cout << "\n Enter input file: "; //File wanting to be edited
                std::cin >> inp;
                std::cout << "\n Enter output file: "; //New file
                std::cin >> out;
                std::cout << "\n Enter how many places you would like to shift?: "; //How many places the letters are going to move
                std::cin >> key;
        std::cout << "\n\n 1. Encrypt\n 2. Decrypt\n\n Select choice(1 or 2): "; //The choice of encryption or decryption
        std::cin >> choice;

        if(choice == 1)
                a.encrypt(inp,out,key); //Where we call back the void function to use the data from user input
        else if(choice == 2)
                a.decrypt(inp,out,key); //Where we call back the void function to use the data from user input
        else    std::cout << "\n\n Unknown choice"; //If user picks neither 1 or 2

        std::cin.get();

}


<--- Hello.txt --->
hello
turns into
<--- encrypt.txt --->
JGNNQ

how do I keep it lowercased?

Thank you for any response. Any help is appreciated!
Last edited on
That's really complicated. Anyway, the algorithms, do they seem right to you? Do they really implement this? https://en.wikipedia.org/wiki/Caesar_cipher

encrypt:
1
2
3
4
5
6
7
                if(buf >= 'a'&&buf <= 'z') //If it equals a through z
                {
                        buf -= 'a';
                        buf += key;
                        buf %= 26;
                        buf += 'A';
                }


decrypt:
1
2
3
4
5
6
7
                if(buf >= 'A'&&buf <= 'Z')
                {
                        buf -= 'A';
                        buf += 26-key;
                        buf %= 26;
                        buf += 'a';
                }

Topic archived. No new replies allowed.