Jump to content
  • Advertisement
Sign in to follow this  

Simple Objective C Error

This topic is 4012 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I've been programming in C++ and Java a lot the past few years and I'm now being required to learn objective c. So, I wrote a simple first app to just get my feet wet, but I get a "bus error" during runtime. Any ideas? main.m
#import <Foundation/Foundation.h>
#import "BMI.h"
#import <stdio.h>

int main (int argc, const char * argv[]) {
    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
	BMI *newBMI = [[BMI alloc]init];
	int inches;
	int weight;
	int userBMI;
	printf("What is your height in inches: ");
	scanf("%i", inches);
	[newBMI setHeight: inches];
	printf("\n\nWhat is you weight in pounds: ");
	scanf("%x", weight);
	[newBMI setWeight: weight];
	[newBMI calculateBMI];
	userBMI = [newBMI getBMI];
	printf("Your BMI is: %f");
	[newBMI release];
	[pool drain];
    return 0;

#import <Cocoa/Cocoa.h>
#import <Foundation/NSObject.h>

@interface BMI : NSObject {
	int height;
	int weight;
	int actualBMI;
 - (void) calculateBMI;
 - (int) getBMI;
 - (void) setHeight:(int) feet;
 - (void) setWeight:(int) lb;

#import "BMI.h"

@implementation BMI
 - (void) calculateBMI {
	actualBMI = (weight * 703) / (height * height);
- (int)  getBMI {
	return actualBMI;

 - (void) setHeight:(int) feet {
	height = feet;

- (void) setWeight:(int) lb {
	weight = lb;

I'm sure it's just something small, but I can't put my finger on it. Thanks James

Share this post

Link to post
Share on other sites
A "bus error" is usually a segmentation fault, access violation, etc. It looks like your scanf calls are the culprits:

scanf("%i", inches);

should be:

scanf("%i", &inches);

scanf takes a pointer to the variable to receive the value, therefore you must pass the address of the variable.

Share this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!