Please I need urgent help, I'm supposed to turn this in a few hours.
The program's supposed to ask the user 3 options:
1 for Encryption 2 for Decryption 3 to Exit
For Encryption first step is to ask for a plain text to be encrypted, the program gets past that.
Next step is to ask for the Encryption/Decryption code, when I put in something that's where the error comes up
Here's the full code:
#include bits/stdc++.h
#include stdio.h
using namespace std;
void encrypt001();
void decrypt001();
string yes;
string inname;
string key;
string alphabets="abcdefghijklmnopqrstuvwxyz";
int size=0;
int y;
int main()
{
int useroption;
do
{
cout<<"*****ENIGMA****"<<endl;
cout<<"[1] - Encrypt"<<endl;
cout<<"[2] - Decrypt"<<endl;
cout<<"[3] - Exit"<<endl;
cout<<"Enter Choice:"<<endl;
cin>>useroption;
switch (useroption)
{
case 1:
encrypt001();
break;
case 2:
decrypt001();
break;
default:
cout<<"Exit"<<endl;
break;
}
}
while (useroption!=3);
}
void encrypt001()
{
cout<<"*****ENCRYPTION******"<<endl;
string encrypt;
string ekey;
cin.ignore();
cout<<"Enter Plain Text To Encrypt:"<<endl;
getline(cin,encrypt);
cout<<"Enter Encryption Key:"<<endl;
cin>>ekey;
int elen=encrypt.length();
int ekeylen=ekey.length();
int letterslen=alphabets.length();
int num;
int y=0;
for (int x=0; x<elen; x++)
{
for (int h=0; h<letterslen; h++)
{
if (ekey.at(y)==alphabets[h])
{
num=h;
}
}
int num1=0;
int num2=0;
string space;
space=encrypt.at(x);
if (space==" ")
{
continue;
}
for (int j=0; j<letterslen; j++)
{
if (encrypt[x]==alphabets[j])
{
num1=j;
}
}
num2=num+num1;
if (num2>25)
{
num2=num2-26;
}
string letterrep;
letterrep=alphabets.at(num2);
encrypt.replace(x, 1, letterrep);
y++;
if (y>ekeylen)
{
y=0;
}
}
cout<<"Encrypted Cipher Text:"<<encrypt<<endl;
cout<<"Save To File? (y/n)"<<endl;
cin>>yes;
if (yes=="y")
{
cout<<"Enter File Name:"<<endl;
cin>>inname;
cout<<inname<<".txt saved succcessfully."<<endl;
}
else
{
cout<<"Proceed"<<endl;
}
}
void decrypt001()
{
cout<<"*****DECRYPTION*****"<<endl;
string decrypt;
string dkey;
string output;
cin.ignore();
cout<<"Enter Cipher Text to Decrypt:"<<endl;
getline(cin, decrypt);
cout<<"Enter Decryption Key:"<<endl;
cin>>dkey;
int dlen=0;
int dkeylen=0;
for (int k=0; k<decrypt.length(); k++)
{
if (decrypt[dlen]!=' ')
{
output+=((((decrypt[dlen]-97)+26)-(dkey[dkeylen]-97))%26)+97;
dkeylen++;
dlen++;
if (dkeylen==dkey.length())
{
dkeylen=0;
}
}
else if (decrypt[dlen]==' ')
{
output+=" ";
dlen++;
}
}
cout<<"Decrypted Plain Text:"<<output<<endl;
}
sorry for the first part formatting, I don't have enough reputation to post multiple links.
The decryption part works just fine.
error is terminate called std::out_of_range
Please how do I fix this?
Aucun commentaire:
Enregistrer un commentaire