Skip to main content
gamedev.net
🔒 Locked

C++ String manipulation problem

Started by DevLiquidKnight 0 replies 100+ views
Original Post
DevLiquidKnight
DevLiquidKnight
I am currently trying to make a class file that encrypts strings I send to it using Caeser Cipher I send the following data and then I have had a problem. Currently I have a window with 2 edit boxes one edit box you type in the other outputs encrypted data or if you enter data into the output window (edit box) it decrypts the data and puts it in the other window (edit box). As you type the data is gathered from one edit window and passed into this function encrypt. For some reason it only seems to encrypt the first few letters and does not encrypt ALL the data that is typed. Could someone look this over and see if they can find any problems with it? Thank you. LPTSTR CCaeser::Encrypt(LPTSTR data) { LPSTR en; int size = sizeof(data)+1; char ch; char dat[15000]; strncpy(dat,_strrev(data),size); out.open("en.log"); for(int i = 0; i <= size; i++) { ch = Cipher(data); data[i] = ch; out << ch; } out.close(); LocalFree(dat); return en; } char CCaeser::Cipher(char ch) { char alpha[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; char ciphered[] = "TUVWXYZABCDEFGHIJKLMNOPQRS"; for(int i=0; i<=sizeof(alpha); i++) { if(alpha[i] == towupper(ch)) { return ciphered[i]; } } return ch; } Don''t click me! Killer Eagle Software
gmcbay
gmcbay
To get the string length, use strlen() NOT sizeof().

sizeof() will alwas return 4 (assuming a 32 bit OS) because LPTSTR is going to be a 32 bit value. sizeof() knows nothing of string handling... as far it is concerned you want to know the size of the pointer type, not the number of null-terminated characters exist at that memory location.


Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.