ROT-13 using the entire printable ASCII Character Set C++

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{|}~)

" Please enter your password.

abcdefghijklmnopqrstuvwxyz{|}~

nopqrstuvwxyz{|}~!???????????? "

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#include <iostream>
#include <string>
using namespace std;

void encrypt(string password)
{

int count = 0;
char i;

    for (count = 0; count < password.length(); count++)
    {
        i = password.at(count) + 13;
            if (i >= 127)
                {
                    i = 32 + (i - 126);
                    cout << i;
                }
            else 
                cout << i;          
    }
    return;
}

int main()
{
    string password;

    cout << "Please enter your password." << endl;
    cin >> password;

    encrypt(password);
    cout << "\n";
    return 0;
}
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.

Hope this helps.

Last edited on
I'm not quite sure I know what you mean. Would you mind showing what you're trying to tell me in code?
Thank you so much!
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.

Hope this helps.
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.
Last edited on
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 unsigned char 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<unsigned char>( i ) >= 127)
        {
            i += 32 - 126;   
        }
        cout << i;          
    }
}
Last edited on
Registered users can post here. Sign in or register to post.