Hello C++beginner,
Here is a fix for your shifting problem. I only took into account lower case lettering, but I will leave it up to you to modify for upper case. Just be careful because there are characters in between the letter sets.
CODE
void encode()
{
getline(cin, decoded, '~');
cipk(); //Gets shift key from user
int dlen = decoded.length(); //Sets the lenght from the string
for (int j=0; j<decoded.length(); j++) {
// Check if character is between lowercase "a" and "z"
if (decoded.at(j) >= 'a' && decoded.at(j) <= 'z') {
// Apply the shift and check it against "z"
int shifted = decoded.at(j) + shiftk;
// Greater than "z", so subtract 26 to take it to "a"
// Then apply the shift
if (shifted > 'z') {
decoded.at(j) = ((decoded.at(j) - 26) + shiftk);
}
else {
// Shift can happen without intervention
decoded.at(j) = shifted;
}
}
}
cout << "\nYour encoded string is: \n" << decoded << endl;
}
void decode()
{
getline (cin, encoded, '~');
cipk(); //Gets shift key from user
int elen = encoded.length(); //Sets the length from the string
for (int j=0; j<encoded.length(); j++) {
// Check if character is between lowercase "a" and "z"
if (encoded.at(j) >= 'a' && encoded.at(j) <= 'z') {
// Subtract the shift and check if it is a number less than "a"
int shifted = encoded.at(j) - shiftk;
// Yes it is less than "a", so add 26 to take it to "z"
// Then apply the shift
if (shifted < 'a') {
encoded.at(j) = ((encoded.at(j) + 26) - shiftk);
}
else {
// Shift is fine, so just shift.
encoded.at(j) = shifted;
}
}
}
cout << "\nYour decoded string is: \n"<< encoded << endl;
}
But this should get you moving in the right direction with the shifting cipher. Hope it helps you out!
Enjoy!
"At DIC we be caesar cipher shifting code ninjas!"