strcmp with ""

Started by
4 comments, last by shadowman13131 22 years, 5 months ago
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...
Advertisement
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-
-Vissing-
I didn''t really read your code, but here''s a couple tips it sounds like you could use:
  • To set a string as empty, set the first character to NULL.
  • To test if an initialized string is empty, test if the first character is NULL.


    [Resist Windows XP''s Invasive Production Activation Technology!]
  • Hey...
    So the code would sorta be like this:

    char* playername;
    playername = new char[20];
    playername[0] = NULL;

    and then to test...

    if (playername[0] == NULL)
    {
    //do stuff
    }

    like that?
    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.
    Yeah, that''s a fair warning about the string functions. You can take a look at this for some more in depth information

    [Resist Windows XP''s Invasive Production Activation Technology!]

    This topic is closed to new replies.

    Advertisement