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

Author Topic: sf::String and int  (Read 3800 times)

0 Members and 1 Guest are viewing this topic.

DarK_DemoN

  • Newbie
  • *
  • Posts: 11
    • AOL Instant Messenger - incinerator4995
    • View Profile
    • http://1337codez.darkbb.com
sf::String and int
« on: January 07, 2011, 06:35:16 am »
Hi i have a question on why i cannot set a sf::String to the value of an int.

Any help would be appreciated :D
- DarK_DemoN -

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
sf::String and int
« Reply #1 on: January 07, 2011, 09:13:02 am »
Convert your int to a regular standard string (Google for it) then assign it to your sf::String.
Laurent Gomila - SFML developer

DarK_DemoN

  • Newbie
  • *
  • Posts: 11
    • AOL Instant Messenger - incinerator4995
    • View Profile
    • http://1337codez.darkbb.com
sf::String and int
« Reply #2 on: January 08, 2011, 07:00:59 am »
Here is what i tried:

Code: [Select]

#include <iostream>
#include <windows.h>
#include <SFML/Graphics.hpp>
#include <SFML/System.hpp>
#include <sstream>

using namespace std;


int main()
{
    sf::RenderWindow App(sf::VideoMode(400,100,16), "Calculator - By: DarK_DemoN");

    sf::Font MyFont;
    if (!MyFont.LoadFromFile("Fonts\\AdamFont.ttf"))
        return EXIT_FAILURE;

    int a;
    int b;
    int c;
    std::stringstream stream;
    sf::String Output("Please enter a number: ");
    sf::String InputStr;

    while (App.IsOpened())
    {
        sf::Event Event;
        while (App.GetEvent(Event))
        {
            const sf::Input& Input = App.GetInput();
            if (Event.Type ==sf::Event::Closed )
                App.Close();

            if (Input.IsKeyDown(sf::Key::Escape))
                App.Close();

            if (Event.Type == sf::Event::TextEntered)
            {
                if (Event.Text.Unicode < 256)
                {
                    a += static_cast<char>(Event.Text.Unicode);
                    stream << a;
                }
            }
            InputStr.SetText(stream.str());

    }

}

 App.Draw(Output);
 App.Draw(InputStr);

 App.Display();
    }
    return 0;
}


It doesnt produce any errors but it is VERY unstable. When the window appears after compiling i cant enter in any numbers and after a couple of seconds it stops responding. Its either it does that or right after its done compiling i get a see-through window (no BG color, no text, nothing).

Please help
- DarK_DemoN -

Mjonir

  • Full Member
  • ***
  • Posts: 142
    • View Profile
sf::String and int
« Reply #3 on: January 08, 2011, 08:34:10 am »
Here's what I do:

Code: [Select]

#include <sstream>
#include <string>

string itos(const int &integer)
{
    ostringstream oss;
    oss << integer;
    return oss.str();
}


It works just the same way for other types like long and float so if you want you could overload the function for other types.

I used it a lot and never got any problems with it so it should be stable.

Maxamor

  • Newbie
  • *
  • Posts: 8
    • View Profile
sf::String and int
« Reply #4 on: January 10, 2011, 05:01:53 am »
You can also convert primitives to std::string easily using boost::lexical_cast.

Example:

Code: [Select]
int a;
sf::String InputStr;

a = 42;

InputStr.SetText(boost::lexical_cast<int>(a));


Originally I had written my own code to perform such conversions but realized that I was reinventing a wheel that has been invented many times before :)