name encryption

This is a discussion on name encryption within the C++ Programming forums, part of the General Programming Boards category; G'day This is my second post, also i am new to C++ programming so if i make a mistake or ...

  1. #1
    Registered User
    Join Date
    Nov 2006
    Location
    under your bed....
    Posts
    24

    name encryption

    G'day

    This is my second post, also i am new to C++ programming so if i make a mistake or something please correct me ;P.

    Today i started this program that converts a persons name to code:

    Code:
    #include <iostream>
    
    using namespace std;
    
    int main()
    {
        // Database
        char cLetters[] = {'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'};    
        char cEncryption[] = {'9', 'x', 'j', '4', 't', '5', 'p', 'y', 'c', '3', 'v', 'b', '2', 'e', 'n', 'w', 'o', 'm', 'i', '1', 'r', '1', '7', 'k', 'u', '8'};
    
        // Input users name
        char cMiddleInitial[1];
        char cFirstname[13];
        char cLastname[13];
        
        cout << "Enter Firstname: ";
        cin >> cFirstname;  
        cout << "Enter Middlename Initial: ";
        cin >> cMiddleInitial;   
        cout << "Enter Lastname: ";
        cin >> cLastname;
        
        char cCode[27];
        
        // Converts name to code
        int i;
        int a;
        for(i = 0; i < 26; i++)
        {
              // firstname
              for(a = 0; a < 13; a++)
              {
              if(cLetter[i] == cFirstname[a])
              {          
              cCode[] = cEncryption[i];
              }
              }
        }
        
        // Output
        cout << "Fullname: " << cFirstname << " " << cMiddleInitial << ". " << cLastname << endl;
        cout << "Code: " << cCode << endl;
        
    	system("Pause");
    	return 0;
    }
    I need help with making an array of the code (cCode[]) by going through cLetters[] and getting each char. I tryed using For statements but i cant get it right.

    Cheer in advance.

  2. #2
    Registered User
    Join Date
    Mar 2006
    Posts
    725
    Code:
              if(cLetter[i] == cFirstname[a])
              {          
              cCode[] = cEncryption[i];
              }
              }
    You have an extra closing brace there.

    Basically, you should have it like this:
    Code:
    string str;
    string encoded_str;
    input -> str;
    
    for each char "c" in str
        for each letter "l" in cletters
            if c == l //we've found the code for this letter
                add the corresponding letter in cencryption to encoded_str
                break //optional, saves some time since we've already found what we wanted
    
    //now encoded_str holds the encoded representation of str
    Also, instead of the clumsy
    Code:
    {'f', 'o', 'o', 'b', 'a', 'r'}
    you could just use
    Code:
    "foobar"
    That is, if there are no zero characters in your array.
    Code:
    #include <stdio.h>
    
    void J(char*a){int f,i=0,c='1';for(;a[i]!='0';++i)if(i==81){
    puts(a);return;}for(;c<='9';++c){for(f=0;f<9;++f)if(a[i-i%27+i%9
    /3*3+f/3*9+f%3]==c||a[i%9+f*9]==c||a[i-i%9+f]==c)goto e;a[i]=c;J(a);a[i]
    ='0';e:;}}int main(int c,char**v){int t=0;if(c>1){for(;v[1][
    t];++t);if(t==81){J(v[1]);return 0;}}puts("sudoku [0-9]{81}");return 1;}

  3. #3
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    Code:
    char cMiddleInitial[1];
    You should probably either use a single character:
    Code:
    char cMiddleInitial;
    Or leave space for a NULL:
    Code:
    char cMiddleInitial[2];
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  4. #4
    Registered User
    Join Date
    Nov 2006
    Location
    under your bed....
    Posts
    24
    thanks jafet and dkws for your replies.

    I followed jafets outline and got it working.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 16
    Last Post: 11-23-2007, 12:48 PM
  2. help needed with edit control & encryption
    By willc0de4food in forum Windows Programming
    Replies: 2
    Last Post: 03-16-2006, 07:21 PM
  3. abt encryption algorithm
    By purIn in forum C Programming
    Replies: 9
    Last Post: 12-22-2003, 09:16 PM
  4. What's wrong with my Stream Cipher Encryption?
    By Davros in forum C++ Programming
    Replies: 3
    Last Post: 04-18-2002, 09:51 PM
  5. File Encryption & Read/Write in Binary Mode
    By kuphryn in forum C++ Programming
    Replies: 5
    Last Post: 11-30-2001, 05:45 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21