Thread: crack a cipher with string arrays

  1. #1
    Registered User
    Join Date
    Sep 2009
    Posts
    2

    crack a cipher with string arrays

    hey guys, I have the assignment to build a program to decode a cryptogram puzzle using provided word tables. for each table the program should print the decoded text generated by that table and the number of actual english words in the decoded text. the encoded text is in the file crypt.txt and its less than 80 characters. the codetables are in codetables.txt and each line contains a tablein the form of an ASCII string and the program should process however many tables there are. the file dict.txt contains the dictionary which the program should use to determine word validity.

    I had the program set up to save each table then test all of them at once but decided to change it to read in a table then test it immediatly and output then move onto the next table. but on restructuring it I keep getting logic errors.

    please take a look at my code and see what you think, also let me know if you have tips on how I could structure this better.
    thanks, i'd appreciate all the help i can get

    Code:
    #include <iostream>
    #include <string>
    #include <iomanip>
    #include <fstream>
    using namespace std;
    
    //**********************************
    //*********Function Prototypes******
    //**********************************
    void buildAlpha (string&);
    void getCrypt   (string&, string&);
    void getTable   (string&, string&);
    void testTable  (string&, string&, string);
    int findPositionOf (char, string);
    //**********************************
    //********Main Function*************
    //**********************************
    
    int main(){
                                            // my arrays
    string crypt;
    string alpha;
    //string answer;
    string table, decrypt;
    buildAlpha (alpha);
    getCrypt   (crypt, decrypt);
    getTable (table, decrypt);
    
       cout << "Cipher" << endl;
    for(int i=0; i<80; i++)
      { cout << crypt [i]; }
       cout << " " << endl;
       cout << "Alphabet" << endl;
    
     for(int i=0; i<26; i++)
      { cout << alpha [i]; }
        cout << " " << endl;
    
     return 0;
    }
    
    //**********************************
    //********Function Definitions******
    //**********************************
    
    void buildAlpha (string & alpha)
    {
    for(int i=0; i<26; i++)
        { alpha [i] = 'a'+i; }
    }
    
    void getCrypt (string & crypt, string & decrypt)
    {
    ifstream numdata;
        numdata.open("crypt.txt");
        if ( !numdata )                               // Check to make sure file was opened
        {
            cout << "Error opening file" << endl;
        }
            else
            {
            int i = 0;
       {
       getline(numdata, crypt);
       i++;
       }
    for(int i=0; i<80; i++)
       { decrypt[i] = crypt [i]; }
       numdata.close();
    }
    }
    
    void getTable (string & table, string & decrypt)
    {
    ifstream numdata;
        numdata.open("codetable.txt");
        if ( !numdata )                               // Check to make sure file was opened
        {
            cout << "Error opening file" << endl;
        }
            else
            {
            while (!numdata.eof())
    
               getline(numdata, table);                     //get table 1
       cout << "Table 1" << endl;
     for(int i=0; i<26; i++)
      { cout << table [i]; }
        cout << " " << endl;
    
               testTable (table, decrypt, crypt);           //test table 1
       cout << "Table 1 tested" << endl;
     for(int i=0; i<26; i++)
      { cout << decrypt [i]; }
        cout << " " << endl;
    
               getline(numdata, table);                     //get table 2
       cout << "Table 2" << endl;
     for(int i=0; i<26; i++)
      { cout << table [i]; }
        cout << " " << endl;
    
               testTable (table, decrypt, crypt);           //table 2 tested
       cout << "Table 2 tested" << endl;
     for(int i=0; i<26; i++)
      { cout << decrypt [i]; }
        cout << " " << endl;
    
    
               getline(numdata, table);
       cout << "Table 3" << endl;                           //get table 3
     for(int i=0; i<26; i++)
      { cout << table [i]; }
        cout << " " << endl;
    
    
               testTable (table, decrypt, crypt);           //table 3 tested against cipher
       cout << "Table 3 tested" << endl;
     for(int i=0; i<26; i++)
      { cout << decrypt [i]; }
        cout << " " << endl;
    
               getline(numdata, table);                     //get table 4
       cout << "Table 4" << endl;
     for(int i=0; i<26; i++)
      { cout << table [i]; }
        cout << " " << endl;
    
               testTable (table, decrypt, crypt);                   //table 4 tested against cipher
       cout << "Table 4 tested" << endl;
     for(int i=0; i<26; i++)
      { cout << decrypt [i]; }
        cout << " " << endl;
    
               getline(numdata, table);                     //get table 5
     cout << "Table 5" << endl;
     for(int i=0; i<26; i++)
      { cout << table [i]; }
        cout << " " << endl;
    
               testTable (table, decrypt, crypt);           //test table 5
       cout << "Table 5 tested" << endl;
     for(int i=0; i<26; i++)
      { cout << decrypt [i]; }
        cout << " " << endl;
    
            }
    
       numdata.close();
    }
    
    void testTable (string & table, string & decrypt, string crypt)
    {
       int  letPos;
    
       for (int i=0; i<=80; i++)
       {
       char letterToLookup = crypt[i];
       letPos = findPositionOf ( letterToLookup, table );
       decrypt[ i ] = table[ letPos ];
       }
    }
    int findPositionOf (char letterToLookup, string table)
    {
       for (int i=0; i<=26; i++)
       {
           if (letterToLookup == table[i])
              return i;
       }
    }
    }


    this is the output that i get on compiling

    sh-3.00$ g++ prog1.cpp
    prog1.cpp: In function 'void getTable(std::string&, std::string&)':
    prog1.cpp:91: error: conversion from 'char* (*)(const char*, const char*)' to non-scalar type 'std::string' requested
    prog1.cpp:103: error: conversion from 'char* (*)(const char*, const char*)' to non-scalar type 'std::string' requested
    prog1.cpp:117: error: conversion from 'char* (*)(const char*, const char*)' to non-scalar type 'std::string' requested
    prog1.cpp:129: error: conversion from 'char* (*)(const char*, const char*)' to non-scalar type 'std::string' requested
    prog1.cpp:141: error: conversion from 'char* (*)(const char*, const char*)' to non-scalar type 'std::string' requested
    prog1.cpp: At global scope:
    prog1.cpp:171: error: expected declaration before '}' token
    prog1.cpp: In function 'int findPositionOf(char, std::string)':
    prog1.cpp:170: warning: control reaches end of non-void function

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    The thing you're compiling and the thing you've posted are not the same thing. For instance, line 91 is
    Code:
    testTable (table, decrypt, crypt);
    which doesn't have any conversion to string in it anywhere. What it does have is a completely unknown variable "crypt". Same with all your other lines -- they're all on the lines with testTable on them, but you must have changed them since you compiled since the error messages indicate something like table = testTable(decrypt, crypt) or something.

  3. #3
    Registered User
    Join Date
    Sep 2009
    Posts
    2
    thanks guys, I made some changes, got rid of decrypt and now just call get crypt in a loop to reset it, and a few other things. The codetables come in and output correctly but there seems to be a problem with the letter switch in test table and find position. because all the decrypted code comes out the same. Once again I'd appreciate some tips. thanks any tips help a bunch.heres the new improved code

    Code:
    #include <iostream>
    #include <string>
    #include <iomanip>
    #include <fstream>
    using namespace std;
    
    //**********************************
    //*********Function Prototypes******
    //**********************************
    void getCrypt   (string&);
    void getTable   (string&, string&);
    void testTable  (string&, string&);
    int findPositionOf (char, string);
    //**********************************
    //********Main Function*************
    //**********************************
    
    int main(){
                                            // my arrays
    string crypt;
    string table;
    getCrypt (crypt);
    
       cout << "Cipher" << endl;
    for(int i=0; i<80; i++)
      { cout << crypt [i]; }
       cout << " " << endl;
    
    getTable (table, crypt);
    
     return 0;
    }
    
    //**********************************
    //********Function Definitions******
    //**********************************
    
    void getCrypt (string & crypt)
    {
    ifstream numdata;
        numdata.open("crypt.txt");
        if ( !numdata )                               // Check to make sure file was opened
        {
            cout << "Error opening file" << endl;
        }
            else
            {
       getline(numdata, crypt);
       numdata.close();
            }
    }
    
    void getTable (string & table, string & crypt)
    {
    int count = 1;
    ifstream numdata;
        numdata.open("codetable.txt");
        if ( !numdata )                               // Check to make sure file was opened
        {
            cout << "Error opening file" << endl;
        }
            else
            {
                    while (!numdata.eof())
                    {
               getline(numdata, table);                     //get table 1
       cout << "Table " << count << endl;
     for(int i=0; i<26; i++)
      { cout << table [i]; }
        cout << " " << endl;
    
               testTable (table, crypt);                    //test table 1
       cout << "Table "<<count<<" tested" << endl;
     for(int i=0; i<26; i++)
      { cout << crypt [i]; }
        cout << " " << endl;
    
        getCrypt (crypt);
        count++;
                    }
            }
    
       numdata.close();
    }
    
    void testTable (string & table, string & crypt)
    {
       int  letPos;
    
       for (int i=0; i<=80; i++)
       {
       char letterToLookup = crypt[i];
       letPos = findPositionOf ( letterToLookup, table );
       crypt[ i ] = table[ letPos ];
       }
    }
    
    int findPositionOf (char letterToLookup, string table)
    {
       for (int i=0; i<=26; i++)
       {
           if (letterToLookup == table[i])
              return i;
       }
    }
    I still get this error but it does compile

    prog1.cpp: In function 'int findPositionOf(char, std::string)':
    prog1.cpp:105: warning: control reaches end of non-void function
    sh-3.00$ a.out

    Heres my output, see the decrypted sections are the same so the problem must be in findposition I just don't see it.

    Cipher
    vshx su wgcy wx mcix sy, cvwclu gcu rxxe, cvwclu wsvv rx.;`
    Table 1
    bcdefghijklmnopqrstuvwxyza
    Table 1 tested
    vshxsuwgcywxmcixsyc
    Table 2
    bcsdfgleuvqwxzijkmhnptayor
    Table 2 tested
    vshxsuwgcywxmcixsyc
    Table 3
    lgtakcdevpjusfybxmhzrwoniq
    Table 3 tested
    vshxsuwgcywxmcixsyc
    Table 4
    cfelsvptmxzkdbyghinjauroqw
    Table 4 tested
    vshxsuwgcywxmcixsyc
    Table 5
    cirsdgavuwhjtxmynzopflbekq
    Table 5 tested
    vshxsuwgcywxmcixsyc
    Table 6
    irsdgavuwhjtxmynzopflbekq
    Table 6 tested
    skivideridirqiir

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. char Handling, probably typical newbie stuff
    By Neolyth in forum C Programming
    Replies: 16
    Last Post: 06-21-2009, 04:05 AM
  2. String Class
    By BKurosawa in forum C++ Programming
    Replies: 117
    Last Post: 08-09-2007, 01:02 AM
  3. Compile Error that i dont understand
    By bobthebullet990 in forum C++ Programming
    Replies: 5
    Last Post: 05-05-2006, 09:19 AM
  4. Program using classes - keeps crashing
    By webren in forum C++ Programming
    Replies: 4
    Last Post: 09-16-2005, 03:58 PM
  5. string handling
    By lessrain in forum C Programming
    Replies: 3
    Last Post: 04-24-2002, 07:36 PM