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;
}