Encryption/Decryption program issue

closed account (oNC9Nwbp)
I think the getline on line 17 and 67 is incorrect because the program shows both the "Enter a message" and "Enter key" on the same line (when ran) and does not allow for a message to be entered, only the key. I need it to be able to do both as this is an encryption/decryption program. Any help would be appreciated.

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
#include <iostream>
#include <fstream>
using namespace std;

int main()
{
    string choice;
        cout << "Do you want to Encrypt or Decrypt? "<< endl;
        cin >> choice;
//encryption code here
if (choice== "encrypt")
{
    char text[100], ch;
    int i, key;

    cout<< "Enter a Message to Encrypt: ";
    cin.getline(text, 100);
    cout<< "Enter Key: ";
    cin>> key;

    for(i= 0; text[i]!= '\0'; i++)
    {
        ch= text[i];

        if(ch>= 'a'&& ch<= 'z')
        {
            ch= ch+ key;

            if(ch> 'z')
            {
                ch= ch- 'z'+ 'a'- 1;
            }

            text[i]= ch;

        }
        else if(ch>= 'A'&& ch<= 'Z')
        {
            ch= ch+ key;

            if(ch> 'Z')
            {
                ch= ch- 'Z'+ 'A'- 1;
            }

            text[i] = ch;
        }
    }

    cout<< "Encrypted Message: "<< text;

    ofstream fileEnc;
    fileEnc.open ("Encryption.txt");
    fileEnc<< text;
    fileEnc.close();


}
//decryption code here
else
    {

    char text[100], ch;
    int i, key;

    cout<< "Enter a Message to Decrypt: ";
    cin.getline(text, 100);
    cout<< "Enter Key: ";
    cin>> key;

    for(i= 0; text[i]!= '\0'; i++)
    {
        ch= text[i];

        if(ch>= 'a'&& ch<= 'z')
        {
            ch= ch- key;

            if(ch< 'a')
            {
                ch= ch+ 'z'- 'a'+ 1;
            }

            text[i] = ch;
        }
        else if(ch>= 'A'&& ch<= 'Z')
        {
            ch= ch- key;

            if(ch> 'a')
            {
                ch= ch+ 'Z'- 'A'+ 1;
            }

            text[i]= ch;
        }
    }

    cout<< "Decrypted Message: "<< text;

    ofstream fileDec;
    fileDec.open ("Decryption.txt");
    fileDec<< text;
    fileDec.close();

    }
    return 0;
}
Last edited on
Check line 19. key is defined as an int: is that what you enter?

If you do not enter an int, then the stream std::cin enters a failure state, which causes all further attempts to read from the stream to fail immediately.
closed account (oNC9Nwbp)
Yes the key for my encryption is an integer. This is a caesar cipher and it moves the letters X amount over.

Example(also the way it should be entered when the program is run):
Question: Do you want to encrypt or decrypt?
Answer: Encrypt
Please enter message: Hello!
Enter Key: 1
New Message: Ifmmp!

what the program does:

Do you want to encrypt or decrypt?
Answer: Encrypt
Please enter message: Enter Key: 1
New Message:


The program doesnt even give you the option to put something in for the message. I had it working originally without the if statement but when i added that it started messing up.
Is it because you're reading choice with >> ? That leaves the newline in the input stream so the first getline after that reads an empty string. Try reading choice with getline to ensure the newline is eaten.
Topic archived. No new replies allowed.