Skip to main content
GameDev.net gamedev.net
🔒 Locked

strange strcmp problem

Started by Prog101 Jun 16, 2007 at 10:22 AM 4 replies 750+ views
Original Post
Prog101
Prog101
i have two arrays one holds 97 names (pAIDataFile->pAIData->pAIDriverData.DriverName) and oone has 33 names (pAIDriversThisSeason->pAIData->pAIDriverThisRace[j].DriverName) whhat i m doing is trying to compare the names so i can pass the ID of the name. But when i do my loop the strcmp does not seem to be working it keeps treating the if statement as true and going into the if statement, i have checked the names and they are always changing and should not be true ? by the way the .DriverName is = char DriverName[32]; the code is in C

char *name1;
char *name2;

	for(i=0;i<pAIDataFile->pAIData->nbAIDrivers;i++)
	{
		name1 = pAIDataFile->pAIData->pAIDriverData.DriverName;
		for(j=0;j<pAIDriversThisSeason->pAIData->nbAIDrivers;++j)
		{
			name2 = pAIDriversThisSeason->pAIData->pAIDriverThisRace[j].DriverName;	
			if(strcmp(name1,name2)==0) ;
			{
				pAIDriversThisSeason->pAIData->pAIDriverThisRace[j].DriverID = i;
			}
		}
	}

Prog101
Prog101
sorry
if(strcmp(name1,name2)==0) ;

should be
if(strcmp(name1,name2)==0)
wack
wack
I'm not sure what you want, but that code will always find at the very least name2 on the same driver as name1, because the inner loop loops over the same data.
Zahlman
Zahlman
Quote:
Original post by Prog101
sorry
if(strcmp(name1,name2)==0) ;

should be
if(strcmp(name1,name2)==0)


Yep, that would do it.

That said:

Data* AI = pAIDataFile->pAIData;for (i = 0; i < AI->nbAIDrivers; ++i){	char* name1 = AI->pAIDriverData.DriverName;	Data* TS = pAIDriversThisSeason->pAIData;	Driver* drivers = TS->pAIDriverThisRace;	for (j = 0; j < TS->nbAIDrivers; ++j)	{		char* name2 = drivers[j].DriverName;			if (!strcmp(name1,name2))		{			drivers[j].DriverID = i;		}	}}


Also, you really should reconsider your naming conventions. (And while I hate to start a flamewar about bracing, K&R style would have made the bug more obvious, by putting the spurious semicolon right next to an open brace.)

Also, any particular reason for not using C++?
Jerax
Jerax
Turn up the warning level on your compiler - most will warn about empty controlled statements.

Topic Locked

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

Sign in to reply to this topic.