Advertise here




Advertise here

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Sign In with Google Sign In with OpenID
Please do not post the same thing multiple times. The board software automatically flags certain posts as needing moderator attention. This happens the most often for new users. I'm pretty sure this is made clear at the time you attempt to post. Posting the same thing over and over again just makes that many more posts the moderators have to weed through later. This makes us sad. Don't make us sad. If your post/thread doesn't appear, just wait a while. Don't post it again. If it hasn't shown up by the next day, then you can try again. I normally go through posts in the mornings, and try to check a few times throughout the day, but I'm not here 24/7. There will typically be a significant delay before posts are approved. Just be patient.

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

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]
Post edited by KinAz on

Replies

Sign In or Register to comment.