Transposition cipher code not completely working

Pages: 12
An alternative, maybe tortuous, version (with one warning):
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
36
#include <iomanip>
#include <iostream>
#include <string>
#include <vector>

constexpr unsigned NEWLINE_AT { 10 };

int main()
{
    std::string from { "ONE OF THE EARLIEST KNOWN USES OF TRANSPOSITION "
                       "CODES WAS DESIGNED BY THE SPARTANS" };
    std::vector<std::vector<char>> to(NEWLINE_AT,
                                      std::vector<char>(NEWLINE_AT, '#'));

    for(std::size_t next_char {}, used_rows {};
        next_char < from.length() && used_rows < NEWLINE_AT;
        ++used_rows)
    {
        for(std::size_t step { used_rows == 0 ? used_rows : NEWLINE_AT - 1 };
            step < NEWLINE_AT;
            ++step)
        {
            for(int col = step, row = used_rows;
                col > -1 && row < NEWLINE_AT && next_char < from.length();
                --col, ++row)
            {
                to.at(row).at(col) = from.at(next_char++);
            }
        }
    }

    for(const auto& v : to) {
        for(const auto c : v ) { std::cout << c; }
        std::cout << '\n';
    }
}


Output:
ON   INEAO
EOTEEOSNNA
FHASW S SE
ERTNOPC DE
L  FOOD  A
KU SDEBSN#
STIESYPS##
RTSI A####
I GTR#####
WNHT######

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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#include <iostream>
#include <string>
using namespace std;

const int SIZE = 10;
const char BLANK = ' ';


string cipher( string text, bool encode )
{
   if ( text.size() > SIZE * SIZE )                        // Too big
   {
      cout << "Unable to encode/decode\n";
      return text;
   }
   else if ( text.size() < SIZE * SIZE )                   // Pad to size
   {
      text += string( SIZE * SIZE - text.size(), BLANK );
   }

   string result( SIZE * SIZE, BLANK );

   int pos = 0, cipherPos = 0;
   int length;


   for ( int diagonal = 0; diagonal < 2 * SIZE - 1; diagonal++ )
   {
      if ( diagonal < SIZE )
      {
         length = diagonal + 1;
         pos = diagonal;
      }
      else
      {
         length = 2 * SIZE - 1 - diagonal;
         pos = ( diagonal + 2 - SIZE ) * SIZE - 1;
      }

      for ( int l = 0; l < length; l++ )
      {
         if ( encode ) result[pos] = text[cipherPos];
         else          result[cipherPos] = text[pos];
//       if ( encode ) result[cipherPos] = text[pos];
//       else          result[pos] = text[cipherPos];
         pos += ( SIZE - 1 );
         cipherPos++;
      }
   }

   return result;
}


int main()
{
   string test = "ONE OF THE EARLIEST KNOWN USES OF TRANSPOSITION "
                 "CODES WAS DESIGNED BY THE SPARTANS";

   cout << "Original: " << test << "\n";

   test = cipher( test, true );
   cout << "Encoded: " << test << "\n";

   test = cipher( test, false );
   cout << "Decoded: " << test << "\n";
}


Original: ONE OF THE EARLIEST KNOWN USES OF TRANSPOSITION CODES WAS DESIGNED BY THE SPARTANS
Encoded: ON   INEAOEOTEEOSNNAFHASW S SEERTNOPC DEL  FOOD  AKU SDEBSN STIESYPS  RTSI A    I GTR     WNHT      
Decoded: ONE OF THE EARLIEST KNOWN USES OF TRANSPOSITION CODES WAS DESIGNED BY THE SPARTANS
That's indeed a complete and flexible solution, lastchance!
Thank you so much. I haven't taken a look at your code yet as I still want to figure it out on my own (I learn the most this way), but I definitely will once my code works. This is what I have, but it keeps getting stuck. Transposition is meant to start at [0,0] and fill the diagonals and increase by 1 after each box. But the program keeps crashing, "main.exe not working". I changed the transposition function from my original post.

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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177

#include <iostream>

using namespace std;

class Functions
{
    private:
        char cipherArray[10][10];
        char cipherArrayTwo[10][10];
        int input{};
        int incrementTwo{};
        int counter{};
        int ones{};
        int holder{};
        int rows{};
        string sentance;

    public:
        void arrayInitialize (char (&tempArray)[10][10])
        {
            for (int j=0;j<10;j++)
            {
                for (int i=0;i<10;i++)
                {
                    tempArray[j][i]='#';
                }
            }
        }

        void intArrayInitialize (int (&tempArray)[10])
        {
            for (int i=0;i<10;i++)
            {
                if (i==0)
                {
                    tempArray[i]=0;
                }

                else
                {
                    incrementTwo++;
                    tempArray[i]+=incrementTwo+tempArray[i-1];
                }
            }
        }

        void userInput (string &tempString)
        {
            cout << "Enter a sentance: ";
            getline (cin, tempString);
            cout << endl;
        }

        void transposition ()
        {
            for (int i=0;i<10;i++)
            {
                cipherArrayTwo[0][i]=cipherArray[rows][ones];
                ones++;
                counter=0;

                for (int j=0;j<=i;j++)
                {
                    if (i>0)
                    {
                        if (ones<10)
                        {
                            cipherArrayTwo[i+counter][i-counter]=cipherArray[rows][ones];
                            ones++;
                            counter++;
                        }

                        else if (ones>=10)
                        {
                            holder=ones;
                            rows=ones/10;
                            ones=holder%10;

                            cipherArrayTwo[i+counter][i-counter]=cipherArray[rows][ones];
                            ones=holder;
                            ones++;
                            counter++;
                        }

                    }

                }
            }
        }

        void stringArrayDisplay (char tempArray[10][10])
        {
            cout << "Encrypted Chart Form:" << endl << endl;

            for (int j=0;j<10;j++)
            {
                for (int i=0;i<10;i++)
                {
                   cout << cipherArrayTwo[j][i] << " ";
                }
                cout << endl;
            }

            cout << endl << "Encrypted Regular Form: ";

            for (int j=0;j<10;j++)
            {
                for (int i=0;i<10;i++)
                {
                    cout << tempArray[j][i];
                }
            }
            cout << endl << endl;
        }

        void arrayFill (char (&tempArray)[10][10])
        {
            for (int j=0;j<10;j++)
            {
                for (int i=0;i<10;i++)
                {
                    tempArray[j][i]=sentance[counter];
                    counter++;

                    if (counter==sentance.length())
                    {
                        break;
                    }
                }

                if (counter==sentance.length())
                {
                    break;
                }
            }

        }

        void menu ()
        {
            while (input!=3)
            {
                cout << "1. Transposition cipher" << endl;
                cout << "2. Code breaker" << endl << endl;
                cout << "Enter the number of the option you want to proceed with: ";
                cin >> input;
                cout << endl;
                cin.ignore();

                switch (input)
                {
                    case 1:
                        arrayInitialize(cipherArray);
                        userInput(sentance);
                        arrayFill(cipherArray);
                        arrayInitialize(cipherArrayTwo);
                        //intArrayInitialize(initialOnes);
                        transposition();
                        stringArrayDisplay(cipherArrayTwo);
                        break;
                }
            }
        }

};

int main()
{
    Functions program;
    program.menu();

    return 0;
}


@Shezshade,
I can't follow all your code (my bad!) but I do suggest that you PUT SOME DEBUGGING LINES IN; it's more informative than the boolean operation of does it crash or doesn't it.

Please put the line
cout << i+counter << " " << i-counter << " " << rows << " " << ones << endl;
BEFORE EACH of your current lines 69 and 80.

Then it will be clear to you where you go outside array bounds. Spectacularly.
It seems icy1 is the only one who can shed light on OP's code... :-)
Topic archived. No new replies allowed.
Pages: 12