An Atbash cipher is a simple substitution cipher wherein a key is used to determine what letters to swap, for example:
Original alphabet: abcdefghijklmnopqrstuvwxyz
Substitute (key): zyxwvutsrqponmlkjihgfedcba
Original message: hello world
Altered message: svool dliow
In my program, I get the key by printing the original letter, and then getting the letter that is to substitute it, and adding it to a key
string, for example:
a = <user input>
b = <user input>
c = <user input>
...
Here is the simple code that I use for it:
std::cout << "Key:\n";
std::string key;
for(int i = 0; i <= 25; i++){
std::cout << static_cast<char>('a' + i) << " = ";
char inputChar;
std::cin >> inputChar;
key += inputChar;
std::cin.ignore();
std::cin.clear();
std::cout << std::flush;
}
How can I improve this code?
std::cin.ignore();
andstd::cin.clear();
are superfluous. \$\endgroup\$char
s,- whitespace delimited or not -, should work fine in any implementation. \$\endgroup\$cin
andcout
that they actually ignore-clear-flush on death \$\endgroup\$