Re: Cast from gchar* to int?



I had this same exact problem William.  I used atoi() just like you did
however it worked for me. These are the 2 relevant lines of code.

int pick3player[3] = { -1, -1, -1 };
pick3player[i] =
atoi(gtk_editable_get_chars(GTK_EDITABLE(pick3entry[i]), 0, -1));


This gives me the numbers that were put into the 3 entry boxes and
stores them in the int array. 
There shouldn't be anything else that you need to do.

I use g_snprintf() to make the int go back to a char to use
gtk_entry_set_text on it.

Probably not the most elegant solution but I don't profess to be a real
programmer.

HTH
brandon





"William L. Thomson Jr." wrote:

Martyn,

On Tue, 2002-09-24 at 15:13, Martyn Russell wrote:
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:


I had thought about it, but prefer the look of the regular text field. I
have buttons that will handle the incrementing up or down, as well as
extracting the relevant data from a database and filling in all
appropriate fields.

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.

I like that the spin_button can be set to only accept numeric input. I
have not found a similar function call for the regular entry fields.

Now with that said, from playing with the spin_button, is there a way to
not specify a maximum size? The max size will be determined by seeing if
the table in the database has that many rows. I could set it at run
time, but it would be another query. One that I would like to avoid if
possible.

I really would like to stick with the standard gtk_entry widget, and not
use the spin_button if I can. It should be possible, no?

I am not really worried about non numeric input, because only
intelligent people will be using this app internally. We know to only
put numeric input in numeric fields.

I just really would like to know how to convert a char* to an int, and
why the atoi functions and others will not do this for me. Not to
mention why they output 0.

If I test with a regular numeric string and convert to an int/long/etc
using the atoi/atol/etc functions it works. If I swap out my hard coded
numeric string with the input from the gtk_entry field I get 0.

Really strange and a little frustrating.

Thanks for your input though, I may still have to use the spin_button,
so thanks for the suggestion.

--
Sincerely,
William L. Thomson Jr.
Support Group
Obsidian-Studios Inc.
439 Amber Way
Petaluma, Ca. 94952
Phone  707.766.9509
Fax    707.766.8989
http://www.obsidian-studios.com

_______________________________________________
gtk-app-devel-list mailing list
gtk-app-devel-list gnome org
http://mail.gnome.org/mailman/listinfo/gtk-app-devel-list



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