Archived

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

ELS1

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

Recommended Posts

ELS1    127
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
SilentCoder    122
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]

Share this post


Link to post
Share on other sites
Guest Anonymous Poster   
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
Brandisco    122
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
a person    118
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
ncsu121978    1344
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   
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
a person    118
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