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

  • Home
  • SEARCH
  • 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 7934757
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T21:43:44+00:00 2026-06-03T21:43:44+00:00

Is there an easy way in C++ to have a message lookup table that

  • 0

Is there an easy way in C++ to have a message lookup table that allows the use of variables.

For example in C you can have something like this:

const char* transaction_messages[] = {
"UNAUTHORIZED",
"UNKNOWN_ORDER",
"UNKNOWN_DEALER",
"UNKNOWN_COMMODITY",
"INVALID_MESSAGE",
"BOUGHT %d %s @ %f.1 FROM %s", //amount, commodity, price, dealer
"SOLD %d %s @ %f.1 FROM %s", //amount, commodity, price, dealer
"%d HAS BEEN FILLED", //order id
"%d HAS BEEN REVOKED", //order id
"%d %s %s %s %d %f.1 HAS BEEN POSTED", //order id, dealer, side, commodity, amount, price
"%d %s %s %s %d %f.1" //order id, dealer, side, commodity, amount, price
};

and then use it in a function like thislike this:

void myfunction(int amount, char* commodity, double price, char* dealer){

    char *msg = transaction_message[6];
    printf(msg, amount, commodity, price, dealer);
}

I want to be able to do the same with an ostream instead of having to do something with the << operator where the same thing would look like:

ostream << "BOUGHT" << amount << " " << commodity << " @ " << price << " FROM " << dealer;

the only way I can think of doing this right now would be to have a bunch of inline functions that return strings and instead of having a char* table instead have a function table that looks up the inline functions. There must be an easier way tho.

  • 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-06-03T21:43:45+00:00Added an answer on June 3, 2026 at 9:43 pm

    What you are doing is very similar to Localization (AKA L10N).

    There are a couple of questions on this: Best way to design for localization of strings

    But there are several packages that already deal with this.
    These are basically designed to take all the strings from your application and package them in a separate resource (the correct resource (depending usually on locale) is selected at runtime). But they usually use the “English” (or should I say original text for non English programers) as the look-up text to find the correct resource string (thus the code is still readable to the developer) and the user gets a language specific string displayed.

    Of course boost also has one

    • boost::localization

    But there are others (A quick google finds)

    • GNU gettext
    • GNU libiconv

    Othere resources:

    • http://en.wikipedia.org/wiki/GNU_gettext
    • http://site.icu-project.org/

    But getting the correct string is only half the battle. Then you need to correctly swap run-time values with place holders in the string. Personally this is where I think boost::format really shines.

    Example:

    sprintf("The time is %s in %s.", time, country);
    

    The trouble is the order of nouns and verbs differ across languages. For example if we translate

    “The time is 12:00 in Germany.”

    into Azerbaijani

    “Saat Almaniya saat 12:00 deyil.”

    You will notice that the word for “Germany” (Almaniya) swaps places with the time. Thus the simple task of replacing items in a specific order does not work. What you need are indexed placeholders. (boost::format to the rescue).

    std::cout << boost::formt("The time is %1 in %2.") << time << country;
    // Notice the place holders are number '%1' means the first bound argument
    // But the format string allows you to place them in any order in the string
    // So your localized resource will look like this:
    
    std::cout << boost::formt("Saat %2 saat %1 deyil.") % time % country;
    

    Or more likely:

    std::cout << boost::formt(I10N.get("The time is %1 in %2.")) % time % country;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is there an easy way to change the validation message that occurs for something
Is there an easy way to have the language of the modx backend interface
Is there an easy way to unroll the stack in Tcl? I have this
I am sure there is no easy way to do this but I have
Is there an easy way to calculate the derivative of non-liner functions that are
Is there an easy way to use UIElement.Effect in WPF to shift the color
Is there an easy way to work with JSON within oracle? I have a
Is there an easy way to have the Ant logger (default or other) add
is there an easy way to timeout an SQL statement so that it will
I have a table with one column that is of text type. There's 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.