I'm learning strings and am trying to write a cipher program where a user inputs a string, inputs the key(how many spaces to shift a number either left or right) and outputs the encrypted string. I believe I have it almost figured out, But I need help fixing the issue where spaces and special characters get changed as well as the letters. I believe it has something to do with the toupper function, but I can't be sure. Any help would be appreciated!
#include<iostream>
#include<string>
using namespace std;
int main() {
string message;
int key;
cout << "Your message? ";
getline(cin, message);
cout << "Encoding key? ";
cin >> key;
for (int i = 0; i < message.length(); i++) {
if (islower(message[i])) {
message[i]=toupper(message[i]);
}
if (message[i] >= 'A' && message[i] <= 'Z') {
message[i] += key;
}
if (message[i] > 'Z') {
int overLimit = message[i] - 'Z';
message[i] = 'A' + overLimit - 1;
}
else if (message[i]<'A') {
int underLimit = 'A' - message[i];
message[i] = 'Z' - underLimit + 1;
}
}
cout << message << endl;
return 0;
}