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

Author Topic: Event::TextEntered strange behavior..  (Read 1462 times)

0 Members and 1 Guest are viewing this topic.

gwyn0431

  • Newbie
  • *
  • Posts: 3
    • View Profile
    • Email
Event::TextEntered strange behavior..
« on: July 22, 2017, 12:12:26 pm »
Hello everyone, i've just begun to learn SFML (couple days), my knowledge in c++ is average, i've read "C++ Primer", Lippman and already have some c++ code experience.
I have this simple code and my problem is, that it actually does't outputs characters to my console. It outputs everything i've typed AFTER i close the window. But, if i uncomment the << endl; statement, it outputs characters typed in real time. It seems i can't output my typed characters in real time, without having a new line after each character, which is super strange.
If anyone knows, please help)
P.S Sorry for my bad english sometimes, probably, i'm not a native speaker(

#include <SFML/Graphics.hpp>
#include <iostream>
#include <string>

int main() {
    sf::RenderWindow window(sf::VideoMode(500, 500), "SFML works!");
    std::string display = "";
    window.setKeyRepeatEnabled(false);
    while (window.isOpen()) {
        sf::Event event;
        while (window.pollEvent(event)) {
            if (event.type == sf::Event::Closed) {
                window.close();
            }

            else if (event.type == sf::Event::TextEntered) {
                std::cout << (char)(event.text.unicode) /* Try to uncomment THIS << std::endl */;
            }
        }

        window.display();

    }
    return 0;
}
« Last Edit: July 22, 2017, 07:18:14 pm by gwyn0431 »

G.

  • Hero Member
  • *****
  • Posts: 1593
    • View Profile
Re: Event::TextEntered strange behavior..
« Reply #1 on: July 22, 2017, 03:50:22 pm »
Use std::flush if you want to output your text when the buffer is incomplete.
std::cout << (char)(event.text.unicode) << std::flush;
It also works with std::endl because std::endl calls flush.

gwyn0431

  • Newbie
  • *
  • Posts: 3
    • View Profile
    • Email
Re: Event::TextEntered strange behavior..
« Reply #2 on: July 22, 2017, 07:17:58 pm »
Thanks a lot, man, a lack of knowledge here) I got it now.