Search code examples
c++cvisual-studiovisual-c++allegro5

Why does al_draw_textf() prints numbers instead of alphabets stored in a string variable?


I want to print a user's name on the allegro screen, for this I'm declaring a string variable and asking the user to input his name, after that, I'm converting the string to const char* so that I can pass on the variable to al_draw_textf(). But even after passing const char* variable to the function it is printing some numbers instead of the actual name entered by the user as alphabets. The code snippet is attached at the end.

There are no compile-time error being thrown, I'm using Visual studio 2019 enterprise and working with Allegro 5. I'm posting some screenshots of the program when run.

std::string name; 
std::cout << "\n\nHello player, what's your name?"; getline(cin, name); 
name = name.c_str();
const char* Name = name.c_str();
al_draw_textf(font, al_map_rgb(255, 255, 255), 10, 10, 0, "Hi, %d",Name);

Solution

  • The %d format string is used to print integers. To print a char * as text use %s.

    al_draw_textf(font, al_map_rgb(255, 255, 255), 10, 10, 0, "Hi, %s",Name);