Now, consider this: C++ does not specify that the character 'A' must have a value of 65; nor does it specify that the upper case characters in the basic execution character set must have contiguous values.
Other than that a. the null character has a value of zero, b. the value of each character after 0 in the list of decimal digits shall be one greater than the value of the previous, and c. the values of the characters in the basic execution character set are non-negative, "the values are locale specific".
Learning to write robust, portable code is not difficult; unlearning bad programming habits (often encouraged/insisted-on by 'career' teachers) is hard.
I can think of using that by calculating everything in one go and using%26 to break up the line into rows. I'm sure you can use % to calculate the letters more efficiently, but I'm at a loss on how to do that currently. What I don't understand is how to use % when starting the alphabet over, going from z to a.
JLBorges,
The more I look at these forums, the more bad habits I realize that I've been taught. I will look up a couple of things in your code that I'm not familiar with and hopefully learn more. Thank you for your reply!
I can think of using that by calculating everything in one go and using%26 to break up the line into rows. I'm sure you can use % to calculate the letters more efficiently, but I'm at a loss on how to do that currently. What I don't understand is how to use % when starting the alphabet over, going from z to a.
Not nearly as elegant or as bug/error proof as JLBorges' solution.
1 2 3 4 5 6
constint NUM_LETTERS{ 26 };
for( int i{}; i < NUM_LETTERS; i++ ) {
for( int j{}; j < NUM_LETTERS j++ ) {
alpha[i][j] = 'A' + (i + j) % NUM_LETTERS;
}
}
Taking into account what JLBorges said (which I agree with),I'm trying to figure out now how to do a vigenere cipher without the bad programming habits. I'm still not quite understanding the algorithm. I know how to use the table on paper, but coming up with an algorithm and coding it is difficult.
1. There are 26 characters, each with an index number in the array between 0 and 25 inclusive
2. Take any plaintext character and find its corresponding index number, i
3. Add the shift number to i to encrypt the plaintext character
4. If the shifted index number is > 25 then subtract 25 ( hence %, modular arithmetic, because the index number must always remain in the 0 to 25 range.