Related to my game again, i want to choose my options by typing the command. Yet, i am in a big difficulty, because switch() only accepts numbers, or it accepts one letter.
Everyone knows how switch works, i dont think i need to give an example.
Anyway, i will post the skeleton for the typing program i tried.

#include <iostream.h>
char TYPED;

int main()
{
	cout<< "Type punch, kick, or bite." <<endl;
	cin  >> TYPED;
	switch(TYPED)
	{
	case 'punch':
		cout<< "You punch him." <<endl;
	break;
	case 'kick':
		cout<< "You kick him." <<endl;
	break;
	case 'bite':
		cout<< "You bite him." <<endl;
	break;
	default:
		cout<< "I dont know how to do that." <<endl;
		cout<<TYPED<<endl;
	};
return 0;
}

Obviously, it doesnt work, or it doesnt work the way i want it to work. I tried to make an

enum attacks {null, punch, kick, bite};

But it doesnt work...

I have two options:
1) Use the switch with text like

case punch:

2) Use the switch with numbers like

case 1:

and the program must know it is number "1" because i typed "punch".

Both ways are fine for me, i just want to make it work!

You can't do what you are trying...

First, you can't put more than a single character inside single qoutes. (In any case, for switch you should just be writing:

switch (TYPED)

{

case punch:

...etc...

}

where punch is without quotes, etc.
Also, the TYPED should be defined as an int, not a char.

As I see it,you have two options:
1) abandon the switch and do
if ( TYPED == "punch" )

else if ( TYPED == "kick" )

(assuming that you define TYPED as a std::string

or :

2) make TYPED an int and make the user type a number
cout<< "Type punch (0), kick (1), or bite.(2)" <<endl;

Thank you... string. I will try it now...

#include <iostream.h>
using namespace std;
string TYPED;

int main()
{
	cout<< "Type punch, kick, or bite." <<endl;
	cin  >> TYPED;
	
	if ( TYPED == "punch" )
	cout << "You punch him." <<endl;
else 	if ( TYPED == "kick" )
	cout << "You kick him." <<endl;
else 	if ( TYPED == "bite" )
	cout << "You bite him." <<endl;
else 	cout << "I dont know how to do that." <<endl;

return 0;
}

It is working. VERY NICE IDEA! Thank you.

But let me show you a piece of code i saw in a free-source game called K-mud 1.0.

switch (path->getSrcDir())
  		{
			case NORTH     : d.addExit(mudProfile->getDirections()->north); break;
			case NORTHEAST : d.addExit(mudProfile->getDirections()->northeast); break;
			case EAST      : d.addExit(mudProfile->getDirections()->east); break;        		
			case SOUTHEAST : d.addExit(mudProfile->getDirections()->southeast);

I have no idea how it works...

NORTH, NORTHEAST, SOUTH, and SOUTHEAST are all integral values, probably members of an enum, possibly #define's. The switch is keyed on the getSrcDir() member of path, which returns one of those integral values. It's basically an abstracted version of this:

int dir;

// ...

switch (dir) {
  case 0: /* Do north */ break;
  case 1: /* Do NE     */ break;
  case 2: /* Do south */ break;
  case 3: /* Do SE     */ break;
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.