Page 1 of 1

[C++] Multi Say System (Console)

Posted: Mon Jul 02, 2012 1:30 pm
by vitinho444
Hello guys, im making a little project on my own just for fun and i would like to ask you guys if you know how to make a system or a function that can detect multiple commands at once.

I shall make an example:

My project is on command line, and its checking for commands all the time, and i got commands like: Display.All.Ages, and it display ages of a class called bots. But i would like to make like: Display.Ages.1 and it only display the age of the Bot number 1.

Here's how im detecting commands:
First i store them into chars

Code: Select all

///Commands
char Command_Hello[256] = "hello";
char Command_Stop[256] = "stop";
char Command_Clear[256] = "cls";
char Command_ShowAge[256] = "display.ages";
char Command_ShowGender[256] = "display.genders";
Then in the update i got:

Code: Select all

cin >> Command; //check for command
	if(strcmp(Command, Command_Hello) == 0)
	{
		cout << "Hello to you sir" << endl;
		Blank();
	}
	else if(strcmp(Command, Command_Stop) == 0)
	{
		cout << "Bye Bye" << endl;
		Blank();
		Running = false;
	}
	else if(strcmp(Command, Command_Clear) == 0)
	{
		system("cls");
		Blank();
	}
	else if(strcmp(Command, Command_ShowAge) == 0)
	{
		for(int i = 0; i < 30; i ++)
		{
			cout << "Bot #" << i << " Age: " << bot.age[i] << " years" << endl;
		}
		Blank();
	}
	else if(strcmp(Command, Command_ShowGender) == 0)
	{
		for(int i = 0; i < 31; i ++)
		{
			if(bot.gender[i] == 0)		cout << "Bot #" << i << " Gender: Female" << endl;
			if(bot.gender[i] == 1)		cout << "Bot #" << i << " Gender: Male" << endl;
		}
		Blank();
	}
If you dont know, strcmp will compare the Command to the prebuilt commands and check if they are the same, if yes it returns 0, and the function above is called.

I dont know how to this.. If you guys know tell me please :)

Re: [C++] Multi Say System (Console)

Posted: Mon Jul 02, 2012 7:13 pm
by Jackolantern
You are going to need some more complex string functionality than just checking if a word is in the string. Basically, you need to be able to split (or explode, if you are familiar with PHP) the strings on a certain character; period in your case. To my amazement, a Google search didn't turn up a library function for doing this. However, here is a forum post that describes making your own function for this purpose. Boost C++ Library also has some better string manipulation functions. So you will need to split the string into an array on the periods, so that you can do more complex logic based on the input command. You could basically just make some nested IF statements that perform different things as you move through each element of the command string array.