Archived

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

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

This topic is 5744 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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?

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
quote:
quote:
--------------------------------------------------------------------------------
Original post by SilentCoder
You can''t use == to compare char arrays. You could use strcmp.

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



He''s right


They''re right

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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 code

char *cmdList[] =
{"/help",
"/exit",
"/specialCommand3",
NULL}; // null terminator for list, so we dont care about

// the length of the list



int 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


Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites