hmm...why cant i detect it? (strings)

Started by
7 comments, last by ELS1 22 years, 1 month ago
this is what i have:
  
void ProcessCommand(HWND ConsoleScreen, HWND EditBox) {		
//	ConsoleString CommandString ;


	int CommandLength = 0 ;
	char* Command = 0 ;
	int EditBoxLength = GetWindowTextLength(EditBox) + 1 ;
	int ConsoleScreenLength = GetWindowTextLength(ConsoleScreen) + 1 ;

	char* NewEditBoxString = new char[EditBoxLength] ;

	GetWindowText(EditBox, NewEditBoxString, EditBoxLength) ;
	SendMessage(EditBox, WM_SETTEXT, 0, (LPARAM)"") ;

	if(NewEditBoxString == "/commands") {
		AddConsoleText(ConsoleScreen, "commands found") ;
	}else {
		AddConsoleText(ConsoleScreen, "Invalid Command!") ;
	}

	delete []NewEditBoxString ;
}
  
for some reason, everytime i process a command, i get "Invalid COmmand". Ive been tinkering with it for a while..but i cant figure it out! any help?
Advertisement
You can't use == to compare char arrays. You could use strcmp.

I would suggest using std::string instead. It would make this kind of stuff a whole lot easier.

[edited by - SilentCoder on March 20, 2002 8:59:41 PM]
quote:Original post by SilentCoder
You can''t use == to compare char arrays. You could use strcmp.


He''s right

_____________________________________________________

ICQ #: 149510932
Google - OpenGL - DirectX - Windows Guide Network - MSDN - Symantec Virus Info
"Imagination is more important than knowledge." - Albert Einstein

quote: quote:
--------------------------------------------------------------------------------
Original post by SilentCoder
You can''t use == to compare char arrays. You could use strcmp.

--------------------------------------------------------------------------------



He''s right


They''re right
quote:quote:
--------------------------------------------------------------------------------
quote:
--------------------------------------------------------------------------------
Original post by SilentCoder
You can''t use == to compare char arrays. You could use strcmp.

--------------------------------------------------------------------------------



He''s right
--------------------------------------------------------------------------------



They''re right




He''s right
actually, you would not use strcmp, but instead use strstr and make sure that the return value it equal to the address of the command string. otherwise you wont be allowed to have args for your commands, and that severly limits the usefulness of a console.

example:

  // PSUEDEO codechar *cmdList[] =    {"/help",    "/exit",    "/specialCommand3",    NULL}; // null terminator for list, so we dont care about           // the length of the listint CheckForValidCmd(char *cmdStringEntered){   // num_cmds is the number of commands in the array   // cmdList   for(i=0; ; i++)   {      if(cmdList[i]==NULL)          break; // end of list      if(cmdStringEntered == strstr(cmdStringEntered, cmdList[i]))         return i; // command is found   }   return -1; // error, not found}// actually handling of the command can be done// via a switch statment  
quote:
quote:
--------------------------------------------------------------------------------
quote:
--------------------------------------------------------------------------------
quote:
--------------------------------------------------------------------------------
Original post by SilentCoder
You can''t use == to compare char arrays. You could use strcmp.

--------------------------------------------------------------------------------



He''s right
--------------------------------------------------------------------------------



They''re right

--------------------------------------------------------------------------------





He''s right

This guy knows what he is talking about.


"I pity the fool, thug, or soul who tries to take over the world, then goes home crying to his momma."
- Mr. T
quote:Original post by a person
actually, you would not use strcmp, but instead use strstr and make sure that the return value it equal to the address of the command string. otherwise you wont be allowed to have args for your commands, and that severly limits the usefulness of a console.


It would make a lot more sense to tokenize with strtok to get the first word as the command, then strcmp. Or perhaps even strncmp w/o tokenizing. Strstr''s doing more work than is required here.
well a quote from the linux man page:
(http://www.mkssoftware.com/docs/man3/strtok.3.asp in case you dont got linux to read the page)
quote:
Never use this function. This function modifies its first
argument. The identity of the delimiting character is
lost. This function cannot be used on constant strings.


therefore its not going to work in all cases. its nice and all, but it is not a very good function. once you use the function, your orginal string is no longer usable unless you do some work (it writes NULLS to the string as it tokenizes it). i personally parse my own strings, and handle finding parms myself. i am pretty sure that strtok cant handle quoted text corretcly (ie text in quotes is a single string).
i still think that my method is easier, more straight forward, and safer. though your method is VERY good if you dont care about the original string at all.

This topic is closed to new replies.

Advertisement