Re: Cast from gchar* to int?



El mar, 24-09-2002 a las 18:13, Martyn Russell escribió:
William L. Thomson Jr. wrote:
Ok, I am still not having any luck, here is some code examples for you all

Neither of these work, and both return the exact same result, 0.
Now the 0 is not the one that I initialized gint i to, but the value that
is returned from passing the const gchar *num to the atoi function.

So if I start with gint i = 4, it will still output 0;

const gchar *num = gtk_entry_get_text(GTK_ENTRY(contact_num_entry));
gint i = 0;
i = atoi(num);
cout << i << endl;
[...]
Basically what I am trying to do, is I have a entry field that will
accept numeric input. I then need to get that value, and increment it by
one.
[...]
It looks to me like you are using the wrong control.  Perhaps you want a 
spin button?  This would mean you could guarantee an integer and not a 
string with other character (not 0-9) AND you can get an int using a gtk 
function call: see here:

http://developer.gnome.org/doc/API/2.0/gtk/gtkspinbutton.html

another way is to use sscanf.

sscanf (num, "%d", &i);

However, it could be nice have something like an entry mask.

-- 
German Poo Caaman~o
mailto:gpoo ubiobio cl
http://www.ubiobio.cl/~gpoo/chilelindo.html
"Hay 10 tipos de personas: las que entienden binario y las que no."




[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]