help using switch

This is a discussion on help using switch within the C++ Programming forums, part of the General Programming Boards category; I am trying to create a text game that lets the user type something, then depending on what they typed ...

  1. #1
    Registered User New001's Avatar
    Join Date
    May 2004
    Posts
    6

    help using switch

    I am trying to create a text game that lets the user type something, then depending on what they typed it would respond with a different thing. Something like this:

    cin>> x;

    switch (x)
    {
    case 'die' : cout<<"That was stupid, You lose\n";
    break;
    case 'fight' : cout<<"You chose to stay and fight\n";
    //... several more options
    default : cout<<"That is not a valid command, Try again\n";
    }

    But I can not get this to work. If i add char x; then I can get it to work, but each case can only be one letter long. Is there a way to get it so one could type in a command as a full word at cin>>x; and have it select the right case statement?

  2. #2
    mustang benny bennyandthejets's Avatar
    Join Date
    Jul 2002
    Posts
    1,401
    1. The single apostrophes are only useful when using one character. Ie, 'die' has no meaning.

    2. Placing a string within double quotes will resolve to the address of the string, so even that wont work.

    3. When you switch a pointer to char, it is actually switching the address of the char buffer, not the contents of it. So you cannot really use a switch statement. Try this:
    Code:
    if (!lstrcmpi(x,"die"))
    {
       cout<<"That was stupid, You lose\n";
    }
    else if (!lstrcmpi(x,"fight"))
    {
        cout<<"You chose to stay and fight\n";
    }
    else
    {
        cout<<"That is not a valid command, Try again\n";
    }
    4. Use code tags please.

    EDIT: I forgot to explain my code. lstrcmpi() compares the contents of the two specified char buffers and returns 0 if they are the same. It also returns other values but I wont get into that here. The code above is pretty much exactly the same as a switch statement, in terms of native code, but it's the only way to do it with char buffers.
    Last edited by bennyandthejets; 05-28-2004 at 09:26 PM.
    benforbes@optusnet.com.au
    Microsoft Visual Studio .NET 2003 Enterprise Architect
    Windows XP Pro

    Code Tags
    Programming FAQ
    Tutorials

  3. #3
    vae victus! skorman00's Avatar
    Join Date
    Nov 2003
    Posts
    594
    If you want to stick with the switch, you can have a function that will take in a string, and return a number depending on what that string is. That way the switch can test the value of that number.

  4. #4
    Registered User New001's Avatar
    Join Date
    May 2004
    Posts
    6
    Quote Originally Posted by skorman00
    If you want to stick with the switch, you can have a function that will take in a string, and return a number depending on what that string is. That way the switch can test the value of that number.
    That sounds great, but how would that be done?

  5. #5
    vae victus! skorman00's Avatar
    Join Date
    Nov 2003
    Posts
    594
    However you want it to be done. Look at what entries you will recognize, and determine the best way to differentiate them. It may be as simple as the number of letters in the string, or maybe the integral sum of the character values. Just make sure word will always give the same output, and you probably dont want different words giving the same output.

    An alternative would be to display what actions the user could take, and take in a number value instead of the word, like this:

    What do you want to do?
    1. Die
    2. Fight


    If you only have a few entries that you will recognize, the if would be better suited, but if you want many, a switch would be easier to read/follow.

  6. #6
    mustang benny bennyandthejets's Avatar
    Join Date
    Jul 2002
    Posts
    1,401
    Good idea skorman. I could imagine such a setup:
    Code:
    #define STR_DIE 1
    #define STR_FIGHT 2
    
    int GetStringIndex(char *lpString)
    {
        if (!lstrcmpi(lpString,"die"))
            return STR_DIE;
        if (!lstrcmpi(lpString,"fight"))
            return STR_FIGHT;
    
        return -1;
    }
    
    //somewhere else
    	cin >> x;
    	switch (GetStringIndex(x))
    	{
    	case STR_DIE:
    		//Do die stuff
    		break;
    	case STR_FIGHT:
    		//Do fight stuff
    		break;
    	default:
    		//Everything else
    		break;
    	}
    benforbes@optusnet.com.au
    Microsoft Visual Studio .NET 2003 Enterprise Architect
    Windows XP Pro

    Code Tags
    Programming FAQ
    Tutorials

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Data Structure Eror
    By prominababy in forum C Programming
    Replies: 3
    Last Post: 01-06-2009, 09:35 AM
  2. ascii rpg help
    By aaron11193 in forum C Programming
    Replies: 18
    Last Post: 10-29-2006, 01:45 AM
  3. Switch
    By cogeek in forum C Programming
    Replies: 4
    Last Post: 12-23-2004, 06:40 PM
  4. Switch Case
    By FromHolland in forum C++ Programming
    Replies: 7
    Last Post: 06-13-2003, 04:51 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21