I've created this program to decrypt sentences with a key that shifts the alphabet. The problem with it is that when i have any non-Alphabetic characters, the punctuation or numbers will turns it into a Alphabetic character for some reason. I cannot find the problem with the code. I know the error occurs when it's checking for the uppercase letters because uppercase letters are always displayed. If you guys could shed any light onto this that would be great. I also commented out the quick fix I made...but it does not allow some characters as well.
Code:#include <iostream> #include <fstream> //read from file using namespace std; int main() { int offset; const int ENCRYPTED = 250; char message[ENCRYPTED]; char AlphabetLower[]="abcdefghijklmnopqrstuvwxyz"; char AlphabetHigher[]="ABCDEFGHIJKLMNOPQRSTUVWXYZ"; char AlphabetLowerKey[26]; char AlphabetHigherKey[26]; // char OtherChars[]="1234567890- ;:,.?!$&*()=+-"; int length; int k = 0; int l = 0; int m = 0; int n = 0; int loop = 1; int num = 0; int arraynum = 0; cout << "Please enter the encrypted message: "; cin.getline (message, ENCRYPTED, '\n'); cin.ignore(100, '\n'); cout << "Please enter the decryption key: "; cin >> offset; int a = offset; //Have to be initialized after offset is inputted int b = offset; length = strlen(message); for (a; a < 26; a++) { AlphabetLowerKey[k]=AlphabetLower[a]; k++; } for (b; b < 26; b++) { AlphabetHigherKey[l]=AlphabetHigher[b]; l++; } for (m; m < offset; m++) { AlphabetLowerKey[k]=AlphabetLower[m]; k++; } for (n; n < offset; n++) { AlphabetHigherKey[l]=AlphabetHigher[n]; l++; } while (arraynum < length) { while (loop == 1) { if (message[arraynum] == AlphabetLower[num]) { message[arraynum] = AlphabetLowerKey[num]; loop = 0; } else if (message[arraynum] == AlphabetHigher[num]) { message[arraynum] = AlphabetHigherKey[num]; loop = 0; } else // if (message[arraynum] == OtherChars[num]) // { // loop = 0; // } // else num++; } num = 0; loop = 1; arraynum++; } cout << message << endl; return 0; }



LinkBack URL
About LinkBacks


