🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: rec.games.programmer
Article #1640

Re: Goto [variable] in C++ ?

#1640
From: Andrew Bell
Date: Wed, 30 Dec 1992 08:01
43 lines
1616 bytes
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.
Technical Details
Message-ID: <18566@borg.cs.unc.edu>
Path: arc1-ca.pugleaf.net!sparky!uunet!gatech!concert!borg!beethoven!bell
References: <725647749.AA00000@therose.pdx.com>

Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!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