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

Author Topic: Why does it delete the last charachter  (Read 2254 times)

0 Members and 1 Guest are viewing this topic.

anttton

  • Newbie
  • *
  • Posts: 17
    • View Profile
    • Email
Why does it delete the last charachter
« on: March 02, 2017, 05:46:32 pm »
Hello, I always get "out of range" when I delete a the invisble charachter. I dont know why I run into this error. I have search and include the s1.getSize so I can see how big the string is but I dosent seem to work.

S1 is a SF::string
CharachterName is a SF::Text


if (event.type == sf::Event::TextEntered)
                {
                                if (event.text.unicode == '\b' && s1.getSize() > 0)
                                {
                                        s1.erase(s1.getSize() - 1, 1);
                                        this->characterName->setString(s1);
                                }
                       
               
                                if (event.text.unicode < 128)
                                {
                                        s1 += static_cast<char>(event.text.unicode);
                                }              
                        else
                        {
                        }
                        break;
                }
 

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11030
    • View Profile
    • development blog
    • Email
Why does it delete the last charachter
« Reply #1 on: March 02, 2017, 06:46:44 pm »
The easier solution with C++11 is to use s1.pop_back(), no index juggling and quite expressive.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

anttton

  • Newbie
  • *
  • Posts: 17
    • View Profile
    • Email
Re: Why does it delete the last charachter
« Reply #2 on: March 02, 2017, 06:55:11 pm »
Hey, I can not use .pop_back with s1 insted of s1.erase(s1.getSize() - 1, 1). Have I done something wrong?
I think you only can use pop_back if its a std::string
« Last Edit: March 02, 2017, 06:57:59 pm by anttton »

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11030
    • View Profile
    • development blog
    • Email
Re: Why does it delete the last charachter
« Reply #3 on: March 02, 2017, 08:20:41 pm »
Ah, I didn't realize s1 was a sf::String.

I took your code and made a minimal example out of it and it seems to work fine.

#include <SFML/Graphics.hpp>

int main()
{
    sf::RenderWindow window(sf::VideoMode(800, 600), "Test");
    window.setVerticalSyncEnabled(true);

    sf::Font font;
    font.loadFromFile("arial.ttf");

    sf::String s1;
    sf::Text txt(s1, font);

    while(window.isOpen())
    {
        sf::Event event;
        while(window.pollEvent(event))
        {
            if (event.type == sf::Event::Closed)
            {
                window.close();
            }

            if (event.type == sf::Event::TextEntered)
            {
                if (event.text.unicode == '\b')
                {
                    if (s1.getSize() > 0)
                        s1.erase(s1.getSize() - 1, 1);
                }
                else if (event.text.unicode < 128)
                {
                    s1 += static_cast<char>(event.text.unicode);
                }

                txt.setString(s1);
            }
        }

        window.clear();
        window.draw(txt);
        window.display();
    }
}

I guess your error must come from a different place than what you thought.
I noticed that with the posted code, you're still adding the backspace character to the string, as such I moved the size condition inside the other if block and added an else if for the second block.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

anttton

  • Newbie
  • *
  • Posts: 17
    • View Profile
    • Email
Re: Why does it delete the last charachter
« Reply #4 on: March 02, 2017, 10:00:31 pm »
Thank you for the help, It work now!
« Last Edit: March 02, 2017, 10:28:34 pm by anttton »

 

anything