#include<iostream>
#include<conio.h>
#include<string>
usingnamespace std;
string encrypt(string boom);
string decrypt(string &boom);
int main()
{
cout<<" Press E to encrypt or D to decrypt.\n \
-------------------------------------------------------------------------------\n\n";
string boom;
char res;
res=_getch();
if(res=='e')
{
cout<<"Enter the string to encrypt: "<<endl;
cin>> boom; // code works fine till here
string en;
int num=0;
for(int i=0; i<boom.length(); i++)
{
if(int(boom.at(i)%2)==1)
num=21;
else
num=20;
en.at(i)=int(boom.at(i))+num;
}
cout<<"Encrypted text is: "<<en<<endl;
}
return 0;
}
The empty string en is defined at line 25. Then later at line 35 en.at(i) attempts to modify characters of en which are outside the string. That is the cause of the error.