Thread: Writing past the end of a string

  1. #1
    Registered User Nippashish's Avatar
    Join Date
    Sep 2002
    Posts
    34

    Writing past the end of a string

    I've been playing around with encrypting a string, and I've got it working great except for one thing. If the values entered are larger than the variables have room for then the program messes up. Obviously this is bad, so I need some way to truncate the strings that are input so that they fit in the space I've allocated.

    Here's what I've got right now (it's not that long so I'll just post the whole thing):
    Code:
    #include <iostream>
    #include <conio.h>
    
    using namespace std;
    
    #define STRLEN 256
    #define CODELEN 10
    
    int main()
    {
    	int j,i,codeLen;	// j and i are for the for loops, codeLen is the length of the encryption code
    	char str[STRLEN];	// stores the string to be encrypted
    	char code[CODELEN];	// stores the code
    	for (;;)
    	{
    		cout << "Enter a string to be encrypted (exit to quit) -> ";
    		cin.getline(str, STRLEN);
    
    		if (!strcmp(str, "exit"))
    			break;
    
    		cout << "Enter an enctyption code (up to " << CODELEN << " characters) -> ";
    		cin.getline(code, CODELEN);
    
    		for (i=0;str[i] != '\0';i++)
    		{
    			for (j=0;code[j] != '\0';j++)
    			{
    				str[i] ^= code[j];
    			}
    			if (str[i] == '\0')
    				str[i] = (char)255;
    		}
    
    		codeLen = j-1;
    		cout << "Your string encrypted is: " << str << endl;
    
    		for (i=0;str[i] != '\0';i++)
    		{
    			if (str[i] == (char)255)
    				str[i] = '\0';
    			for (j=codeLen;code[j] >= 0;j--)
    			{
    				str[i] ^= code[j];
    			}
    		}
    
    		cout << "And then decrypted again: " << str << endl;
    		cout << endl;
    	}
    	cout << endl << "Press any key to continue...";
    	getch();
    	return 0;
    }
    Now I thought that cin.getline() was supposed to limit the length of the input, but as far as I can tell I was wrong, because it really doesn't seem to be working.

  2. #2
    Registered User
    Join Date
    Nov 2001
    Posts
    1,348
    One solution is a string object. Here is an example.

    Code:
    string input;
    
    std::getline(cin, input);
    Kuphryn

  3. #3
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    No gripes about using the string class. That is what it is there for. But I would advise you do fix your code to cope with the problem by making a larger code string or by padding the input string with zeros. I know you are just playing around with the encryption stuff now, but a 10 byte buffer is far to small to make a sufficiently encrypted file. You should make the code buffer at least 4k.

  4. #4
    Registered User Nippashish's Avatar
    Join Date
    Sep 2002
    Posts
    34
    Well, I wasn't able to get the string object to work properly, so I figured I'd deal with that later and move on to something else. (I tend to do that alot). Anyway, now I've run into a different problem, and I can't for the live of me figure out what's going wrong. I've moved from encrypting strings of characters to trying to encrypt files, and I've got it so that it works some of the time. Sometimes it works, I encrypt the file and it decrypts fine, but sometimes, (depending on the key I use), I can only decrypt part of the file.

    I think I'm getting a certain character form the XOR's that is stoping the file from being decrypted any further, because if (for example) I use my name (misha) as the key, I can decrypt anything before, but not including the first lowercase d in the original file. That makes me think that I'm puting a character into the file that I shouldn't be (Like an EOF or something). But then the encrypted file displays fine in notepad (it's all mimbo jumbo but it displays the exact same number of characters as the original), so I really don't know what it is. (I'll post the code if you need it, but I don't want to throw it all up here if I don't have too).

  5. #5
    Registered User
    Join Date
    Nov 2001
    Posts
    1,348
    Make sure you read and write data in binary mode.

    Kuphryn

  6. #6
    Registered User Nippashish's Avatar
    Join Date
    Sep 2002
    Posts
    34
    Right on! Thanks! All my problems are solved (Well, that one is anyway )

    *goes off to try and implement the string class insted of char arrays*

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. copy a string in the end of another string!?
    By hyaku_ in forum C Programming
    Replies: 9
    Last Post: 11-29-2006, 06:22 AM
  2. Compile Error that i dont understand
    By bobthebullet990 in forum C++ Programming
    Replies: 5
    Last Post: 05-05-2006, 09:19 AM
  3. Sizeof - Writing past the end of an array
    By milkydoo in forum C++ Programming
    Replies: 2
    Last Post: 07-15-2003, 02:20 AM
  4. Next Question...
    By Azmeos in forum C++ Programming
    Replies: 3
    Last Post: 06-06-2003, 02:40 PM
  5. Something is wrong with this menu...
    By DarkViper in forum Windows Programming
    Replies: 2
    Last Post: 12-14-2002, 11:06 PM