Hey guys, I am working on this encryption/decryption project and I have just finished writing the encryption part of the code. I complied it and ran it. The first part worked well. However once I chose to encrypt a file ( I didnt choose decrypt because it was not completed), the encrypted part did not output, instead it was an empty space as though there was nothing written to the second string.

Can someone take a quick look at my encrypt function and see what is going on with my code, I know its probably just some easy problem, but I've been working on it for a bit and havent solved it yet. Thanks!

#include <iostream>
#include <string>
#include <cctype>

using namespace std;

void encrypt(string, int);
void decrypt(string);

int main ()
{
    string code;
    int choice, key;
    bool wrong = true;
    
    do
    {
         wrong = true;
         
         cout << "Enter 1 to  encrypt a message or 2 to decrypt a message?" << endl;
         cin >> choice;
         
         if (choice != 1 || choice !=2)
             wrong = false;
    }  while(wrong);
    
         
    if (choice == 1)
    {
          cout << endl << "Please enter the code that you would like to encrypt." << endl;
          cin >> code;
          cout << endl << "Please enter a key 1-100 to encrypt your message." << endl;
          cin >> key;
          encrypt(code, key);
    }
    
    
    else
    {
          cout << endl << "Please enter the code that you would like to decrypt." << endl;
          cin >> code;
          cout << endl;
          decrypt(code);
    }

    getchar ();
    getchar ();
    return 0;
}
    
void encrypt (string encrypt_code, int encrypt_key)
{
     string EncryptedChar;
     
     for (int i = 0; i <= encrypt_code.length(); i++)
     {
         if (encrypt_code[i] + encrypt_key > 126)
              EncryptedChar[i] = ((encrypt_code[i] + encrypt_key) - 127) + 32;
     
         else
              EncryptedChar[i] = (encrypt_code[i] + encrypt_key);
     }
     
     cout << "Your encrypted code is " << EncryptedChar << "using key " << encrypt_key << endl;
}

Does this actually compile? In your encryption function you are overwritting the bounds of the encryptedChar string. I would suggest using the append function to add charactures to the string. Or make encryptedChar the same size as encrypt_code and then assign it with your index.

Ok, so I finished working on the entire program, and I am still having the same problem, the program compiles just fine, but the strings will not display. How would the append function work to create the string that I need to display as the encryption. I am also having the same problem with my decryption function, but I know that if I figure one of them out, all I have to do is change the other one in the same way.

#include <iostream>
#include <string>
#include <cctype>

using namespace std;

void encrypt(string, int);
void decrypt(string);

int main ()
{
    string code;
    int choice, key;
    bool wrong = true;
    
    do
    {
         wrong = true;
         
         cout << "Enter 1 to  encrypt a message or 2 to decrypt a message?" << endl;
         cin >> choice;
         
         if (choice != 1 || choice !=2)
             wrong = false;
    }  while(wrong);
    
         
    if (choice == 1)
    {
          cout << endl << "Please enter the code that you would like to encrypt." << endl;
          cin >> code;
          cout << endl << "Please enter a key 1-100 to encrypt your message." << endl;
          cin >> key;
          encrypt(code, key);
    }
    
    
    else
    {
          cout << endl << "Please enter the code that you would like to decrypt." << endl;
          cin >> code;
          cout << endl;
          decrypt(code);
    }

    getchar ();
    getchar ();
    return 0;
}
    
void encrypt (string encrypt_code, int encrypt_key)
{
     string EncryptedChar;
     
     for (int i = 0; i <= encrypt_code.length(); i++)
     {
         if (encrypt_code[i] + encrypt_key > 126)
              EncryptedChar[i] = ((encrypt_code[i] + encrypt_key) - 127) + 32;
     
         else
              EncryptedChar[i] = (encrypt_code[i] + encrypt_key);
     }
     
     cout << "Your encrypted code is " << EncryptedChar << "using key " << encrypt_key << endl;
}

void decrypt (string decrypt_code)
{
     string DecryptedChar;
     for (int key = 1; key <= 100; key++)
     {
         for (int j = 0; j <= decrypt_code.length(); j++)
         {
             if (decrypt_code[j] - key < 32)
                  DecryptedChar[j] = ((decrypt_code[j] - key) + 127) - 32;
             else
                  DecryptedChar[j] = (decrypt_code[j] - key);
         }
         cout << "Key " << key << ":  " << DecryptedChar << endl; 
     }
}

I finally figured it out, my problem was when I was outputting it. I had to make a for loop to output each character in the string. Once I did that it worked perfectly. Thanks for your help though, I greatly appreciate it!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.