Archived

This topic is now archived and is closed to further replies.

shadowman13131

strcmp with ""

Recommended Posts

Hey, I''m making a game right now (online only) and I''m having a problem with getting new user data. You see, when a user logs in, it checks if the player''s screen name exists. If it doesn''t, then it fills in the new screen name data. If it does exist, then it sends the player''s screen name to the player. However, I''ve found that it doesn''t really work... Anyone know why? The code is this, where companynames is an array of player screen names, the infotag in gMsg is the player number, and gMsg->data is an array of unsigned chars which contains the sn the player is sending: case GAME_MSGID_SETPLAYERSN: { if (strcmp(companynames[gMsg->InfoTag],"") == 0) //strcmp(companynames[gMsg->InfoTag],"") == 0 { char compare[20]; strcpy(compare,(char*)gMsg->data); bool similarname = false; for (int x = 0; x < 100; x++) { if (strcmp(companynames[x],compare) == 0) { similarname = true; } } if (similarname) { GAMEMSG_GENERAL sendinfo; sendinfo.dwInfoType = GAME_MSGID_ERRORINLOGIN; char msg[300]; strcpy(msg, "Company name already used!"); strcpy((char*)sendinfo.data,msg); WPlay.SSend(pPlayerInfo->dpnidPlayer,sendinfo); } else { strcpy(companynames[gMsg->InfoTag],compare); } } else { /* char testchars[20]; BYTE* test; test = (BYTE*)&testchars; for (int x = 0; x < sizeof(testchars); x++) { test[x] = gMsg->data[x]; } if (strcmp(testchars, companynames[gMsg->InfoTag]) != 0) { GAMEMSG_GENERAL sendinfo; sendinfo.dwInfoType = GAME_MSGID_ERRORINLOGIN; char msg[300]; strcpy(msg, "Company names do not match.. Invalid login!"); strcpy((char*)sendinfo.data,msg); WPlay.SSend(pPlayerInfo->dpnidPlayer,sendinfo); }*/ } float compone; compone = sizeof(companynames[0]); compone = 300/compone; compone = (float)floor(compone); int compsend; compsend = (int)compone; for (int y = 0; y < 100; y += compsend) { GAMEMSG_GENERAL sendinfo; for (int xyx = 0; xyx < compsend && xyx+y < 100; xyx++) { BYTE* test = (BYTE*)&companynames[xyx+y]; for (int xyz = 0; xyz < sizeof(companynames[xyx+y]); xyz++) { sendinfo.data[xyz+(int)(xyx*sizeof(companynames[xyx+y]))] = test[xyz]; } } sendinfo.InfoTag = y; sendinfo.dwInfoType = GAME_MSGID_SENDSNS; WPlay.SSendToAll(sendinfo); } break; } Hope you can help...

Share this post


Link to post
Share on other sites
Hi, one idea is to test the lenght of your string instead of comparing your string with and empty string.

try using strlen() instead.

happy coding

-vissing-

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Also, be careful with strcmp in server programs. You should not use strcmp (or strcpy, for that matter) unless you have already verified that the size of your string is smaller than your buffer. It''s far better to use strncmp and strncpy if possible.

Share this post


Link to post
Share on other sites