Article View: rec.games.programmer
Article #1784Re: Goto [variable] in C+
From: Cyberman
Date: Thu, 31 Dec 1992 17:19
Date: Thu, 31 Dec 1992 17:19
41 lines
1656 bytes
1656 bytes
Sir spoke forth saying:
"Goto [variable] in C++ ?" toward
on 12-28-92 14:44
Ap> This is a question from someone new to C...
Ap> How could I set up C code so that a program goes to a function where
Ap> the name of the function is placed in a string variable? The specific
Ap> use I'm thinking of is for an adventure game, where the function the
Ap> parser calls depends on the verb the player used in typing in his
Ap> command.. I could set it up as:
Ap> -If verb_parsed is 'eat' then call eat_function
Ap> -If verb_parsed is 'give' then call give_function
Ap> and so forth, but this is awful... How could I write it in C to look
Ap> like:
Ap> -Go to [verb_parsed + string_holding_parameter_characters]
You could use tokens and a switch statement:
ie
switch(verb_parse(data)) {
case eat:eat(args); break;
...
}
or you can do this
int (*actions)()[TOTAL_ACTIONS];
then
actions[verb_parse(data)](args);
Stephen Cyberman@Toz.Buffalo.NY.US
Mangled on Wed 12-30-1992 at 17:27:24
... It balances! It balances! Run the paychecks!
---
* Blue Wave/QWK v2.11 *
Technical Details
Message-ID:
<gate.mZPNwB1w165w@toz.buffalo.ny.us>Path:
arc1-ca.pugleaf.net!sparky!uunet!usc!zaphod.mps.ohio-state.edu!cis.ohio-state.edu!rutgers!ub!toz!cybermanShow Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!sparky!uunet!usc!zaphod.mps.ohio-state.edu!cis.ohio-state.edu!rutgers!ub!toz!cyberman From: cyberman@toz.buffalo.ny.us (Cyberman) Newsgroups: rec.games.programmer Subject: Re: Goto [variable] in C+ Message-ID: <gate.mZPNwB1w165w@toz.buffalo.ny.us> Date: 31 Dec 92 17:19:44 GMT Lines: 41 X-Maildoor: WaflineMail 1.00r