Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: text object prints out name of font when drawn  (Read 1497 times)

0 Members and 2 Guests are viewing this topic.

grim

  • Guest
text object prints out name of font when drawn
« on: January 29, 2014, 11:42:05 pm »
This is the code that creates my text and font object:

   
font.loadFromFile("sansation.ttf");
text.setColor(sf::Color::Black);
text.setFont(font);
text.setCharacterSize(20);
text.setString(""+count);

It kind of works but instead of printing out the value of count, it prints out the name of the font i'm using. Now here is the funny part.  I have an event that is supposed to increase the value of count, but instead it updates the string so that it does this:
sansation.ttf
ansation.ttf
nsation.ttf
..and so on until the entire word is gone.

Why won't it print out what I set the string to?

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: text object prints out name of font when drawn
« Reply #1 on: January 29, 2014, 11:51:31 pm »
"" is a pointer to string literal. "" + count is a pointer to something which is located "count" bytes after the string literal in memory; most likely another string literal, like your font name.

Integers don't automatically convert to string in C++, you need to use something explicitly (depends on whether you use C++11 or not).
Laurent Gomila - SFML developer

Jesper Juhl

  • Hero Member
  • *****
  • Posts: 1405
    • View Profile
    • Email
Re: text object prints out name of font when drawn
« Reply #2 on: January 30, 2014, 09:19:42 pm »
With C++11 this does what you seem to want:

  text.setString(std::to_string(count));

With C++98/03 you can use a stringstream to obtain a textual representation of your count integer (this also works with C++11 of course).

There is also the option of using something like boost::lexical_cast or even functions from the C library (like sprintf) although I would not recommend that.