 |
GTK+ Forums Discussion forum for GTK+ and Programming. Ask questions, troubleshoot problems, view and post example code, or express your opinions.
|
|
|
| Author |
Message |
|
|
Ujjwol Familiar Face
Joined: 26 Feb 2008 Posts: 10
|
Posted: Mon Mar 03, 2008 1:45 am Post subject: Addition problem with GTK |
|
|
I am at learning phase of C and GTK and i wrote following code in callbacks.c
but it get compiles without error but if i click a button (button1) the GNOME Bugbudddy comes
-------------
#ifdef HAVE_CONFIG_H
# include <config.h>
#endif
#include <gtk/gtk.h>
#include "callbacks.h"
#include "interface.h"
#include "support.h"
void
on_button1_clicked (GtkButton *button,
gpointer user_data)
{
GtkWidget * label = lookup_widget(GTK_WIDGET(button), "label1");
GtkWidget * entry1 = lookup_widget(GTK_WIDGET(button), "entry1");
GtkWidget * entry2 = lookup_widget(GTK_WIDGET(button), "entry2");
GtkWidget * entry3 = lookup_widget(GTK_WIDGET(button), "entry3");
gint numone = 0;
gint numtwo = 0;
numone = gtk_entry_get_text(GTK_ENTRY(entry1));
numtwo = gtk_entry_get_text(GTK_ENTRY(entry2));
gint ans = 0;
ans = numone + numtwo;
gtk_label_set_text(GTK_LABEL(label),"The solution is:");
gtk_entry_set_text(GTK_ENTRY(entry3),ans);
}
-----------------------
I think the error is in this statement:
ans = numone + numtwo;
Thanks in advance... |
|
| Back to top |
|
 |
caracal GTK+ Geek
Joined: 21 Jun 2007 Posts: 75 Location: Wilkes Barre Pa
|
Posted: Tue Mar 11, 2008 3:59 am Post subject: |
|
|
| Could you post all of the code please all you have there is a gtk button clicked event. |
|
| Back to top |
|
 |
kharrison Familiar Face
Joined: 03 Oct 2007 Posts: 25 Location: Virginia (Northern)
|
Posted: Thu Mar 20, 2008 5:59 am Post subject: |
|
|
I think your problem is that:
gtk_entry_get_text() returns a pointer to a character string which contains the "text" value internal to the widget.
Therefore when it executes: numone = gtk_entry_get_text(GTK_ENTRY(entry1));
numone is actually an address, not the numeric value of the string.
Your ans = numone + numtwo simply adds two pointer values.
When you perform the gtk_entry_set_text(GTK_ENTRY(entry3),ans)
the value of "ans" is an address to some bogus memory location.
To do what you want to do (assuming that you want to stick with a GtkEntry widget (instead of a GtkSpinButton which is what I recommend) you will have to convert the string to a numerical value ( look at the function "strtol" ) to assign it to your gint values. Then after calculating the answer (ans) convert it back to a string using sprintf before using gtk_entry-set_text() which requires a string pointer.
(Note: to clarify, by "string I am referring to a char* or char array, NOT the C++ STL string class.)
Kirk |
|
| Back to top |
|
 |
|
Powered by phpBB © 2001, 2005 phpBB Group CodeBB 1.0 Beta 2
|