-1

I have 4 functions: encrypt(), decrypt(), generate_key() and menu().

The menu just asks the user what action to perform and using if and else if it will execute the selected function like this:

if (choice == 1) {
   encrypt();
   
   menu();
}

So what will happen is that the encrypt() function should execute first and after it finishes executing, it will go back to the menu by calling the menu() function.

Now this is my encrypt() function:

    int encrypt() {
    // Ask the user for a message to encrypt and the key to use
    std::cout << "Enter a message to encrypt: ";
    std::string message;
    std::cin >> message;
    
    std::cout << "Enter a key to use: ";
    int key;
    std::cin >> key;
    
    // Encrypt the message
    std::string encryptedMessage = "";
    for (int i = 0; i < message.length(); i++) {
        encryptedMessage += (message[i] + key);
    }
    
    // Print the encrypted message
    std::cout << "Encrypted message: " << encryptedMessage << std::endl;
    
    return 0;
    }

From what you see here, the program should ask for a message and the user enters one then asks for key and enters a key as well, then it would "encrypt" the message and print it. But what happens is that it asks for a message and then after the user enters one, asks for key, however when you enter the key, the application just closes with no error messages, no nothing. I'm really clueless and don't know what I'm doing wrong.

There's my full code: https://paste.ubuntu.com/p/NdVTKmcg9J/

nnaem
  • 177
  • 1
  • 2
  • 12
  • `std::cin >> message;` Will the message contain a space character? Remember that this stops reading at the first space character typed. – drescherjm May 02 '22 at 14:23
  • It depends, I tested with and without, with spaces the program just crashes after entering the message, without it goes to key which doesn't have spaces but has special characters, letters and numbers. – nnaem May 02 '22 at 14:24
  • If there is the possibility of typing a space you need `std::getline()` instead of `cin >> message;` – drescherjm May 02 '22 at 14:24
  • I tried implementing that but it asked for the message and then instantly without waiting for input went to key. Maybe I did something wrong? Could you provide an example, please? – nnaem May 02 '22 at 14:25
  • Related: [https://stackoverflow.com/questions/39881604/how-to-read-names-with-space](https://stackoverflow.com/questions/39881604/how-to-read-names-with-space) – drescherjm May 02 '22 at 14:26
  • From what I understand, this `std::getline(std::cin, messageToEncrypt);` should work? – nnaem May 02 '22 at 14:28
  • 1
    If the key is supposed to have special characters and letters, how do you expect to be able to store it in an `int`? – Nathan Pierson May 02 '22 at 14:29
  • Oh god, thank you for poiting that out, I'll try using a string. – nnaem May 02 '22 at 14:31
  • Okay but now there's another problem, https://imgur.com/a/86BS620 – nnaem May 02 '22 at 14:32
  • 1
    We don't know what your code is supposed to do. Don't apply fixes blindly, without any thought. You seem to try to implement Ceasar's cypher, which needs an `int` key. [Care is needed when mixing `getline` with `>>`](https://stackoverflow.com/questions/21567291/why-does-stdgetline-skip-input-after-a-formatted-extraction). The problem might be also that after entering key your program ends and terminal window closes so fast that you don't notice the output. [You are also not allowed](https://stackoverflow.com/questions/2128321/can-main-function-call-itself-in-c) to call `main()` yourself – Yksisarvinen May 02 '22 at 14:38
  • Okay thank you so much for helping, I fixed the input. Also the key being `int` means it can only be numbers right? Just making sure.. – nnaem May 02 '22 at 14:45
  • Yes, it must be a number. If your `int` is 32-bit, it needs to be a number between -2 billion and 2 billion. – Yksisarvinen May 02 '22 at 14:51
  • Well, nothing works. I'll try making something else then. Than you for your help! – nnaem May 02 '22 at 15:11

1 Answers1

0

you can't just add char to string in c++, you have to use stringstream

#include <sstream>
int encrypt() {
    // Ask the user for a message to encrypt and the key to use
    std::cout << "Enter a message to encrypt: ";
    std::string message;
    std::cin >> message;

    std::cout << "Enter a key to use: ";
    int key;
    std::cin >> key;

    std::stringstream encryptedMessage;
    for (int i = 0; i < message.length(); i++) {
        encryptedMessage << (message[i] + key); // "add" the result to stream
    }

    // Print the encrypted message
    std::cout << "Encrypted message: " << encryptedMessage.str() /*build the string*/ << std::endl;

    return 0;
}
Majorek
  • 1
  • 1