The loop in the encrypt
function is completely useless, because there is no path through the loop-body without a return
statement causing the loop to be terminated and control returned to the caller of encrypt
. This makes that the program as a whole repeatedly encrypts the first character of the message with successive elements from the key.
The easiest way around this is to make the following changes
- Remove the loop from the
encrypt
function Pass, as an additional argument, the element from the message that you want to encrypt, making the signature
int encrypt(string message, int k, int i)
Some miscellaneous remarks:
- The global variable
new_crypt
is not used anywhere. You can safely remove it. (You should avoid the use of global variables as much as reasonably possible). - Instead of using the magic number
65
, you can also use the character literal'A'
. This has the advantage that you don't need a comment to explain the number 65 and that it is always the correct value for the capital A, even if you end up not using ASCII. (But see also the next bullet) - Your code assumes the letters A to Z (and a to z) have contiguous values (such that Z == A+26). This may happen to be the case for the English alphabet in the ASCII encoding, but it is not guaranteed for other language alphabets or encodings.