Thread: You people have helped me alot...But I got another question for you...

  1. #1
    Registered User
    Join Date
    Aug 2002
    Posts
    67

    Cool You people have helped me alot...But I got another question for you...

    Okay. Thanks to my other thread I have started to use cases alot more than before. But I have yet another question. I am making a text adventure game. And use cases for the commands. But whenever the player enters an invalid command it just closes the program. So my question is. How can I make it so if a player does not use a valid entry it just returns them to the top of the case list or something simaliar? Sorry if this is confusing...But I think you get the idea. I tried else case but it wouldn't work...maybe I just didn't use it right or something. Any help here?

  2. #2
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    Code:
    switch(Whatever)
    {
       case WALK:
          *Do something*
          break;
    
       case LOOK:
          *Do something else*
          break;
    
       default:
          *Do stuff if no commands match*
          break;
    }
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  3. #3
    Registered User
    Join Date
    Aug 2002
    Posts
    67
    Thanks for the help. My game was at a standstill...

  4. #4
    Registered User
    Join Date
    Aug 2002
    Posts
    67
    Sorry for all these questions but how do I make it so if they don't enter a valid entry it just starts the case thing over again? Should I just use the case inside the default case? Sorry if that didn't make much sense...I don't explain things very well.

  5. #5
    Registered User
    Join Date
    Jun 2002
    Posts
    151
    You could try something like -

    Code:
    #include <iostream>
    #include <string>
    #include <map>
    
    using namespace std;
    
    int main()
    {
    	enum {INVALID, FIGHT, RUN};
    	
    	map <string, int> cmds;
    	cmds["fight"] = FIGHT;
    	cmds["run"] = RUN;
    	string curcmd;
    
    	cout << "Enter Command: ";
    	cin >> curcmd;
    
    	while(cmds[curcmd]==INVALID)
    	{
    		cout << "Please enter valid command: ";
    		cin >> curcmd;
    	}
    
    	switch(cmds[curcmd])
    	{
    	case FIGHT:
    		cout << "You died!!\n";break;
    	case RUN:
    		cout << "You lived to see another day\n";break;
    	default:
    		cout << "Don't know how this got through\n";
    	}
        
    	
    
    	return 0;
    }
    Build a map of valid commands and then ensure you have valid input before actioning the command.

  6. #6
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    2,718
    Code:
    bool ExitSwitch;
    do
    {
        ExitSwitch = true;
        switch(Whatever)
        {
           case WALK:
              *Do something*
              break;
           case LOOK:
              *Do something else*
              break;
           default:
              *Do stuff if no commands match*
              ExitSwitch = false;
              break;
        }
    } while(!ExitSwitch)
    Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah

    You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie

  7. #7
    Registered User
    Join Date
    Apr 2002
    Posts
    362
    skyruler54,

    Combining the code Enmeduranki supplied with XSquared's idea (along with a trivial enhancement of my own):
    Code:
    #include <iostream>
    #include <string>
    #include <map>
    #include <conio.h>  // for clrscr(); and getch();
    
    using namespace std;
    
    int main()
    {
    	enum {INVALID, FIGHT, RUN};
    
    	map <string, int> cmds;
    	cmds["fight"] = FIGHT;
    	cmds["run"] = RUN;
    	string curcmd;
    
            do {
            clrscr();
    	cout << "Enter Command: ";
    	cin >> curcmd;
    	}while(cmds[curcmd]==INVALID);
    
    
    	switch(cmds[curcmd])
    	{
    	case FIGHT: cout << "You died!!\n"; break;
    	case RUN:   cout << "You lived to see another day\n"; break;
    	default:    cout << "Don't know how this got through\n"; break;   // note inclusion of break;
    	}
    
    
            getch();  // pause program
    	return 0;
    }
    Nothing but a valid command will get through the DO...WHILE statement and, anything else input will simply clear the screen back to the prompt again.

    Enmeduranki supplies an error message which is not a bad idea from a 'user-friendly' standpoint, but I opted to exclude this feature to avoid cluttering the screen with text while the player learns to type.

    XSquared's idea is very good (which is why I included the methodology in my example ) but doesn't show that the default case element must re-prompt the player or else the execution will result in an infinite loop. (Very minor issue, but, once again, you could end up with a fair amount of useless text on the screen.)

    It's possible - probable ? - that someone will suggest (correctly) that DO...WHILE statements aren't the best idea. True enough, but in this case, that rule-of-thumb can be discounted since the player must be prompted, at least, once here.

    P.S. The 'default' case element in the switch statement (with the break; included) isn't really necessary, but is still good practice.

    -Skipper
    "When the only tool you own is a hammer, every problem begins to resemble a nail." Abraham Maslow

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. the us constitution
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 121
    Last Post: 05-28-2002, 04:22 AM
  2. people on this board have helped me so much..
    By cozman in forum Game Programming
    Replies: 0
    Last Post: 05-01-2002, 09:13 PM
  3. Language
    By nvoigt in forum A Brief History of Cprogramming.com
    Replies: 19
    Last Post: 04-29-2002, 02:28 PM
  4. Party question
    By Barjor in forum A Brief History of Cprogramming.com
    Replies: 7
    Last Post: 03-18-2002, 03:39 PM
  5. Religious Bull****
    By Witch_King in forum A Brief History of Cprogramming.com
    Replies: 119
    Last Post: 10-23-2001, 07:14 AM