Discuss Scratch

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:
#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);
}
When I run the program, I get the entry box, but when I try entering something, I get a segfault. I tried adding this:
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);
}
And when I enter something, I get a load of gibberish in the terminal, followed by the segfault. What am I doing wrong

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.
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;
}
}
Shouldn't the
for (int i{}, e{}; i<length; ++i, ++e)
be
for (int i{}, e{}; i<length-1; ++i, ++e)
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!
SC_DStwo_Master
Scratcher
100+ posts

gtkmm entry holds gibberish

lugga wrote:

Just thought that maybe this is the problem.
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;
}
}
Shouldn't the
for (int i{}, e{}; i<length; ++i, ++e)
be
for (int i{}, e{}; i<length-1; ++i, ++e)
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!
nope
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

Powered by DjangoBB