### Problem with Encryption Program.

Hi , I posted on the beginner's forum Earlier.
Even though it's a simple program , the assignment requires
the encryption to be done by an external function.
Basically the program receives an input , and encrypts it by
shifting the alphabetical letters by a desired amount. when the shift goes past Z
it should start over at the beginning of the alphabet.
Example : Encrypting "abcd" with a shift of 3 would output "defg"

my program returns a blank value.
 ``12345`` ``````Enter the shift distance: 3 Enter a string to be encrypted: abcd The original string was: abcd The encrypted message is: Decryption of the encrypted message: ``````

Here is the main.cpp which we received pre-coded.
 ``1234567891011121314151617181920212223242526`` ``````#include #include #include #include "task2functions.h" using namespace std; int main() { int shift; cout << "Enter the shift distance: "; cin >> shift; cin.ignore(std::numeric_limits::max(), '\n'); string testString; cout << "Enter a string to be encrypted: "; getline(cin,testString,'\n'); cout << "The original string was: " << testString << endl; string encrypted; encrypted = caesarCipher(testString, shift); cout << "The encrypted message is: " << encrypted << endl; cout << "Decryption of the encrypted message: "; cout << caesarCipher(encrypted, -1 * shift) << endl; return 0; } ``````

The function.cpp , which is where I have to our programming
 ``12345678910111213141516171819202122232425262728`` ``````#include "task2functions.h" #include #include #include using namespace std; string caesarCipher(const string& text,int shift) // DO NOT CHANGE HEADER { int x; string Encrypt; for (x=0; x < text.length(); x++) { toupper(text[x]); if (text[x] >= '!' && text[i] <= '@' || text[x] >= '[' && text[x] <= '}') Encrypt[x] = text[x]; else if (text[x] + shift > 'Z') Encrypt[x] = (text[x] -26) + shift; else Encrypt[x] += shift; } return Encrypt ; } ``````
Last edited on
You are lucky it did not crash.
string Encrypt was default constructed and has a length of 0, so any subscript operation will be out of bounds and cause undefined behavior.
When you are trying to output that string, it sees that length is still 0 and do not output anything.

Also I would advise against things like `text[x] >= '!'`. You cannot know what codepage will be used on target PC. That is why you should use functions like isalpha instead of checking character code.
I'm still very new to c++ programming , so I'm not really sure what
you're telling me. How do I fix it ?

Sorry X(
just make Encrypt same size as text.
Either by initializating by copy: `string Encrypt = text;`
Creating a string of some size: `string Encrypt(text.size());`
Or by resizing it after declaration:
 ``12`` ``````string Encrypt; Encrypt.resize(text.size());``````

Here is a bonus — caesar cypher in C++11:
http://ideone.com/55hXhc
Last edited on
Well, maybe you can use `Encrypt += text[x];`
Okay Thank you, it's working. but after Encrypting it outputs the original input again.
Can't figure out what's wrong with my function ?
 ``1234567891011121314151617181920212223`` ``````string caesarCipher(const string& text,int shift) // DO NOT CHANGE HEADER { int x; string Encrypt; for (x=0; x < text.length(); x++)//[Warning] comparison between signed and unsigned integer expressions [-Wsign-compare] { toupper(text[x]); if ((text[x] >= '!' && text[x] <= '@' )|| (text[x] >= '[' && text[x] <= '}'))//NOTE:priority! Encrypt += text[x]; else if (text[x] + shift > 'Z') Encrypt += (text[x] -26) + shift; else if(text[x]+shift<'A')//shift maight be negative! Encrypt += (text[x] +26) + shift; else Encrypt+=text[x]+shift;//Encrypt[x] += shift; } return Encrypt ; }``````
Small letters are matched by `(text[x] >= '[' && text[x] <= '}'))` condition and are not encrypted at all.