Discuss Scratch
- Discussion Forums
- » Advanced Topics
- » gtkmm entry holds gibberish
- SC_DStwo_Master
- Scratcher
100+ posts
gtkmm entry holds gibberish
I'm trying to make a simple program that does input validation on the text that the user inputs so that you cannot enter ‘a’. Here's the code:
When I run the program, I get the entry box, but when I try entering something, I get a segfault. I tried adding this:
And when I enter something, I get a load of gibberish in the terminal, followed by the segfault. What am I doing wrong
#include <gtkmm/application.h>
#include <gtkmm/window.h>
#include <gtkmm/entry.h>
#include <cstring>
#include <iostream>
class entry
{
protected:
Gtk::Entry myEntry;
public:
void changed()
{
Glib::ustring text = myEntry.get_text();
int length = text.length();
char newText[length];
if (text[length-1]=='a')
{
for (int i{}, e{}; i<length; ++i, ++e)
{
if (i==length-1) { --e; continue; }
newText[e]=text[i];
std::cout << text[i] << std::endl;
}
}
Glib::ustring newTextConverted(newText);
myEntry.set_text(newTextConverted);
}
entry(Gtk::Window& window)
{
myEntry.signal_changed().connect(sigc::mem_fun(this,&entry::changed));
window.add(myEntry);
myEntry.show();
}
};
int main(int argc, char** argv)
{
Glib::RefPtr<Gtk::Application> app = Gtk::Application::create(argc,argv,"org.nyap.test.base");
Gtk::Window window;
window.set_default_size(200,200);
entry textt(window);
return app->run(window);
}
void changed()
{
Glib::ustring text = myEntry.get_text();
//Here!
std::cout << text.c_str() << std::endl;
int length = text.length();
char newText[length];
if (text[length-1]=='a')
{
for (int i{}, e{}; i<length; ++i, ++e)
{
if (i==length-1) { --e; continue; }
newText[e]=text[i];
std::cout << text[i] << std::endl;
}
}
Glib::ustring newTextConverted(newText);
myEntry.set_text(newTextConverted);
}
Last edited by SC_DStwo_Master (Oct. 8, 2016 17:38:36)
- MegaApuTurkUltra
- Scratcher
1000+ posts
gtkmm entry holds gibberish
Why not just use ustring::find() and ustring::replace()?
- lugga
- Scratcher
500+ posts
gtkmm entry holds gibberish
Just thought that maybe this is the problem.
Shouldn't the be To see if an the text is being accessed out of bounds.
Just to warn you, I might be wrong but it's worth a try!
if (text[length-1]=='a')
{
for (int i{}, e{}; i<length; ++i, ++e)
{
if (i==length-1) { --e; continue; }
newText[e]=text[i];
std::cout << text[i] << std::endl;
}
}
for (int i{}, e{}; i<length; ++i, ++e)
for (int i{}, e{}; i<length-1; ++i, ++e)
Just to warn you, I might be wrong but it's worth a try!
- SC_DStwo_Master
- Scratcher
100+ posts
gtkmm entry holds gibberish
nope Just thought that maybe this is the problem.Shouldn't theif (text[length-1]=='a')
{
for (int i{}, e{}; i<length; ++i, ++e)
{
if (i==length-1) { --e; continue; }
newText[e]=text[i];
std::cout << text[i] << std::endl;
}
}befor (int i{}, e{}; i<length; ++i, ++e)To see if an the text is being accessed out of bounds.for (int i{}, e{}; i<length-1; ++i, ++e)
Just to warn you, I might be wrong but it's worth a try!
- SC_DStwo_Master
- Scratcher
100+ posts
gtkmm entry holds gibberish
turns out there's a function just for this called “erase”
it works fine now
thanks for the help though
it works fine now
thanks for the help though
- Discussion Forums
- » Advanced Topics
- » gtkmm entry holds gibberish