0

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;


}
Adrian Mole
  • 49,934
  • 160
  • 51
  • 83

1 Answers1

0

Your message[i] > 'Z' and message[i] < 'A' should be inside the first if, where you already have detected a letter. If it's outside, it's also changing any non-letter characters too! (for example, space ' ' passes message[i] < 'A' and gets changed)

Here's the fixed code, I just placed the ifs in the correct place

#include<iostream>
#include<string>
using namespace std; // This is bad, you should get used to writing std::cout
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;
}
Offtkp
  • 366
  • 2
  • 9
  • Hey thanks a bunch, really helped a lot. just a follow up if you have time, why is it considered bad to use "using namespace std;"? i ask becasue so far thats how ive been taught and would like to know why its frowned upon – Amando Juarez Jan 04 '22 at 03:29
  • @AmandoJuarez Hey if this answer helped you please click the check mark to mark it as the accepted answer. Here's [a list of reasons](https://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice) why it's considered bad practice – Offtkp Jan 04 '22 at 16:31