Re: Cast from gchar* to int?



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;


gchar *num = gtk_editable_get_chars(GTK_EDITABLE(contact_num_entry),0,-1);
gint i = 0;
i = atoi(num);
cout << i << endl;


So can someone please tell me why both the above return 0.

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.

So if I enter 4, I need to be able to increment it to 5. Pretty basic,
but I can't seem to get the value entered into a numeric data type to be
added or in another scenario subtracted.

I assume you can't subtract and add char* as if they were numeric data
types. I have not tried it, but I really doubt it could be done, so I am
pretty sure I will have to convert to a numeric data type to
add/subtract/etc.

Thanks.

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

and

to set the value:
http://developer.gnome.org/doc/API/2.0/gtk/gtkspinbutton.html#gtk-spin-button-set-value

to get the value (by default as a double):
http://developer.gnome.org/doc/API/2.0/gtk/gtkspinbutton.html#gtk-spin-button-get-value

or as an int:
http://developer.gnome.org/doc/API/2.0/gtk/gtkspinbutton.html#gtk-spin-button-get-value-as-int

there is also a function to get it as a float. Remember if using this control to call _gtk_spin_button_set_numeric_ to make sure it ONLY accepts numeric values.

Regards,

Martyn.




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