#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2013
    Posts
    3
    Rep Power
    0

    Jefferson's Cipher help! [C++,Code::Blocks,Win7]


    Hi there everyone, I'm new here and to programming and I wanted to wright in C++ a wheel cipher, but I have encountered a problem that I don't understand it has to do may be with the type of variables I'm using I don't really know anyway I came here hoping to find an answer, I welcome any help, suggestion and/or opinions about my code.
    after trying to find where the problem came from by using the function step into of the Code::Blocks debugger it stops at the line 74: meaning this condition if(plain_text[i]==cylinder_1[n]) in the first case of the switch.
    I was happy when the compiler showed Process terminated with status 0 (0 minutes, 1 seconds)
    0 errors, 0 warnings
    but when I run the program there is a problem and it stops.
    Thanks to you all in advance


    Code:
    #include <iostream> 
    #include <cctype> 
    #include <string.h> 
    #include <stdio.h>
     #include <stdlib.h> 
    using namespace std;
     int main() 
    { cout << "Hello world!" << endl; string cylinder_1="ZWAXJGDLUBVIQHKYPNTCRMOSFE", cylinder_2="KPBELNACZDTRXMJQOYHGVSFUWI", cylinder_3="BDMAIZVRNSJUWFHTEQGYXPLOCK", cylinder_4="RPLNDVHGFCUKTEBSXQYIZMJWAO", cylinder_5="IHFRLABEUOTSGJVDKCPMNZQWXY", cylinder_6="AMKGHIWPNYCJBFZDRUSLOQXVET", cylinder_7="GWTHSPYBXIZULVKMRAFDCEONJQ", cylinder_8="NOZUTWDCVRJLXKISEFAPMYGHBQ", cylinder_9="XPLTDSRFHENYVUBMCQWAOIKZGJ", cylinder_10="UDNAJFBOWTGVRSCZQKELMXYIHP";
     string plain_text,cipher_text,f_test; 
    unsigned int key[10]={0,0,0,0,0,0,0,0,0,0}; 
    int key_l=-1,cipher_row=-1;
     cout << " Enter key length between 4 and 10 \n"<< endl; 
    cin >> key_l; 
    for(int i=0;i<key_l;i++) 
    { cout <<"\nEnter key part "<< i+1 << endl; 
    cin >> key[i]; } 
    cipher_row=key[key_l-1]; 
    cout << "\n Enter your text: "<< endl; cin.ignore(); getline(cin,plain_text); 
    for(unsigned i=0;i < plain_text.length();i++)
     { plain_text[i]=toupper(plain_text[i],locale()); }
     //test //cout << plain_text<<'\n'
    ; int i=0,n=0,j=0,k=0,ptxt_l=plain_text.length();
     int existe=-1;
     cout << ptxt_l<<endl; 
    while( i<ptxt_l-1)
    { for (j=0;j<key_l;j++) 
    { switch (key[j]) 
    { case 1: 
    for (n=0;n<26 || existe== 1;n++) { if(plain_text[i]==cylinder_1[n]) 
    { k=n+cipher_row; if ( k > 25) k=k-25; cipher_text[i]=cylinder_1[k]; existe=1; }
     else if(plain_text[i]==' ')
     { cipher_text[i]=' '; existe=1; } }
     break;
     case 2:for (n=0;n<26 || existe== 1;n++) { if(plain_text[i]==cylinder_2[n]) { k=n+cipher_row; if ( k > 25) k=k-25; cipher_text[i]=cylinder_2[k]; existe=1; } else if(plain_text[i]==' ') { cipher_text[i]=' '; existe=1; } } break; case 3:for (n=0;n<26 || existe== 1;n++) { if(plain_text[i]==cylinder_3[n]) { k=n+cipher_row; if ( k > 25) k=k-25; cipher_text[i]=cylinder_3[k]; existe=1; } else if(plain_text[i]==' ') { cipher_text[i]=' '; existe=1; } } break; case 4:for (n=0;n<26 || existe== 1;n++) { if(plain_text[i]==cylinder_4[n]) { k=n+cipher_row; if ( k > 25) k=k-25; cipher_text[i]=cylinder_4[k]; existe=1; } else if(plain_text[i]==' ') { cipher_text[i]=' '; existe=1; } } break; case 5:for (n=0;n<26 || existe== 1;n++) { if(plain_text[i]==cylinder_5[n]) { k=n+cipher_row; if ( k > 25) k=k-25; cipher_text[i]=cylinder_5[k]; existe=1; } else if(plain_text[i]==' ') { cipher_text[i]=' '; existe=1; } } break; case 6:for (n=0;n<26 || existe== 1;n++) { if(plain_text[i]==cylinder_6[n]) { k=n+cipher_row; if ( k > 25) k=k-25; cipher_text[i]=cylinder_6[k]; existe=1; } else if(plain_text[i]==' ') { cipher_text[i]=' '; existe=1; } } break; case 7:for (n=0;n<26 || existe== 1;n++) { if(plain_text[i]==cylinder_7[n]) { k=n+cipher_row; if ( k > 25) k=k-25; cipher_text[i]=cylinder_7[k]; existe=1; } else if(plain_text[i]==' ') { cipher_text[i]=' '; existe=1; } } break; case 8:for (n=0;n<26 || existe== 1;n++) { if(plain_text[i]==cylinder_8[n]) { k=n+cipher_row; if ( k > 25) k=k-25; cipher_text[i]=cylinder_8[k]; existe=1; } else if(plain_text[i]==' ') { cipher_text[i]=' '; existe=1; } } break; case 9:for (n=0;n<26 || existe== 1;n++) { if(plain_text[i]==cylinder_9[n]) { k=n+cipher_row; if ( k > 25) k=k-25; cipher_text[i]=cylinder_9[k]; existe=1; } else if(plain_text[i]==' ') { cipher_text[i]=' '; existe=1; } } break; case 10:for (n=0;n<26 || existe== 1;n++) { if(plain_text[i]==cylinder_10[n]) { k=n+cipher_row; if ( k > 25) k=k-25; cipher_text[i]=cylinder_10[k]; existe=1; } else if(plain_text[i]==' ') { cipher_text[i]=' '; existe=1; } }
     break; }
     i++; }
     } 
    cout << plain_text<<'\n'; 
    // cout << cipher_text<<'\n';
     return 0; }
  2. #2
  3. Contributed User
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jun 2005
    Posts
    4,376
    Rep Power
    1871
    First of all, if you're going to write code, you should learn how to indent properly (or indeed at all).
    The compiler doesn't care at all, but human readers will just ignore poor presentation.

    Like so.
    Code:
    #include <iostream>
    #include <cctype>
    #include <string.h>
    #include <stdio.h>
    #include <stdlib.h>
    using namespace std;
    int main()
    {
      cout << "Hello world!" << endl;
      string cylinder_1 = "ZWAXJGDLUBVIQHKYPNTCRMOSFE", 
          cylinder_2 =  "KPBELNACZDTRXMJQOYHGVSFUWI", 
          cylinder_3 = "BDMAIZVRNSJUWFHTEQGYXPLOCK", 
          cylinder_4 = "RPLNDVHGFCUKTEBSXQYIZMJWAO", 
          cylinder_5 = "IHFRLABEUOTSGJVDKCPMNZQWXY", 
          cylinder_6 = "AMKGHIWPNYCJBFZDRUSLOQXVET", 
          cylinder_7 = "GWTHSPYBXIZULVKMRAFDCEONJQ", 
          cylinder_8 = "NOZUTWDCVRJLXKISEFAPMYGHBQ",
          cylinder_9 = "XPLTDSRFHENYVUBMCQWAOIKZGJ", 
          cylinder_10 ="UDNAJFBOWTGVRSCZQKELMXYIHP";
      string plain_text, cipher_text, f_test;
      unsigned int key[10] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
      int key_l = -1, cipher_row = -1;
    
      cout << " Enter key length between 4 and 10 \n" << endl;
      cin >> key_l;
      for (int i = 0; i < key_l; i++) {
        cout << "\nEnter key part " << i + 1 << endl;
        cin >> key[i];
      }
      cipher_row = key[key_l - 1];
    
      cout << "\n Enter your text: " << endl;
      cin.ignore();
      getline(cin, plain_text);
      for (unsigned i = 0; i < plain_text.length(); i++) {
        plain_text[i] = toupper(plain_text[i], locale());
      }
    
      //test //cout << plain_text<<'\n'
      ;
      int i = 0, n = 0, j = 0, k = 0, ptxt_l = plain_text.length();
      int existe = -1;
      cout << ptxt_l << endl;
      while (i < ptxt_l - 1) {
        for (j = 0; j < key_l; j++) {
          switch (key[j]) {
          case 1:
            for (n = 0; n < 26 || existe == 1; n++) {
              if (plain_text[i] == cylinder_1[n]) {
                k = n + cipher_row;
                if (k > 25)
                  k = k - 25;
                cipher_text[i] = cylinder_1[k];
                existe = 1;
              } else if (plain_text[i] == ' ') {
                cipher_text[i] = ' ';
                existe = 1;
              }
            }
            break;
          case 2:
            for (n = 0; n < 26 || existe == 1; n++) {
              if (plain_text[i] == cylinder_2[n]) {
                k = n + cipher_row;
                if (k > 25)
                  k = k - 25;
                cipher_text[i] = cylinder_2[k];
                existe = 1;
              } else if (plain_text[i] == ' ') {
                cipher_text[i] = ' ';
                existe = 1;
              }
            }
            break;
          case 3:
            for (n = 0; n < 26 || existe == 1; n++) {
              if (plain_text[i] == cylinder_3[n]) {
                k = n + cipher_row;
                if (k > 25)
                  k = k - 25;
                cipher_text[i] = cylinder_3[k];
                existe = 1;
              } else if (plain_text[i] == ' ') {
                cipher_text[i] = ' ';
                existe = 1;
              }
            }
            break;
          case 4:
            for (n = 0; n < 26 || existe == 1; n++) {
              if (plain_text[i] == cylinder_4[n]) {
                k = n + cipher_row;
                if (k > 25)
                  k = k - 25;
                cipher_text[i] = cylinder_4[k];
                existe = 1;
              } else if (plain_text[i] == ' ') {
                cipher_text[i] = ' ';
                existe = 1;
              }
            }
            break;
          case 5:
            for (n = 0; n < 26 || existe == 1; n++) {
              if (plain_text[i] == cylinder_5[n]) {
                k = n + cipher_row;
                if (k > 25)
                  k = k - 25;
                cipher_text[i] = cylinder_5[k];
                existe = 1;
              } else if (plain_text[i] == ' ') {
                cipher_text[i] = ' ';
                existe = 1;
              }
            }
            break;
          case 6:
            for (n = 0; n < 26 || existe == 1; n++) {
              if (plain_text[i] == cylinder_6[n]) {
                k = n + cipher_row;
                if (k > 25)
                  k = k - 25;
                cipher_text[i] = cylinder_6[k];
                existe = 1;
              } else if (plain_text[i] == ' ') {
                cipher_text[i] = ' ';
                existe = 1;
              }
            }
            break;
          case 7:
            for (n = 0; n < 26 || existe == 1; n++) {
              if (plain_text[i] == cylinder_7[n]) {
                k = n + cipher_row;
                if (k > 25)
                  k = k - 25;
                cipher_text[i] = cylinder_7[k];
                existe = 1;
              } else if (plain_text[i] == ' ') {
                cipher_text[i] = ' ';
                existe = 1;
              }
            }
            break;
          case 8:
            for (n = 0; n < 26 || existe == 1; n++) {
              if (plain_text[i] == cylinder_8[n]) {
                k = n + cipher_row;
                if (k > 25)
                  k = k - 25;
                cipher_text[i] = cylinder_8[k];
                existe = 1;
              } else if (plain_text[i] == ' ') {
                cipher_text[i] = ' ';
                existe = 1;
              }
            }
            break;
          case 9:
            for (n = 0; n < 26 || existe == 1; n++) {
              if (plain_text[i] == cylinder_9[n]) {
                k = n + cipher_row;
                if (k > 25)
                  k = k - 25;
                cipher_text[i] = cylinder_9[k];
                existe = 1;
              } else if (plain_text[i] == ' ') {
                cipher_text[i] = ' ';
                existe = 1;
              }
            }
            break;
          case 10:
            for (n = 0; n < 26 || existe == 1; n++) {
              if (plain_text[i] == cylinder_10[n]) {
                k = n + cipher_row;
                if (k > 25)
                  k = k - 25;
                cipher_text[i] = cylinder_10[k];
                existe = 1;
              } else if (plain_text[i] == ' ') {
                cipher_text[i] = ' ';
                existe = 1;
              }
            }
            break;
          }
          i++;
        }
      }
      cout << plain_text << '\n';
    // cout << cipher_text<<'\n';
      return 0;
    }
    Next, when you start copy/pasting code and putting numbers on the ends of variables, you're doing it wrong.
    Use an array.
    Code:
          string cylinders[10] = {
            "ZWAXJGDLUBVIQHKYPNTCRMOSFE",
            "KPBELNACZDTRXMJQOYHGVSFUWI",
            "BDMAIZVRNSJUWFHTEQGYXPLOCK", 
            // etc
          };
    Then your entire bloated case statement disappears and you have a single block reading something like
    Code:
            for (n = 0; n < 26 || existe == 1; n++) {
              if (plain_text[i] == cylinder[key[j]-1][n]) {
                k = n + cipher_row;
                if (k > 25)
                  k = k - 25;
                cipher_text[i] = cylinder[key[j]-1][k];
                existe = 1;
              } else if (plain_text[i] == ' ') {
                cipher_text[i] = ' ';
                existe = 1;
              }
            }
            break;
    > if (plain_text[i] == cylinder[key[j]-1][n])
    Your code needs a final else clause (or does it?), to catch all the cases where the first two tests do not match up.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2013
    Posts
    3
    Rep Power
    0
    Hi again! thank you salem for your respond I changed the code as you said "by the way sorry for the not mess I just copy/past from my compiler did know it would look like that"
    anyway
    Code:
          for (n = 0; n < 26 || existe == 1; n++) {
              if (plain_text[i] == cylinder[key[j]-1][n]) {
                k = n + cipher_row;
                if (k > 25)
                  k = k - 25;
                cipher_text[i] = cylinder[key[j]-1][k];
                existe = 1;
              } else if (plain_text[i] == ' ') {
                cipher_text[i] = ' ';
                existe = 1;
              }
            }
            break;
    anyway I still can't make it work the error it give me when debugging is SIGSEGV Segmentation fault and stops in this line
    Code:
    if (plain_text[i] == cylinder[key[j]-1][n])
    I'm not an expert in debugging but I don't think that this code should be segfaulting I guess something is wrong maybe a corrupted library who knows. I'm going to try re-installing code blocks and let you know. again thanks for your help!
  6. #4
  7. Contributed User
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jun 2005
    Posts
    4,376
    Rep Power
    1871
    segfaults just mean the code is broken.
    Re-installing the compiler / libraries won't fix anything.

    > if (plain_text[i] == cylinder[key[j]-1][n])
    Either use the debugger (or lots of print statements) to check the subscripts.
    i
    j
    n
    key[j]-1

    If any of these are out of range (one of them will be), then you get a segfault (well 99% of the time anyway).
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2013
    Posts
    3
    Rep Power
    0
    thanks for your help I wasn't home so I couldn't reply nor do some coding I hope tonight I would be creative.

IMN logo majestic logo threadwatch logo seochat tools logo