code isnt doing anything??

This is a discussion on code isnt doing anything?? within the C++ Programming forums, part of the General Programming Boards category; Code: string encryptKey(string key) { string password; int passwordlength; int keylength = key.length(); string encryptedKey; cin >> password; //executed passwordlength ...

  1. #1
    Registered User
    Join Date
    Mar 2006
    Posts
    4

    code isnt doing anything??

    Code:
    string encryptKey(string key)
    {
    	string password;
    	int passwordlength;
    	int keylength = key.length();
    	string encryptedKey;
    	cin >> password; //executed
    	passwordlength = password.length();
    	cin >> password; //executed
            for(int x = 0;x<keylength;x++) 
            {
    	   for(int o = 0;o<passwordlength;o++)
                  {
    			    encryptedKey[x]=key[x]^password[o];
    			    cout << encryptedKey[x];
    			    cin >> password;
    	      }	  
                 cout << encryptedKey[x];
    	     cin >> password;  
            }
    	    
    
        cout << key;
        cout << passwordlength; //executed
        cout << encryptedKey;
        cin >> password; //executed
    	return encryptedKey;
    }
    so this function is supposed to take a key passed to it as a parameter, ask for a password, encrypt the key with the password using that XOR thing and then return the encrypted key. I put in a bunch of extra couts and cins to try to figure out what the program is doing, because its not doing what its supposed to. When I run the program it asks for input twice, outputs the passwordlength, asks for input again and then finishes. the string that it returns doesnt contain anything inside of it either


    the output looks kinda like this:
    Code:
    asdf  (asks for input)
    sd    (asks for input again)
    4    (displays the length of password)
    df    (asks for input)
    so why doesnt it go through the for loops and actually encrypt the password
    Last edited by Saddamizer; 05-21-2006 at 01:35 PM.

  2. #2
    The Richness... Richie T's Avatar
    Join Date
    Jan 2006
    Location
    Ireland
    Posts
    469
    after a little tinkering, i've discovered that you are getting a
    bounds error - the program crashes on this line:

    encryptedKey[x]=key[x]^password[o];

    why? encryptedKey[x] is out of bounds because you just created
    a string and didn't give it any information about how big it needs
    to be - it doesn't expect to be asked to resize itself in this
    manner. it becomes out of bounds when x = 1.
    No No's:
    fflush (stdin); gets (); void main ();


    Goodies:
    Example of fgets (); The FAQ, C/C++ Reference


    My Gear:
    OS - Windows XP
    IDE - MS Visual C++ 2008 Express Edition


    ASCII stupid question, get a stupid ANSI

  3. #3
    Registered User
    Join Date
    Mar 2006
    Posts
    4
    alright i guess that makes sense, but how do i define its bounds?
    Code:
    	string encryptedKey[keylength];
    when i do that, it wont let the funtion return that string. dev-cpp gives me this error:
    88 C:\Dev-Cpp\My C++ Work\Encryption.cpp conversion from `std::string*' to non-scalar type `std::string' requested

  4. #4
    Registered User
    Join Date
    Jun 2005
    Posts
    6,208
    Look up the resize() method of the string class. Call it before entering the loop.

  5. #5
    Registered User
    Join Date
    Mar 2006
    Posts
    4
    thanks grumpy and richie, i think its been fixed now

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Extended ASCII Characters in an RTF Control
    By JustMax in forum C Programming
    Replies: 18
    Last Post: 04-03-2009, 08:20 PM
  2. Enforcing Machine Code Restrictions?
    By SMurf in forum Tech Board
    Replies: 21
    Last Post: 03-30-2009, 07:34 AM
  3. Obfuscated Code Contest
    By Stack Overflow in forum Contests Board
    Replies: 51
    Last Post: 01-21-2005, 03:17 PM
  4. Interface Question
    By smog890 in forum C Programming
    Replies: 11
    Last Post: 06-03-2002, 05:06 PM
  5. Replies: 0
    Last Post: 02-21-2002, 05:05 PM

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