| View previous topic :: View next topic |
| Author |
Message |
Stylo Grandmaster Cheater Supreme
Reputation: 3
Joined: 16 May 2007 Posts: 1073 Location: Israel
|
Posted: Thu Oct 23, 2008 2:06 pm Post subject: [C++] having problem with my code |
|
|
i got this code
| Code: |
wchar_t *szEnteredPass = new wchar_t[MAX_PATH];
wchar_t *szRealPass = new wchar_t[MAX_PATH];
szRealPass = _T("password");
GetDlgItemText(hWnd,IDC_PASS_EDIT,szEnteredPass,MAX_PATH);
if (strcmp((char*)szRealPass,(char*)szEnteredPass) == 0){
MessageBox(0,0,0,0);
}
|
so when i enter "password" into textbox it pops up the message
so far it's good
but when i enter "passwordddd" it still pops up the messagebox!?!?
how come??
_________________
Stylo |
|
| Back to top |
|
 |
lurc Grandmaster Cheater Supreme
Reputation: 2
Joined: 13 Nov 2006 Posts: 1900
|
Posted: Thu Oct 23, 2008 2:27 pm Post subject: |
|
|
1. Use TCHAR routines.
2. strcmp is ANSI
3. Use lstrcmpi for non-case sensitivity if you'd like that.
| Code: | LPTSTR lpRealPass = new TCHAR[260];
LPTSTR lpPass = new TCHAR[260];
lstrcpy(lpRealPass, _T("Password"));
if (GetDlgItemText(hWnd, IDC_PASS_EDIT, lpPass, 260) > 0)
if (!lstrcmp(lpPass, lpRealPass))
MessageBox(hWnd, _T("Correct."), 0, 0);
delete[] lpRealPass;
delete[] lpPass; |
Btw, if your using dynamic memory, use it to your advantage and only allocate enough memory required for the data your storing.
_________________
Last edited by lurc on Thu Oct 23, 2008 2:31 pm; edited 1 time in total |
|
| Back to top |
|
 |
--Pillboi-- Grandmaster Cheater Supreme
Reputation: 0
Joined: 06 Mar 2007 Posts: 1383 Location: I don't understand the question. Is this a 1 to 10 thing?
|
Posted: Thu Oct 23, 2008 2:30 pm Post subject: |
|
|
| Quote: | | This function starts comparing the first character of each string. If they are equal to each other, it continues with the following pairs until the characters differ or until a terminanting null-character is reached. |
Due to a null terminator being reached in one of the 2 strings, it stops checking the rest.
An easy way to stop this is to also check if both strings are the same length.
| Code: | if(strlen(password)==strlen(enteredpassword) && strcmp(password,enteredpassword)==0){
Password is correct
} else {
Password is incorrect
} |
_________________
Enter darkness, leave the light, Here be nightmare, here be fright...
Earth and Water, Fire and Air. Prepare to meet a creature rare.
Enter now if you dare, Enter now the dragon's lair. |
|
| Back to top |
|
 |
Stylo Grandmaster Cheater Supreme
Reputation: 3
Joined: 16 May 2007 Posts: 1073 Location: Israel
|
|
| Back to top |
|
 |
--Pillboi-- Grandmaster Cheater Supreme
Reputation: 0
Joined: 06 Mar 2007 Posts: 1383 Location: I don't understand the question. Is this a 1 to 10 thing?
|
Posted: Thu Oct 23, 2008 2:45 pm Post subject: |
|
|
It's how I do it, but I code in C.
_________________
Enter darkness, leave the light, Here be nightmare, here be fright...
Earth and Water, Fire and Air. Prepare to meet a creature rare.
Enter now if you dare, Enter now the dragon's lair. |
|
| Back to top |
|
 |
hcavolsdsadgadsg I'm a spammer
Reputation: 26
Joined: 11 Jun 2007 Posts: 5801
|
Posted: Thu Oct 23, 2008 6:06 pm Post subject: |
|
|
| --Pillboi-- wrote: | | Quote: | | This function starts comparing the first character of each string. If they are equal to each other, it continues with the following pairs until the characters differ or until a terminanting null-character is reached. |
Due to a null terminator being reached in one of the 2 strings, it stops checking the rest.
An easy way to stop this is to also check if both strings are the same length.
| Code: | if(strlen(password)==strlen(enteredpassword) && strcmp(password,enteredpassword)==0){
Password is correct
} else {
Password is incorrect
} |
|
what.
Checking the length if you're going to do that is pointless
|
|
| Back to top |
|
 |
smartz993 I post too much
Reputation: 2
Joined: 20 Jun 2006 Posts: 2013 Location: USA
|
Posted: Thu Oct 23, 2008 6:26 pm Post subject: |
|
|
| slovach wrote: | | --Pillboi-- wrote: | | Quote: | | This function starts comparing the first character of each string. If they are equal to each other, it continues with the following pairs until the characters differ or until a terminanting null-character is reached. |
Due to a null terminator being reached in one of the 2 strings, it stops checking the rest.
An easy way to stop this is to also check if both strings are the same length.
| Code: | if(strlen(password)==strlen(enteredpassword) && strcmp(password,enteredpassword)==0){
Password is correct
} else {
Password is incorrect
} |
|
what.
Checking the length if you're going to do that is pointless |
Why don't you read?
strcmp - compares each character until a difference is found or until a terminanting null-character is reached.
This means that password and passwordddd would return true, because a terminating null-character is reached in the first string. Checking length also, will fix this problem.
|
|
| Back to top |
|
 |
manc Grandmaster Cheater
Reputation: 1
Joined: 16 Jun 2006 Posts: 551
|
Posted: Thu Oct 23, 2008 8:39 pm Post subject: |
|
|
Good luck on your program that happens to crack pins for MapleStory
_________________
|
|
| Back to top |
|
 |
hcavolsdsadgadsg I'm a spammer
Reputation: 26
Joined: 11 Jun 2007 Posts: 5801
|
Posted: Thu Oct 23, 2008 9:23 pm Post subject: |
|
|
| smartz993 wrote: | Why don't you read?
strcmp - compares each character until a difference is found or until a terminanting null-character is reached.
This means that password and passwordddd would return true, because a terminating null-character is reached in the first string. Checking length also, will fix this problem. |
you wish, you're misunderstanding, it doesn't just stop.
If the first string is shorter than the second, you'll get a negative return. Otherwise, if it's longer, you'll get a positive. If it returns zero, they match.
|
|
| Back to top |
|
 |
nog_lorp Grandmaster Cheater
Reputation: 0
Joined: 26 Feb 2006 Posts: 743
|
Posted: Thu Oct 23, 2008 9:53 pm Post subject: |
|
|
Slovach is correct. Others were correct to a degree: it reaches a null early, but in BOTH strings, because characters within the ASCII set translate to UNICODE as the same byte followed by a 0 byte. So the ASCII strcmp routine only tests the first character.
_________________
Mutilated lips give a kiss on the wrist of the worm-like tips of tentacles expanding in my mind
I'm fine accepting only fresh brine you can get another drop of this yeah you wish |
|
| Back to top |
|
 |
Stylo Grandmaster Cheater Supreme
Reputation: 3
Joined: 16 May 2007 Posts: 1073 Location: Israel
|
|
| Back to top |
|
 |
lurc Grandmaster Cheater Supreme
Reputation: 2
Joined: 13 Nov 2006 Posts: 1900
|
Posted: Fri Oct 24, 2008 1:18 pm Post subject: |
|
|
Did you check the last error using WSAGetLastError?
_________________
|
|
| Back to top |
|
 |
Stylo Grandmaster Cheater Supreme
Reputation: 3
Joined: 16 May 2007 Posts: 1073 Location: Israel
|
|
| Back to top |
|
 |
|