1
General / String erase problem while using Event::TextEntered
« on: March 20, 2022, 06:00:23 pm »#include <SFML/Graphics.hpp>
#include <iostream>
using namespace std;
using namespace sf;
int main()
{
Keyboard keyboard;
RenderWindow window( VideoMode( 800, 600 ), "..." );
string help;
while( window.isOpen() )
{
Event event;
while( window.pollEvent( event ) )
{
if( event.type == Event::Closed )
window.close();
if( event.type == Event::KeyPressed && keyboard.isKeyPressed( Keyboard::Backspace ) )
{
help.erase( help.end() - 1 );
cout << help << endl;
}
else if ( event.type == Event::TextEntered && event.text.unicode < 128 )
{
help += static_cast<char>(event.text.unicode);
cout << help << endl;
}
}
window.display();
}
return 0;
}
#include <iostream>
using namespace std;
using namespace sf;
int main()
{
Keyboard keyboard;
RenderWindow window( VideoMode( 800, 600 ), "..." );
string help;
while( window.isOpen() )
{
Event event;
while( window.pollEvent( event ) )
{
if( event.type == Event::Closed )
window.close();
if( event.type == Event::KeyPressed && keyboard.isKeyPressed( Keyboard::Backspace ) )
{
help.erase( help.end() - 1 );
cout << help << endl;
}
else if ( event.type == Event::TextEntered && event.text.unicode < 128 )
{
help += static_cast<char>(event.text.unicode);
cout << help << endl;
}
}
window.display();
}
return 0;
}
Try running it, type anything you would like and try to use backspace. Used once works good, but if i try to delete second character then nothing changes in my string. How to solve this?