I am trying to create a code that will use the entire printable ASCII Character set. My problem is that when it comes to characters that will be a number higher than 126 they print as '?', except for 'r', which prints correctly. Why does my code allow 'r' to roll back up to a printable character but not any characters after that? (stuvwxyz{|}~)
You need to give consideration to the offset of the printable characters.
<unprintable characters> <printable characters> <unprintable characters> ^32^127
First, subtract the offset. Then add your shift (+13) modulo N (which should be 95, I think). Then add the offset back in.
Okay. Given “ABCDEFGHIJKLMNOPQRSTUVWXYZ”, I am only interested in shifting the underlined values.
Some visualizations: each line represents a shift by one:
ABCDEFGHIJKLMNOPQRSTUVWXYZ
ABCDEFHIJKLMNOPQGRSTUVWXYZ
ABCDEFIJKLMNOPQGHRSTUVWXYZ
ABCDEFJKLMNOPQGHIRSTUVWXYZ
Our input alphabet Γ is composed of the 26 English majuscules.
The shifting alphabet Ω is composed only of the 11 consecutive English majuscules starting at 'J' (the 6th element of Γ).
Given a shift N, I can compute the new message with:
an’ = ((an - 6) + N) % 11 + 6
You are doing the very same:
Γ is the full range of character values 0..255.
Ω is the printable ASCII values from 32..126.
some people have had problems (mac?) printing extended ascii. See if you can print from 127 to 255 in a dumb cout loop. If not, your machine may not support this in your terminal.
If you continue to get ? after fixing the code, do this test.
One of the problems here is that char i; is a signed type, its value will be in the range -128 to +127. Because of that, this line cannot work as intended:
if (i >= 127)
because we already know that i can never be greater than 127. There are many possible solutions to this. I show one possibility below. The value is cast to an unsignedchar before that test.
1 2 3 4 5 6 7 8 9 10 11 12
void encrypt(string password)
{
for (size_t count = 0; count < password.length(); count++)
{
char i = password.at(count) + 13;
if ( static_cast<unsignedchar>( i ) >= 127)
{
i += 32 - 126;
}
cout << i;
}
}