inherit
66746
0
Oct 7, 2012 14:05:38 GMT -8
«The Silver Lining»™
^ Me !!!!
1,338
December 2005
chrisrulez001
|
Post by «The Silver Lining»™ on Aug 25, 2011 5:39:25 GMT -8
Hi, I have a problem with retrieving text from a textbox, heres the code:
char getuser[50]; char getpass[50]; int i = 0;
ifstream b_file ("users.txt"); while(!b_file.eof()) { b_file >> getuser; b_file >> getpass; i++; } b_file.close(); HWND GetUsername; HWND GetPassword; char username[_MAX_PATH+1]; char password[_MAX_PATH+1];
GetUsername = GetDlgItem(hwnd, ID_USERNAME); GetWindowText(GetUsername, username, _MAX_PATH); GetPassword = GetDlgItem(hwnd, ID_PASSWORD); GetWindowText(GetPassword, password, _MAX_PATH); if(username == getuser && password == getpass) { MessageBox(NULL, "You Are Now Logged On", "Successful Logon", MB_OK); } else { MessageBox(NULL, "Invalid Username Or Password", "Unsuccessful Logon", MB_OK); }
The problem is that it if you put username to a messagebox it does show the value from the textbox but if you try and use the variable in an if statement like i have in my code it doesnt match the other variable. Whats going on?
|
|
#00AF33
14306
0
1
Sept 8, 2023 8:54:17 GMT -8
Jordan
What is truth?
11,838
October 2003
jab2
|
Post by Jordan on Aug 25, 2011 8:18:28 GMT -8
if(username == getuser && password == getpass) This is not doing what you think it is doing because all of these variables are character arrays, not string objects. The string object has overloaded the == operator so it compares all the individual characters like you would think, but a character array is not an object, it's an array of bytes. Arrays are not objects in C++ like in most high level languages used today, they are just raw data. You're actually just comparing the pointers in that if statement which are definitely not the same because they each point to a different set of data. Even if they contained the same strings, they would still be different because their addresses in memory are in different locations. What you need to use is strcmp(). www.cplusplus.com/reference/clibrary/cstring/strcmp/
|
|
inherit
66746
0
Oct 7, 2012 14:05:38 GMT -8
«The Silver Lining»™
^ Me !!!!
1,338
December 2005
chrisrulez001
|
Post by «The Silver Lining»™ on Aug 25, 2011 14:41:21 GMT -8
Ah so it would be something like this: if(strcmp(username, getuser) == 0 && strcmp(password, getpass) == 0) Thanks for your help
|
|
#00AF33
14306
0
1
Sept 8, 2023 8:54:17 GMT -8
Jordan
What is truth?
11,838
October 2003
jab2
|
Post by Jordan on Aug 25, 2011 14:47:29 GMT -8
Yup, that's it exactly. There are several other C-string functions shown on that website that you should take a look at so you are familiar with them.
|
|
inherit
66746
0
Oct 7, 2012 14:05:38 GMT -8
«The Silver Lining»™
^ Me !!!!
1,338
December 2005
chrisrulez001
|
Post by «The Silver Lining»™ on Aug 26, 2011 4:16:42 GMT -8
Ok thanks for your help
|
|
inherit
171948
0
Sept 22, 2011 7:50:11 GMT -8
kokotea
3
September 2011
kokotea
|
Post by kokotea on Sept 22, 2011 7:49:55 GMT -8
Thanks
|
|