Hello,
I wrote the below program to encrypt and decrypt words, words that are in lower case and no numbers or symbols.
My question is how can I add to the code so that it gives an error when capital letters or numbers or symbols are entered?
#include <iostream>
#include <cstdlib>
#include <string>
usingnamespace std;
int main ()
{
string method, map, word, str;
int encryption, decryption;
cout<<"What is the method (encryption or decryption)?"<<endl;
cin>>method;
if (method=="encryption")
{
cout<<"What is the translation map (type 'default' to use default):"<<endl;
cin>>map;
if (map=="default")
{
cout<<"What is the single word to translate:"<<endl;
cin>>word;
for(int i = 0; i < word.length(); i++)
word.at(i) = 'a' - word.at(i) + 122; //'z' == 122
cout<<"encrypted word: "<<word<<endl;
}
else
cout<<"encryption cannot be performed."<<endl;
}
elseif (method=="decryption")
{
cout<<"What is the translation map (type 'default' to use default):"<<endl;
cin>>map;
if (map=="default")
{
cout<<"What is the single word to translate:"<<endl;
cin>>word;
for(int i = 0; i < word.length(); i++)
word.at(i) = 'a' - word.at(i) + 122; //'z' == 122
cout<<"decrypted word: "<<word<<endl;
}
else
cout<<"decryption cannot be performed."<<endl;
}
else
cout<<"Error: invalid method choice."<<endl;
return 0;
}
#include <iostream>
#include <cstdlib>
#include <string>
usingnamespace std;
int main ()
{
string method, map, word, str;
int encryption, decryption;
cout<<"What is the method (encryption or decryption)?"<<endl;
cin>>method;
if (method=="encryption")
{
cout<<"What is the translation map (type 'default' to use default):"<<endl;
cin>>map;
if (map=="default")
{
cout<<"What is the single word to translate:"<<endl;
cin>>word;
for(unsignedshort i = 0; i < word.size(); i++)
{
if((word[i] >= 'a') && (word[i] <= 'z'))
{
for(int i = 0; i < word.length(); i++)
word.at(i) = 'a' - word.at(i) + 122; //'z' == 122
cout<<"encrypted word: "<<word<<endl;
}
else
cout<<"not valid"<<endl;
}
}
else
cout<<"encryption cannot be performed."<<endl;
}
elseif (method=="decryption")
{
cout<<"What is the translation map (type 'default' to use default):"<<endl;
cin>>map;
if (map=="default")
{
cout<<"What is the single word to translate:"<<endl;
cin>>word;
for(int i = 0; i < word.length(); i++)
word.at(i) = 'a' - word.at(i) + 122; //'z' == 122
cout<<"decrypted word: "<<word<<endl;
}
else
cout<<"decryption cannot be performed."<<endl;
}
else
cout<<"Error: invalid method choice."<<endl;
return 0;
}