Thread: Problem in table referencing

  1. #1
    Registered User
    Join Date
    Mar 2003
    Posts
    5

    Problem in table referencing

    Can anyone help me in the table referencing? See the below example:
    -------------------------------------------------------------------
    Code:
    #include <iostream.h>
    #include <string.h>
    
    //#define TABLE_SIZE 10
    
    struct tableType
    {
       char actual;
       char cipher;
    };
    
    
    class secret
    {
       tableType table[10];
       char text[256];
       char temp[256];
       int length;
    
    public:
       secret();
       //~secret(){}
       void enter_message();
       //char get_message(); 
       void encrypt();
       void display();
    };
    
    secret::secret()
    {
    
       table[0].actual='A'; table[0].cipher='z';
       table[1].actual='B'; table[1].cipher='a';
       table[2].actual='C'; table[2].cipher='q';
       table[3].actual='D'; table[3].cipher='1';
       table[4].actual='E'; table[4].cipher='2';
       table[5].actual='F'; table[5].cipher='w';
       table[6].actual='G'; table[6].cipher='s';
       table[7].actual='H'; table[7].cipher='x';
       table[8].actual='I'; table[8].cipher='c';
       table[9].actual='J'; table[9].cipher='d';
    }
    
    void secret::enter_message()
    {
       cout << "Enter Your Message: ";
       cin.getline(text,256);
    }
    
    
    void secret::encrypt()
    {
       length=strlen(text);
    
       for (int x=0; x {
          for (int y=x; y<10; y++)
          {
             if (text[x]==table[y].actual)
                temp[x]=table[y].cipher;
          }
       }
    }
    
    void secret::display()
    {
       cout << endl;
       cout << "Original Text: " << text << endl << endl;
    
       cout << "Encrypted Text: " << temp << endl << endl;
    
       cout << "ACTUAL" << " " << "CIPHER" << endl;
       cout << "======" << " " << "======" << endl;
    
       for (int i=0; i<10; i++)
       {
          cout << table[i].actual << " " << table[i].cipher << endl;
       }
    }
    
    void main()
    {
       secret sc;
       sc.enter_message();
       sc.display();
    
       cout << endl << endl;
    }
    -------------------------------------------------------------------

    What I want is just create a table with 2 columns. One for actual value and the other is for cipher value. Then I'll enter a message and let the program to convert the character (of the message) to cipher one.

    I think I'll doing in the stupid and incorrect way. I need you guys advise. Please help.
    Last edited by elquex; 03-13-2003 at 10:53 AM.

  2. #2
    Registered User
    Join Date
    Feb 2003
    Posts
    29
    Use your defines!

    You have defined TABLE_SIZE but you donīt use it. Do yourself the favor!
    Also define like:
    Code:
    #define MAXLINESIZE 256
    and replace your magic 256-number with that.

    Your encrypt function seems to be wrong, you might wanna do like:

    Code:
    void secret::encrypt()
    {
       length=strlen(text);
    
       for (int x=0; x<lenght;x++){			// Run through string
          for (int y=0; y<TABE_SIZE; y++)	// Run through look-up table
          {
          	// If character matches look-up table character then put encrypted char in temp
             if (text[x]==table[y].actual) { temp[x]=table[y].cipher; }
    			// Else put normal char in temp
    			else 									{ temp[x]=text[x];}
          }
       }
    }

  3. #3
    Registered User
    Join Date
    Mar 2003
    Posts
    5
    Thanks... I'll try on it...

  4. #4
    Anti-Poster
    Join Date
    Feb 2002
    Posts
    1,401
    Originally posted by Hardboy
    Use your defines!

    You have defined TABLE_SIZE but you donīt use it. Do yourself the favor!
    Also define like:
    Code:
    #define MAXLINESIZE 256
    Is it better form to declare constants using defines, or declare constants using const?
    If I did your homework for you, then you might pass your class without learning how to write a program like this. Then you might graduate and get your degree without learning how to write a program like this. You might become a professional programmer without knowing how to write a program like this. Someday you might work on a project with me without knowing how to write a program like this. Then I would have to do you serious bodily harm. - Jack Klein

  5. #5
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    Code:
    const int ACTUAL = 10;
    const int CYPHER = 10;
    char table[ACTUAL][CYPHER];
    int i, j;
    
    //to decode a secret message using this table
    for(i= 0//etc)
    {
      for(j = 0; //etc)
      {
        //the cypher for a given j will always be element 1
        if(input[i] == table[j][1])
        {
           //the actual for a given j will always be element 0
          cout << table[0][j];
         }
      }
    }
    
    //to encode a secret message using this table reverse the above sequence

  6. #6
    Registered User
    Join Date
    Mar 2003
    Posts
    5
    Thanks for your help. I manage to do the program but using other way. I'm using the array of object as a table. But there is another problem.

    The problem is the compiler prompt me the message that the array size is too large. Can anyone help me again? Please click the below link.

    I have post this problem as another thread

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. problem with league table
    By vw1970 in forum C Programming
    Replies: 6
    Last Post: 01-04-2009, 09:16 AM
  2. problem in multi times table
    By lolguy in forum C Programming
    Replies: 11
    Last Post: 12-28-2008, 11:17 AM
  3. Problem referencing structure elements by pointer
    By trillianjedi in forum C Programming
    Replies: 19
    Last Post: 06-13-2008, 05:46 PM
  4. Writing array, to file
    By zootreeves in forum C Programming
    Replies: 9
    Last Post: 09-08-2007, 05:06 PM
  5. Problem with Hash Table Linear Probing
    By kirby1024 in forum C Programming
    Replies: 5
    Last Post: 10-23-2002, 06:03 AM