Help with Simple encryption using c-style strings

This is a discussion on Help with Simple encryption using c-style strings within the C++ Programming forums, part of the General Programming Boards category; I have been struggling with this piece of code for a few days now, i'm new to c++ and wanted ...

  1. #1
    Registered User
    Join Date
    Feb 2006
    Posts
    1

    Help with Simple encryption using c-style strings

    I have been struggling with this piece of code for a few days now, i'm new to c++ and wanted to master this bit.

    Here is the entire code. Ive marked where the code is missing.

    Any ideas or suggestions are greatly appreciated...thanks...

    Code:
    #include <iostream>
    
    using namespace std;
    using namespace std;
    
    const int alphaLen = 'Z'-'A'+1;
    
    char upper( char c )
    {
    	if ( c>='a' && c<='z' )
    		c=c+'A'-'a';
    
    	return c;
    }
    
    
    /*
      cipherGenerate implicitly returns the generated cipher alphabet via
      char cipherAlphabet [] parameter.
    
      The function can assume that the associated cipherAlphabet array
      is sufficently dimensioned to correctly store the cipher alphabet.
    
    
    */
    
    void cipherGenerate(const char secretphrase[], char cipherAlphabet[])
    {
      
      cipherAlphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
      char str[20]; //not sure how to get length of cipherAlphabet and secretphrase //together?
    
      strcat(str, secretphrase);
      strcat(str, cipherAlphabet);
      puts(str);
      cipherAlphabet = str;
    
      for (int i=0; i < strlen(cipherAlphabet); i++) {
        for(int j=0; j < strlen(cipherAlphabet); j++) {
          if(cipherAlphabet[i]=cipherAlphabet[j]) 
      }
        }
    
      // MISSING CODE
    
    } //End of function cipherGenerate
    
    /*
      encryptMessage implicitly returns the encryted message via
      char encryptedMessage [] parameter.
    
      The function can assume that the associated encryptedMessage array
      is sufficently dimensioned to correctly store the encrypted message.
    
    */
    
    void encryptMessage(const char secretPhrase[],
                        const char message[],
                        char encryptedMessage[] )
    {
      char cipherAlphabet[alphaLen];
      cipherGenerate(secretPhrase, cipherAlphabet);
    
    
      // MISSING CODE
    
    } // end of function encryptMessage
    
    
    /*
      decryptMessage implicitly returns the decryted message via
      char decryptedMessage [] parameter.
    
      The function can assume that the associated decryptedMessage array
      is sufficently dimensioned to correctly store the decrypted message.
    
    */
    
    void decryptMessage(const char secretPhrase[],
                        const char encryptedMessage[],
                        char decryptedMessage[] )
    {
      char cipherAlphabet[alphaLen];
      cipherGenerate(secretPhrase, cipherAlphabet);
    
      //MISSING CODE
    
    } // end function decryptMessage
    
    
    int main(int argc, char * argv[])
    {
    	char cipherAlphabet[alphaLen+1];
    
    	if ( argc<3 )
    	{
    		cout << argv[0] << " \"secret phrase\" \"message\"" << endl;
    		return 1;
    	}
    
     	cipherGenerate(argv[1], cipherAlphabet);
    	cipherAlphabet[alphaLen] = '\0';
    
    	cout << "The cipher alphabet is -" << cipherAlphabet << endl;
    
    	char * encryptedMessage = new char[strlen(argv[2])+1];
    	char * decryptedMessage = new char[strlen(argv[2])+1];
    	encryptMessage(argv[1], argv[2], encryptedMessage );
    
    	cout << "The encrypted message is -" << encryptedMessage<<endl;
    
    	decryptMessage(argv[1], encryptedMessage, decryptedMessage );
    	cout << "The decrypted message is -" << decryptedMessage<<endl;
    
    	delete encryptedMessage;
    }

  2. #2
    System Novice siavoshkc's Avatar
    Join Date
    Jan 2006
    Location
    Tehran
    Posts
    1,236
    I read the code and I was a little confused. Anyway there were some lines that seemed as error to me:

    The line
    Code:
    cipherAlphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
    is wrong. cipherAlphabet is not a modifiable lvalue. You can initialize it in main(), where it is declared or make it local to cipherGenerate().

    The line
    Code:
    cipherAlphabet = str;
    is wrong. cipherAlphabet is not a modifiable lvalue. Use strcpy().

    The line
    Code:
    if(cipherAlphabet[i]=cipherAlphabet[j])
    is wrong. Expected == . Although the first is legal it is an assignment, not a comparison.

    What is this? What do you expect it to do?
    Code:
    for (int i=0; i < strlen(cipherAlphabet); i++) {
        for(int j=0; j < strlen(cipherAlphabet); j++) {
          if(cipherAlphabet[i]=cipherAlphabet[j]) 
        }
    }
    What do you expect the missing codes to do?
    What do you expect your program to do?
    Learn C++ (C++ Books, C Books, FAQ, Forum Search)
    Code painter latest version on sourceforge DOWNLOAD NOW!
    Download FSB Data Integrity Tester.
    Siavosh K C

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. BN_CLICKED, change button style
    By bennyandthejets in forum Windows Programming
    Replies: 13
    Last Post: 07-06-2010, 12:42 AM
  2. simple question about strings
    By panfilero in forum C Programming
    Replies: 2
    Last Post: 11-03-2005, 01:57 AM
  3. Converting C++ Style strings...
    By LightsOut06 in forum C++ Programming
    Replies: 10
    Last Post: 10-26-2005, 04:02 PM
  4. Simple RPG.h newbie style
    By curlious in forum C++ Programming
    Replies: 5
    Last Post: 07-22-2003, 08:08 AM
  5. converting c style strings to c++ strings
    By fbplayr78 in forum C++ Programming
    Replies: 6
    Last Post: 04-14-2003, 04:13 AM

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