Advertise Mobile SDKs Books Events Forum News Social Networking Support Us
Follow @iphonedevsdk on Twitter

Mockup & CodeGen, iPhone & iPad
($9.99)

Make your own iPhone apps
and run them live!
(free)

Manu
($0.99)

Want your application or service advertised on iPhone Dev SDK?

Go Back   iPhone Dev SDK Forum > Mac OS X Development Forums > Objective-C, Python, Ruby Development

Reply
 
LinkBack Thread Tools Display Modes
Old 03-19-2010, 03:22 PM   #1 (permalink)
Registered Member
 
Join Date: Jun 2009
Posts: 3
Unhappy floating point exception occurs...Why??? I'm using integers

Hi, all

Instead of posting my entire code, I'll just post the snippet where the error pops up. I appreciate your responses.

Here is my main code:

#import <Foundation/Foundation.h>

int main (int argc, const char * argv[]) {
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
GasMileage *myGas = [[GasMileage alloc]init];
int miles = 0;

//grabbing the value stored at the address of miles variable
int *milepointer= &miles;


//Obtaining user input
NSLog(@"How many miles did you drive?");
scanf ("%i", &miles);

//setting user input values
[myGas setmilesdriven: *milepointer];

//calculate mpg
[myGas calcmpg];

[pool drain];
return 0;
}

Here's the snippet of code where the error pops up:

@implementation GasMileage

-(void) calcmpg
{

//calculating mpg
int gallons = 0;
int *gallonpointer = &gallons;
*gallonpointer = gallonsused;
NSLog(@"How many gallons did you use?");
scanf ("%i", &gallons);

int total;
total = milesdriven/gallonsused;
NSLog(@"Your mpg: %i", total);

//I did not paste remaining code for this method call

Here's my output from Terminal:

Last login: Fri Mar 19 11:48:54 on ttys001
karen-haqs-imac:~ karenhaq$ /Users/karenhaq/Objective-C/GasMileage/build/Debug/GasMileage ; exit;
2010-03-19 11:52:10.895 GasMileage[55145:10b] How many miles did you drive?
40
2010-03-19 11:52:21.721 GasMileage[55145:10b] How many gallons did you use?
10
Floating point exception
logout

[Process completed]
KinAz is offline   Reply With Quote
Old 03-19-2010, 10:32 PM   #2 (permalink)
Registered Member
 
Join Date: Jun 2009
Posts: 3
Red face Nevermind...

I figured out my error. How embarrassing, it was a simple dereferencing pointer issue.
KinAz is offline   Reply With Quote
Old 03-23-2010, 07:25 AM   #3 (permalink)
Maker of Games
 
Mr Jack's Avatar
 
Join Date: Nov 2009
Location: Coventry, UK
Posts: 395
Default

Code:
int gallons = 0;
int *gallonpointer = &gallons;
*gallonpointer = gallonsused;
Why not just do int gallons = gallonsused;? Faster, less code, and less error prone.
__________________


Visit Mr Jack Games for my blog and more about my games
Mr Jack is offline   Reply With Quote
Old 03-23-2010, 12:31 PM   #4 (permalink)
Registered Member
 
Join Date: Jun 2009
Posts: 3
Default Oh, thanks for your help!

I need to focus at cleaning up the code before the final release.
KinAz is offline   Reply With Quote
Reply

Bookmarks

Tags
floating point

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



» Advertisements
» Online Users: 275
23 members and 252 guests
ADY, AragornSG, Bertrand21, Dani77, Dattee, fkmtc, HDshot, HemiMG, iDifferent, jakerocheleau, JasonR, jimbo, macquitzon216, mer10, prchn4christ, Rudy, sacha1996, silverwiz, sneaky, spiderguy84, Sunny46, theone8one
Most users ever online was 1,187, 10-11-2011 at 08:09 AM.
» Stats
Members: 158,885
Threads: 89,230
Posts: 380,767
Top Poster: BrianSlick (7,129)
Welcome to our newest member, bookesp
Powered by vBadvanced CMPS v3.1.0

All times are GMT -5. The time now is 03:07 PM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0