I’m working on a little console game with ncurses. In the main menu I want the user to be able to set the control keys. Now as far as I understood, in ncurses you can access e.a. the a-key by the int value of the char ‘a’. Using the key ingame with ‘a’ works flawlessly, however I’m stuck with the menu:
For each key binding I have stored an int-value that is defaulted to e.a. ‘a’. When the game starts, I read the ‘a’ binding correctly from the int-value and can use it ingame. However, it is not displayed correctly. I want it to be like this: “Left: a”. I do it like this:
std::string key = "Left: " + static_cast<char>(_value);
_value being the int-value I stored and initialized as ‘a’. I also tried it without the cast, btw.
Now what’s being displayed is strange. Instead of “a” it says “~T^C”. For the letters “s” and “d” it doesn’t display anything at all. “w” becomes some very strange encoding question marks.
I suppose it’s got something to do with the encoding of the characters in int-values or something. So what can I do to get it displayed the right way?
Thanks a lot!
You’re adding together the adress of string literal and ASCII value of
_value. Stringkeyis then constructed from whatever happens to be at that garbage adress.Remember that string literals are of type array of N
const charand that arrays decay to pointer to their first element when passed to functions and operators etc., which yields youconst char*. The built in + operator for pointers doesn’t do string concatenation. You need to construct astd::stringfrom at least one of operands for overloaded operator to kick in: