C++: How To Use %i in Function?

215 Views Asked by At

I have a little question, is it possible to use %i in function? Here is what I need to do. I have a function callback:

engine.gui->menu.addItem(Menu::AGILITY,"Agility (+1 defense)");

and i want to use it in this way:

engine.gui->menu.addItem(Menu::AGILITY,"Agility (%i defense)",engine.level);

How I need to implement that, or that might be not even possible? I tried my best to do something, but I didn't managed it. :( The reason that I want to do this is then player avenges to the next level, engine.level counts gets ++, so then player level ups he can get more AGILITY in different levels, and then leveling he can be informed how many agility he could get.

2

There are 2 best solutions below

0
On BEST ANSWER

If you have C++11, you can do:

engine.gui->menu.addItem(
        Menu::AGILITY,
        "Agility (" + std::to_string( engine.level ) + " defense)" );

If you don't have C++11, you should have the equivalent of std::to_string in your toolkit. Something like:

template <typename T>
std::string
toString( T const& obj )
{
    std::ostringstream result;
    result << obj;
    return result.str();
}
0
On

Most likely boost::format (http://www.boost.org/doc/libs/1_54_0/libs/format/) should do what you want. It won't be that exact syntax but should be close.