thomas123
thomas123

Reputation: 89

C++ wrong letter for output

Hello I have new question about Caesar cipher for example

Key: 3

Plain: ABCÇDEFGĞHIİJKLMNOÖPRSŞTUÜVYZ

Cipher: DEFGĞHIİJKLMNOÖPRSŞTUÜVYZABCD

These are Turkish letters " ç ,ı ,ğ, ö , ş , ü , Ç, İ , Ğ, Ö, Ş, Ü "

I need to make encryption and decryption and program shouldnt have case sensitive. It should be like s=S, ç=Ç

You can see my program below ,but I have some problems

1) Text(Plain) and key should entered by user but I couldnt do it.

2) char text[] = "DEF"; this input should give (for decrypt) "CÇD" but it gives "CÃD"

normally it should give "Ç" instead of "Ã"

I need help :(

# include <iostream>
# include <cstring>

const char alphabet[] ={'A', 'B', 'C', 'Ç', 'D', 'E', 'F', 'G', 'Ğ', 'H', 'I',
                        'İ', 'J', 'K', 'L', 'M', 'N', 'O', 'Ö', 'P', 'R', 'S',
                        'Ş', 'T', 'U', 'Ü', 'V', 'Y', 'Z', '0', '1', '2', '3',
                        '4', '5', '6', '7', '8', '9', '.', ',', ':', ';', ' '};
const int char_num =44;

void cipher(char word[], int count, int key)
{
    int i = 0;
    while(i < count) {
        int ind = -1;
        while(alphabet[++ind] != word[i]) ;
        ind += key;
        if(ind >= char_num)
            ind -= char_num;
        word[i] = alphabet[ind];
        ++i;
    }
}

void decipher(char word[], int count, int key)
{
    int i = 0;
        while(i < count) {
        int ind = -1;
        while(alphabet[++ind] != word[i]) ;
        ind -= key;
        if(ind < 0)
            ind += char_num;
        word[i] = alphabet[ind];
        ++i;
    }
}


int main()
{
    char text[] = "ABC";
    int len = strlen(text);
    std::cout << text << std::endl;
    cipher(text, len, 2);
    std::cout << text << std::endl;
    decipher(text, len, 2);
    std::cout << text << std::endl;
    system("pause");
    return 0;
}

Upvotes: 1

Views: 736

Answers (1)

bames53
bames53

Reputation: 88215

This issue is that your program is using a different encoding than the one the console expects. Windows is configured this way by default; programs use encodings like cp1252 or cp1254 and the console expects something else like cp437.

Here's an article from a Microsoft developer that explains why this is.

There's already a lot of information online covering the numerous ways you can fix the encoding mismatch.

Upvotes: 1

Related Questions