I'm trying to create a simple encryption program. You type in what you want encrypted. Then it evaluates the letters and prints out the encrypted ones.
The code inside the second if statement will never be run if the first one is true, even if word[1] is 'b'.
Just get rid of the "else" so the second if statement doesn't depend on the first one being false.
you could store a map with the letters as the key value and symbols as the mapped value, then just loop through the word and replace each character http://www.cplusplus.com/reference/stl/map/
That kind of looks complex, and I am a begginer. I understand what it is. I just don't understand the coding. Could you explain it, or give me another idea.
Alternatively, you can use strings that map decrypted values to encrypted values:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
constchar *decrypted = "abcdefgh";
constchar *encrypted = "%7+1&/:~";
string word = "bag"; //for example
for (size_t i = 0; i < word.length (); ++i)
{
size_t n = decrypted.find (word[i]);
if (n == string::npos)
{
// The character in `word' was not found in `decrypted'. Handle the problem however you wish.
}
else
{
cout << encrypted[n];
}
}
Notice that I said "map", just as quirkyusername did earlier. The STL provides std::map via the <map> header:
1 2 3 4 5 6 7 8 9 10
map<char, char> encryption_map;
encryption_map['a'] = '%';
encryption_map['b'] = '7';
// Same for the rest of the characters.
// It is probably best to delegate initialization to an initialize_map() function to keep this out of your main program.
for (size_t i = 0; i < word.length (); ++i)
{
cout << encryption_map[word[i]];
}