Re: A Newbie Float Problem
Re: A Newbie Float Problem
- Subject: Re: A Newbie Float Problem
- From: Phil Frost <email@hidden>
- Date: Thu, 15 Dec 2005 22:16:27 -0500
On Thu, Dec 15, 2005 at 10:09:11PM -0500, Jonathan Faulkenberry wrote:
>
Hi all,
>
I am relatively new to Cocoa, although I have some experience with
>
some basic C. For some reason though, I am having trouble creating a
>
small app I am using to help me learn Cocoa. This app is supposed to
>
hold a float value named "money" an have two buttons, one which will
>
call addMoney: (which will add 5 to the float value) and one called
>
drink: (which will decrease the float value by 5). For some reason,
>
when I click on the button that calls addMoney:, the value of the
>
float goes to like a 10 digit number. I am almost positive I have
>
everything right in my header file and nib, so I'm guessing it is
>
something in my .m file. Here is the code: (I haven't finished all of
>
the methods yet since the float won't work right). Any help would
>
greatly appreciated. Thanks in advance.
>
>
...
>
>
- (IBAction)addMoney:(id)sender
>
{
>
money = money + 5;
>
NSLog(@"Money = %d", money);
>
}
Assuming money is defined as a float in your header, this looks like the
problem. The format "%d" means that the coresponding argument is an int,
and it is to be converted to signed decimal notation. Most values of
floats, which interpreted as ints, look like huge values. Try using "%f"
instead.
The irony is that your program probably works otherwise; just the log is
making you think it's broken :)
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden