jeudi 31 décembre 2015

skips "if" statement in certain inputs

So i have done a program that encrypts input in the way Julius Ceasar did (adding to all letters a constant number).

The program seems to work fine until it reaches the z + 6 barrier, at which point it start to dismiss the if statement that will make the alphabet circular.

I just can't understand why it will stop working when it reaches that point.

This is my code:

#include <iostream>
using namespace std;

char encoded(char c, int k) {
    k %= 26; //too big numbers!
    c += k; //encrypt
    if (c > 'z')
        c = c-'z'+ 'a' -1; //circular alphabet
    c = c - 'a' +'A'; //uppercase
    return c;
}

int main () {
    int k;
    while (cin >> k) {
        char c;
        while (cin >> c and c != '.') {
            if ('a' <= c and c <= 'z') 
                cout << encoded(c,k);
            else if (c == '_') 
                cout << ' ';
            else 
                cout << c;
        }
        cout << endl;
    }
}

If someone can give me a clue on what is going on I would greatly appreciate it.

Aucun commentaire:

Enregistrer un commentaire