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

Author Topic: Displaying a number with sf::string  (Read 14072 times)

0 Members and 1 Guest are viewing this topic.

Davi

  • Newbie
  • *
  • Posts: 6
    • View Profile
Displaying a number with sf::string
« on: April 10, 2012, 07:57:08 pm »
Hey all. I am using the MinGW which comes with Code::Blocks, SFML 1.6.

I will be straight on my question. How do I display a number? I've tried this:

Code: [Select]
PlayingPos.SetText(Music1.GetPlayingOffset());
To later display that string, but SetText() can't take float as argument. So, how could I set my string as number for later use? Is there any conversion possible?

texus

  • Sr. Member
  • ****
  • Posts: 499
    • View Profile
    • TGUI
    • Email
Re: Displaying a number with sf::string
« Reply #1 on: April 10, 2012, 08:15:42 pm »
You can use sprintf.

char temp[256];
sprintf(temp, "%f", theFloat);
PlayingPos.SetText(temp);
TGUI: C++ SFML GUI

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Re: Displaying a number with sf::string
« Reply #2 on: April 10, 2012, 08:18:00 pm »
Or you can search Google ("C++ convert float to string") to find the right solution ;)
Laurent Gomila - SFML developer

Davi

  • Newbie
  • *
  • Posts: 6
    • View Profile
Re: Displaying a number with sf::string
« Reply #3 on: April 10, 2012, 08:22:07 pm »
I will try yours solution, @texus.

Or you can search Google ("C++ convert float to string") to find the right solution ;)

I tried that, but std::to_string() and other string conversion functions simply seems to not be available at my compiler at all, or were too complicated, like, multiple lines of code just for a conversion?
« Last Edit: April 10, 2012, 08:23:57 pm by Davi »

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Re: Displaying a number with sf::string
« Reply #4 on: April 10, 2012, 08:29:09 pm »
Quote
I will try yours solution, @texus.
You shouldn't, this is unsafe C code.

Quote
I tried that, but std::to_string() and other string conversion functions simply seems to not be available at my compiler at all, or were too complicated, like, multiple lines of code just for a conversion?
std::to_string is available only if your compiler supports (and is configured for) the new standard.

The good old solution is:
#include <sstream>
#include <string>

std::ostringstream oss;
oss << value;
std::string str = oss.str();

You can make it a function, similar to the std::to_string one:
#include <sstream>
#include <string>

template <typename T>
std::string to_string(T value)
{
    std::ostringstream oss;
    oss << value;
    return oss.str();
}

std::string str = to_string(5.f);
Laurent Gomila - SFML developer

texus

  • Sr. Member
  • ****
  • Posts: 499
    • View Profile
    • TGUI
    • Email
Re: Displaying a number with sf::string
« Reply #5 on: April 10, 2012, 08:42:59 pm »
You shouldn't, this is unsafe C code.
I'll remember that for in the future.

But why would it be unsafe?
I do know that the buffer has to be big enough, but when converting e.g. an integer you will never need 256 characters.
TGUI: C++ SFML GUI

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Re: Displaying a number with sf::string
« Reply #6 on: April 10, 2012, 09:52:55 pm »
Quote
But why would it be unsafe?
Since there's no max size passed to the function, it can easily overflow.

Quote
I do know that the buffer has to be big enough, but when converting e.g. an integer you will never need 256 characters.
Yeah of course it is rarely an issue with such simple code, but it is good practice to avoid such functions in favor of their variant which has an additional max_size argument (snprintf).

And it is also recommended to avoid C functions where there are safer, more strongly typed C++ equivalents :)
Laurent Gomila - SFML developer

VPellen

  • Newbie
  • *
  • Posts: 14
    • View Profile
Re: Displaying a number with sf::string
« Reply #7 on: April 11, 2012, 03:46:29 am »
Ahh yes, converting a number to a string.

If you want to convert a number to an std::string (and you really should use std strings where possible, by the way), C++11 has a std::to_string function. If your particular implementation doesn't support std::to_string, or you don't like to use C++11, and you're not too concerned about speed, the following function works, and when std::to_string is implemented in the standard library, you can just delete the function definition and carry on as normal and everything should just work.

Code: [Select]
#include <string>
#include <sstream>

namespace std
{
    template <typename T>
    string to_string(const T & value)
    {
        stringstream stream;
        stream << value;
        return stream.str();
    }
}

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Re: Displaying a number with sf::string
« Reply #8 on: April 11, 2012, 08:04:54 am »
You didn't read the above messages at all, did you? ???
Laurent Gomila - SFML developer

iHope

  • Newbie
  • *
  • Posts: 35
    • View Profile
Re: Displaying a number with sf::string
« Reply #9 on: April 11, 2012, 03:10:53 pm »
This answer is "bit" late, but I still want tell you how I do it.
Code: [Select]
std::ostringstream oss;
oss << number;
NumberText.SetText(oss.str());
NumberText.SetColor(sf::Color(255,255,255));
NumberText.SetPosition(10.f, 10.f);

-

App.Draw(NumberText);

Works well in my Pong! game, so I assume that it should work.

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Re: Displaying a number with sf::string
« Reply #10 on: April 11, 2012, 03:33:47 pm »
How many useless duplicated answers will we have in this thread? >:(
Laurent Gomila - SFML developer

Rosme

  • Full Member
  • ***
  • Posts: 169
  • Proud member of the shoe club
    • View Profile
    • Code-Concept
Re: Displaying a number with sf::string
« Reply #11 on: April 11, 2012, 05:59:24 pm »
Well since I don't read I'm gonna give another one...
Just kidding Laurent ;)
GitHub
Code Concept
Twitter
Rosme on IRC/Discord

StormWingDelta

  • Sr. Member
  • ****
  • Posts: 365
    • View Profile
Re: Displaying a number with sf::string
« Reply #12 on: April 11, 2012, 07:26:03 pm »
You can use sprintf.

char temp[256];
sprintf(temp, "%f", theFloat);
PlayingPos.SetText(temp);

There's another reason this is unsafe and that is if you don't have a number here or you have the wrong type it will crash.  I've had this happen several times and it gets annoying. :(
I have many ideas but need the help of others to find way to make use of them.

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Re: Displaying a number with sf::string
« Reply #13 on: April 11, 2012, 08:23:05 pm »
Quote
There's another reason this is unsafe and that is if you don't have a number here or you have the wrong type it will crash.  I've had this happen several times and it gets annoying.
Some compilers are now able to detect types that don't match the format string.
Laurent Gomila - SFML developer

 

anything