caeser cipher problem

Hello everyone!
Well I"m supposed to make a version of the ceaser cipher and I thought I got it to work, but when I run the program it goes all the way to the end of cout << "Result:\n"; then terminates even though I have it to output the array.
I can't figure out what's wrong. Thanks for any help, much 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
#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;

void caesar_encrypt(char src[], int key, char dst[])
{
    for(int counter = 0;counter < strlen(src);counter++)
    {
        if (src[counter] >= 65 && src[counter] <= 90)
        {
            src[counter] = + key;
            if (src[counter] + key > 90)
                src[counter] = - 32;
            else if (src[counter] + key < 65)
                src[counter] = + 32;
        }
        else if (src[counter] >= 97 && src[counter] <= 122)
        {
            src[counter] = + key;
            if (src[counter] + key > 122)
                src[counter] = - 32;
            else if (src[counter] + key < 97)
                src[counter] = + 32;
        }
    }

    cout << "Result:\n";
    for(int count = 0;count < strlen(src);count++)
        cout << src[count];
}
void caeser_decrypt(char src[], int key, char dst[])
{
    for (int counter = 0;counter < strlen(src);counter++)
    {
        if (src[counter] >= 65 && src[counter] <= 90)
        {
            src[counter = + key];
            if (src[counter] + key > 90)
                src[counter] = 32;
            else if (src[counter] + key < 65)
                src[counter] = + 32;
        }
        else if (src[counter] > 97 && src[counter] <= 122)
        {
            src[counter] = + key;
            if (src[counter] + key > 122)
                src[counter] = - 32;
            else if (src[counter] + key < 97)
                src[counter] = + 32;
        }
    }

    cout << "Result:\n";
    for(int count = 0;count < strlen(src);count++)
        cout << src[count];
}

int main()
{
    char s1[7];
    int key;
    char text[25];
    char to_be_ciphered[25];

    cout << "Enter operation: encrypt or decrypt\n";
    cin >> s1;
    cout << "Enter key\n";
    cin >> key;
    cout << "Enter text to encrypt/decrypt\n";
    cin >> text;

    strcpy(text, to_be_ciphered);

    if (s1 == "encrypt")
        caesar_encrypt(text, key, to_be_ciphered);
    else
        caesar_encrypt(text, key, to_be_ciphered);
}
Last edited on
it should through compile time error

src[counter = + key]
Alright so should I write src[counter = + key] in every line that has src[counter]? Sorry I'm not fully getting it.
Wouldn't that just change the subscript of src by the number that's in key?
Hi!

You can't compare directly two c-style string if (s1 == "encrypt"), because in this way you compare the address of arrays. You should use the strcmp

http://www.cplusplus.com/reference/clibrary/cstring/strcmp/

or c++ style strings

http://www.cplusplus.com/reference/string/string/

In the last case you can use the equality operator "==".

Additionaly, you should fix it:
Use src[counter] =+ key; instead of src[counter] = + key;

Moreover you have to check the source code. You made lots of mistakes. In encrypt section if you add a number to an element of the src array, you should substruct from element of src array in decrypt section.
I.e.:

1
2
3
4
5
6
7
8
9
10
void caesar_encrypt(char src[], int key, char dst[])
{
    for(int counter = 0;counter < strlen(src);counter++)
    {
        if (src[counter] >= 65 && src[counter] <= 90)
        {
            src[counter] =+ key;
            if (src[counter] + key > 90)
                src[counter] -= 32;
 and so on


1
2
3
4
5
6
7
8
9
void caeser_decrypt(char src[], int key, char dst[])
{
    for (int counter = 0;counter < strlen(src);counter++)
    {
        if (src[counter] >= 65 && src[counter] <= 90)
        {
            src[counter =- key];
            if (src[counter] - key > 90)
                src[counter] += 32;

and so on
Last edited on
Alright so by what you showed me I tried doing the rest of the encrypt and decrypt function along with the string issue. I still get the same thing though with the whole compile time error. Sorry I get confused I'm more of a visual learner.
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
void caesar_encrypt(char src[], int key, char dst[])
{
    for(int counter = 0;counter < strlen(src);counter++)
    {
        if (src[counter] >= 65 && src[counter] <= 90)
        {
            src[counter] =+ key;
            if (src[counter] + key > 90)
                src[counter] =- 32;
            else if (src[counter] < 65)
                src[counter] =+ 32;
        }
        else if (src[counter] >= 97 && src[counter] <= 122)
        {
            src[counter] =+ key;
            if (src[counter] > 122)
                src[counter] = - 32;
            else if (src[counter] < 97)
                src[counter] =+ 32;
        }
    }

    cout << "Result:\n";
    for(int count = 0;count < strlen(src);count++)
        cout << src[count];
}

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
void caeser_decrypt(char src[], int key, char dst[])
{
    for (int counter = 0;counter < strlen(src);counter++)
    {
        if (src[counter] >= 65 && src[counter] <= 90)
        {
            src[counter =- key];
            if (src[counter] - key > 90)
                src[counter] += 32;
            else if (src[counter] + key < 65)
                src[counter] = + 32;
        }
        else if (src[counter] > 97 && src[counter] <= 122)
        {
            src[counter] = + key;
            if (src[counter] + key > 122)
                src[counter] = - 32;
            else if (src[counter] + key < 97)
                src[counter] = + 32;
        }
    }

    cout << "Result:\n";
    for(int count = 0;count < strlen(src);count++)
        cout << src[count];
}

Then for the string problem I put this:
 
 string s1;

1
2
cout << "Enter operation: encrypt or decrypt\n";
cin >> s1;

1
2
if (s1 == "encrypt")
        caesar_encrypt(text, key, to_be_ciphered);

Is that what you meant or was I way off?
Your string usage is OK.
But you should fix the more problems:

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
void caesar_encrypt(char src[], int key, char dst[])
{
    for(int counter = 0;counter < strlen(src);counter++)
    {
        if (src[counter] >= 65 && src[counter] <= 90)
        {
            src[counter] =+ key; // HERE +=
            if (src[counter] + key > 90)
                src[counter] =- 32; // HERE  -=
            else if (src[counter] < 65)
                src[counter] =+ 32; // HERE +=
        }
        else if (src[counter] >= 97 && src[counter] <= 122)
        {
            src[counter] =+ key; // HERE +=
            if (src[counter] > 122)
                src[counter] = - 32;  // HERE -=
            else if (src[counter] < 97)
                src[counter] =+ 32; //HERE +=
        }
    }

    cout << "Result:\n";
    for(int count = 0;count < strlen(src);count++)
        cout << src[count];
}


And of course in the decrypt section you should do it.

src[counter] = - 32 means the -32 value is put to src[counter]. You should substract 32 from src[counter]. It means the followings
src[counter] = src[counter] - 32; in other words src[counter] =- 32;

The other problem when use the src[counter =- key];. Here you change the index of array to negative! number instead of substract a number from an element of src array. src[counter] -=key;
Alright well I changed that up the only problem left is the whole program termination thing when it outputs result and doesn't output the array.
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
void caesar_encrypt(char src[], int key, char dst[])
{
    for(int counter = 0;counter < strlen(src);counter++)
    {
        if (src[counter] >= 65 && src[counter] <= 90)
        {
            src[counter] += key; //changed
            if (src[counter] + key > 90)
                src[counter] -= 32; //changed
            else if (src[counter] < 65)
                src[counter] += 32; //changed
        }
        else if (src[counter] >= 97 && src[counter] <= 122)
        {
            src[counter] += key; //changed
            if (src[counter] > 122)
                src[counter] -= 32; //changed
            else if (src[counter] < 97)
                src[counter] += 32; //changed
        }
    }

    cout << "Result:\n";
    for(int count = 0;count < strlen(src);count++)
        cout << src[count];
}
void caeser_decrypt(char src[], int key, char dst[])
{
    for (int counter = 0;counter < strlen(src);counter++)
    {
        if (src[counter] >= 65 && src[counter] <= 90)
        {
            src[counter] -= key; //changed 
            if (src[counter] - key > 90)
                src[counter] += 32; //changed
            else if (src[counter] + key < 65)
                src[counter] += 32; //changed
        }
        else if (src[counter] >= 97 && src[counter] <= 122)
        {
            src[counter] += key; //changed
            if (src[counter] + key > 122)
                src[counter] -= 32; //changed
            else if (src[counter] + key < 97)
                src[counter] += 32; //changed
        }
     }

    cout << "Result:\n";
    for(int count = 0;count < strlen(src);count++)
        cout << src[count];
}
Any help anyone? I'm lost and don't have an idea to do at all.
Sorry, I was busy!

I have made simplest program from yours. It doesn't test if the encrypted text will be in alphabet range! In other words it is possible that one of character will be a sign after encrypting. I think you should understand it before creating more complex program.

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
#include <stdio.h>
#include <iostream>
#include <string.h>
using namespace std;



void caesar_encrypt(char src[], int key)
{
    for(int counter = 0;counter < strlen(src);counter++)
    {
		src[counter] += key;         
    }

    cout << "Result:\n";
    for(int count = 0;count < strlen(src);count++)
        cout << src[count];
}


void caeser_decrypt(char src[], int key)
{
    for (int counter = 0;counter < strlen(src);counter++)
    {
		src[counter] -= key; 
        
     }

    cout << "Result:\n";
    for(int count = 0;count < strlen(src);count++)
        cout << src[count];
}


int main()
{
    string s1;
    int key;
    string text;
    
    cout << "Enter operation: encrypt or decrypt\n";
    cin >> s1;
    cout << "Enter key\n";
    cin >> key;
    cout << "Enter text to encrypt/decrypt\n";	
    cin >> text;
	
	char text_str[256];
	text_str[255] = '\0';
	strncpy(text_str, text.c_str(), 255);	
    if (s1 == "encrypt")
        caesar_encrypt(text_str, key);
    else
        caeser_decrypt(text_str, key);
}


The result (in command line):


crypt.exe
Enter operation: encrypt or decrypt
encrypt
Enter key
1
Enter text to encrypt/decrypt
a
Result:
b

crypt.exe
Enter operation: encrypt or decrypt
decrypt
Enter key
1
Enter text to encrypt/decrypt
b
Result:
a
Topic archived. No new replies allowed.