Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3402118
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T05:05:56+00:00 2026-05-18T05:05:56+00:00

I need some help with C++, please! I’m writing a command parser for a

  • 0

I need some help with C++, please!

I’m writing a command parser for a small text-based game, and I’ve run into some problems. The parser is supposed to read and parse commands entered by the player.

The most obvious and straightforward solution to this could be something like this (written in pseudo-code):

command <- read input from the player
if command == COMMAND1
    do command1
else if command == COMMAND 2
    do command2
...

I’m writing in C++, so I was thinking I could solve this by using an associative map and function pointers. I’m not that familiar with using functions pointers, so that may be why I’m having problems. What I want to do, is to have some sort of loop that waits for input, parse the input that is inserted, and calls a function depending on the command given. Here’s some C++-ish pseudo-code describing what I am thinking:

while(1) {
 cin >> input;
 char * tok = strtok(input, " ")
 functionpointer fptr = command_map.find(tok);
 ... // here, I get stuck on what to do..
}

So I hope I make myself somewhat clear on what I want to happen. The player could have had input something like

> go south

and I could have finished the code with something like:

destination = strtok(NULL, " ");
fptr(destination);

Basically, the value returned from the map would be the function that performs the command “go”, and that function apparently takes one argument, the destination. Again, this is some C++-pseudo-ish code. So I got the command “go” covered. But now say that I want to have the follwing command:

> attack troll with sword

Now I feel that I need to do something like:

while(1) {
 cin >> input;
 char * tok = strtok(input, " ")
 functionpointer fptr = command_map.find(tok);
 if(tok == "go"){
    destination = strtok(NULL, " ");
    fptr(destination);
 } else if (tok == "attack") {
    target = strtok(NULL, " ");
    weapon = strtok(NULL, " ");
    fptr(target, weapon);
   }
}

Again, this is pseudo-code. You probably see what I get hung up on: I have this map of functions pointers, but because I have variable number of arguments and type of arguments because I want to call different functions depending on what I got as the input, so I could’ve just done this without a map and function pointers like I showed you first. Is there some way I can make this more general, without having to have some if-else clause to figure out how many arguments to pass?

I hope you understand what I need help with 🙂 Thank you for reading!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-18T05:05:57+00:00Added an answer on May 18, 2026 at 5:05 am

    A better solution would be to have all of your functions take the same arguments. A good idea would be to first completely tokenize your input (say, into a vector of strings), and then pass that array to the functions. You could then use an associative container (such as a hash table or a std::map) to map command tokens to handler functions.

    For example:

    typedef std::vector<std::string> TokenArray;
    typedef void (*CommandHandler)(const TokenArray&);
    typedef std::map<std::string, CommandHandler> CommandMap;
    void OnGo(const TokenArray& tokens)
    {
        // handle "go" command
    }
    void OnAttack(const TokenArray& tokens)
    {
        // handle "attack" command
    }
    // etc.
    
    CommandMap handlers;
    handlers["go"] = &OnGo;
    handlers["attack"] = &OnAttack;
    // etc.
    
    while(1) {
      std::string input;
      cin >> input;
      std::istringstream tokenizer(input);
      TokenArray tokens;
      std::string token;
      while(tokenizer >> token)  // Tokenize input into whitespace-separated tokens
        tokens.push_back(token);
      CommandMap::iterator handler = handlers.find(tokens[0]);
      if(handler != handlers.end())
          (*handler)(tokens);  // call the handler
      else
          ; // handle unknown command
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am getting a little confused and need some help please. Take these two
I'm very new to MySQL so I need some help please, I have a
I need some help with a regular expression, please help if you can I
Having some issue with Threadpooling here that I need some help with please. I
I need some help from the shell-script gurus out there. I have a .txt
I need some help regarding algorithm for randomness. So Problem is. There are 50
I need some help calculating Pi. I am trying to write a python program
I need some help with jQuery script again :-) Just trying to play with
I need some help ... I'm a bit (read total) n00b when it comes
Hi I need some help with the following scenario in php. I have a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.