home *** CD-ROM | disk | FTP | other *** search
- Path: sparky!uunet!gatech!concert!borg!beethoven!bell
- From: bell@beethoven.cs.unc.edu (Andrew Bell)
- Newsgroups: rec.games.programmer
- Subject: Re: Goto [variable] in C++ ?
- Message-ID: <18566@borg.cs.unc.edu>
- Date: 30 Dec 92 08:01:08 GMT
- References: <725647749.AA00000@therose.pdx.com>
- Sender: news@cs.unc.edu
- Organization: The University of North Carolina at Chapel Hill
- Lines: 44
-
- kinsman@jupiter.sun.csd.unb.ca (Aphoriel/Kinsman) writes:
-
- >This is a question from someone new to C...
-
- >How could I set up C code so that a program goes to a function where the
- >name of the function is placed in a string variable?
-
- You can't, but why bother?
-
- >The specific use I'm thinking of is for an adventure game, where the
- >function the parser calls depends on the verb the player used in typing
- >in his command..
-
- So set up a table and a function:
-
- struct CommandEntry {
- char *command;
- int (* function)();
- } commandTable[] = {
- {"eat",eatFunction},
- {"give",giveFunction}
- [...]
- };
-
- #define NUMCOMMANDS (sizeof(commandTable))/(sizeof(struct commandEntry))
-
- void
- callCommand(char *command)
- {
- /* Go through the table until we find a match */
- for ( int commandnum = 0 ; commandnum < NUMCOMMANDS ; commandnum++ )
- /* If we find one... */
- if ( !strcmp(command,commandTable[commandnum].command) )
- {
- /* run the command and return. */
- commandTable[commandnum].function();
- return;
- }
- /* If we didn't find this command, say so. */
- fprintf(stderr,"Unrecognized command.\n");
- }
-
- Or something like that.
-
-