| barnsta (5) | |
|
I am making a Caesar cipher using c++, however when i try to compile the code my porgram breaks and claims that i have bad pointers when i am debugging This is the code: char* Cipher::makeCipher(int shiftInput) { char alphabet[] = ("abcdefghijklmnopqrstuvwxyz"); char* cipherAlphabet = NULL; int cipherCount = 0; int alphaCount = 0; if (shiftInput > 26) { cout << "Can only shift a maximum of 25 places" << endl; &Cipher::makeCipher; } else { cout << "Plain-text Alphabet: " << alphabet << endl; cipherAlphabet = alphabet + shiftInput; for (int l = 0; l < 26; l++) { cipherAlphabet[l] = alphabet[l] + shiftInput; } for (int m = 26 - shiftInput; m < 26; m++) { cipherAlphabet[m] = cipherAlphabet[m] - 26; } cipherAlphabet[26] = '\0'; alphabet[26] = '\0'; cout << "Cipher-text Alphabet: " << cipherAlphabet << endl; } return cipherAlphabet; } | |
|
|
|
| Moschops (5961) | |
cipherAlphabet[26] = '\0';That makes no sense. Where does cipherAlphabet point when you do this?cipherAlphabet[l] = alphabet[l] + shiftInput; This will end up writing off the end of the array named alphabet, and if you're lucky you'll get a segFault.
| |
|
Last edited on
|
|