In the CS50 2019 Caesar assignment, I am supposed to perform Caesar shifts on characters by a given number of letters (key).
To do this, I add the value of key to each letter, as such:
for each character in plaintext:
plaintext[character] += key
Since I want z to loop back to a, I then wrote:
while (ciphered_character > 122)
{
ciphered_character -= 26;
}
Running the program with plaintext of z and a key of 26 causes 26 to be added to z (which is represented by 122). This causes plaintext[character] to overflow (past 127, I presume) and become negative before the while loop even kicks in. This gives a garbage output.
I know I can check for potential overflows beforehand and subtract 26 first, but that complicates my code. Can I give the variable 'more room' to prevent it from overflowing?
Do I use a different data type? What is the best practice here?