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

Author Topic: Int to string  (Read 4422 times)

0 Members and 1 Guest are viewing this topic.

DmitryD

  • Newbie
  • *
  • Posts: 3
    • View Profile
Int to string
« on: August 28, 2014, 01:56:46 am »
How correctly convert int to std::string?

OS: Windows
IDE: CodeBlocks with MinGW 4.7 (32 bit)

(click to show/hide)

Strelok

  • Full Member
  • ***
  • Posts: 139
    • View Profile
    • GitHub
Re: Int to string
« Reply #1 on: August 28, 2014, 02:00:51 am »
If I recall correctly you don't have std::to_string() on mingw you need to find a workaround
« Last Edit: August 28, 2014, 02:03:00 am by Strelok »

G.

  • Hero Member
  • *****
  • Posts: 1593
    • View Profile
Re: Int to string
« Reply #2 on: August 28, 2014, 03:47:34 am »
Ever heard of search engines? ;)
What do int and std::string have to do with the Graphics module of SFML?

Hapax

  • Hero Member
  • *****
  • Posts: 3379
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: Int to string
« Reply #3 on: August 28, 2014, 04:36:45 am »
uses namespaces
using namespace sf;
using namespace std;

then...
sf::RenderWindow window(sf::VideoMode(400, 200), "SFML text test");
sf::Font font;
sf::Text Ttext;
std::string score, score2;
std::srand(static_cast<unsigned int>(std::time(NULL)));
Ttext.setColor(sf::Color::Black);
Pscore = std::rand() % 6;
sf::Event event;

Best suggestion is to remove the "using namespace" lines. It'll still work fine since you're correctly fully qualifying everything already.
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*

DmitryD

  • Newbie
  • *
  • Posts: 3
    • View Profile
Re: Int to string
« Reply #4 on: August 29, 2014, 12:53:21 am »
Thanks Strelok! Working. :)

How about add to SFML this function:

#include <sstream>

std::string Convert_Int_to_str(int Integer)
{

std::string result;

result = static_cast<std::ostringstream*>( &(std::ostringstream() << Integer) )->str();
return result;

}

This will useful function for some guys like me.

Jesper Juhl

  • Hero Member
  • *****
  • Posts: 1405
    • View Profile
    • Email
Re: Int to string
« Reply #5 on: August 29, 2014, 01:01:51 am »
There's no reason to have something like that in SFML.

1. It's trivial to implement yourself (using sprintf, string stream, boost::lexical_cast & more).
2. Modern C++11 has std::to_string.
« Last Edit: September 07, 2014, 06:49:53 pm by Jesper Juhl »

 

anything