Caesar Cipher

Heya, need a tad help or input with my Caesar cipher program, what seems to be the issue is that when I input xzy, or if characters exceed the final alphabet character 'z' it creates strange symbols instead, such as [/].

Anyone kind enough to give me a hint? :)

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

int main()
{
    string input;
    int length;
    int shift;
    int decenc;
    cin >> decenc;
    cin >> shift;
    cin.ignore();
    getline(cin, input);

    length = (int)input.length();

    for (int count = 0; count < length; count++)
    {
        if (isalpha(input[count]))
        {
            if(decenc == 1)
            {
                for (int i = 0; i < shift; i++)
                {
                    input[count] = toupper(input[count]);

                    if (input[count] == 'z')
                    {
                        input[count] = 'a';
                    }

                    else
                    {
                        input[count]++;
                    }
                }
            }

            if(decenc == 2)
            {
                for (int i = 0; i < shift; i++)
                {
                    input[count] = tolower(input[count]);

                    if (input[count] == 'a')
                    {
                        input[count] = 'z';
                    }

                    else
                    {
                        input[count]--;
                    }
                }
            }
        }
    }
    cout << input;
}
Last edited on
So hmm, maybe I should include how my program works first.

first input decides if you want to encrypt or decrypt a message (1 for decrypt and 2 for encrypt)

and second input is how far you want to push back the letters.

third input would be the message you want to encrypt/decrypt.

So, with that, I seem to hit a snag because there are certain "symbols"(e.g [ / ] )  there that are not supposed to be included in the program.

For instance,
if I input:

1
25
Attack at dawn

It produces: ZMMZ/D ZM [ZPG

but more desired output would be without the symbols.
Last edited on
26
27
                    input[count] = toupper(input[count]);
                    if (input[count] == 'z') //¿how this could ever happen? 
Last edited on
it seems to be working in some cases, if it exceeds 'z', it will continue to go through the start of the alphabet, i.e 'a'.

or so is the initial thought.

Ok, so you were right, it doesn't do jack, how do I fix this?

I've been trying to include something like another while loop but it doesn't seem to be functioning correctly.

Any ideas, anyone?

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 <string>
using namespace std;

int main()
{
    string input;
    int length = 0;
    int shift = 0;
    int decenc = 0;

    cin >> decenc;
    cin >> shift;
    cin.ignore();

    getline(cin, input);

    length = (int)input.length();

    //while(getline(cin, input))
    {
        for (int count = 0; count < length; count++)
        {
            if (isalpha(input[count]))
            {
                if(decenc == 1)
                {
                    for (int i = 0; i < shift; i++)
                    {
                        input[count] = toupper(input[count]);
                        {
                            input[count]++;
                        }
                    }
                }

                if(decenc == 2)
                {
                    for (int i = 0; i < shift; i++)
                    {
                        input[count] = tolower(input[count]);
                        {
                            input[count]--;
                        }
                    }
                }
            }
        }

        cout << input;
    }
}


my current code.
Last edited on
Use the standard library?

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
#include <iostream>
#include <string>
#include <algorithm>
#include <cctype>

// return the character unchanged if the character is not in the aplphabet
char encrypt( char c, const std::string& alphabet, const std::string& cipher )
{
    auto pos = alphabet.find(c) ;
    return pos != std::string::npos ? cipher[pos] : c ;
}

int main()
{
    const std::string alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ" ;

    constexpr std::size_t shift_by = 5 ; // instead, read from stdin if required

    std::string cipher = alphabet ;
    // shifts right; use cipher.begin(), cipher.begin()+shift_by for left shift
    std::rotate( cipher.rbegin(), cipher.rbegin()+shift_by, cipher.rend() ) ;

    std::string phrase ;
    while( std::getline( std::cin, phrase ) )
    {
        for( char& c : phrase )
            std::cout << encrypt( std::toupper(c), alphabet, cipher ) ;
        std::cout << '\n' ;
    }
}
auto is not working with my current compiler for some reason, produces some juicy errors (codeblocks)

I'm thinking, maybe stick with my current, but if all else fails, I will check out that one, thanks JL
> auto is not working with my current compiler for some reason,

You need to enable C++11 support:

In CodeBlocks,
Menu => Settings => Compiler... Compiler settings =>Compiler Flags

check -std=c++11

While you are at it, also check -Wall, -Wextra and -pedantic-errors
Solved!

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

int main()
{
    string input;
    int length = 0;
    int shift = 0;
    int decenc = 0;

    cin >> decenc;
    cin >> shift;
    cin.ignore();

    getline(cin, input, '\0');

    length = (int)input.length();


    {
        for (int count = 0; count < length; count++)
        {
            if (isalpha(input[count]))
            {
                if(decenc == 1)
                {
                    for (int i = 0; i < shift; i++)
                    {
                        input[count] = toupper(input[count]);
                        if(input[count] == 'Z')
                            (input[count] = 'A');
                        else
                        {
                            input[count]++;
                        }

                    }
                }

                if(decenc == 2)
                {
                    for (int i = 0; i < shift; i++)
                    {
                        input[count] = tolower(input[count]);
                        if(input[count] == 'a')
                        (input[count] = 'z');
                        else
                        {
                            input[count]--;
                        }
                    }
                }
            }
        }
        cout << input;
    }
}
Topic archived. No new replies allowed.