Hi there, me again! So right now, I'm trying to create a program that will, based on user input, calculate the density (which includes mass and volume) of an object.
The problem is that there's something wrong with my code, and I'm not sure what...whenever I run the program it stops after I enter the mass and it puts some text in the console, and then asks for my password (entering my password doesn't fix the problem) It seems like a pretty basic program, so could someone help me out? Thsanks!
@interface:
@implementation:
main:
(don't ask why I named the class Calculator_Methods...I was being a little literal
)
Thanks again!
-iMaster
The problem is that there's something wrong with my code, and I'm not sure what...whenever I run the program it stops after I enter the mass and it puts some text in the console, and then asks for my password (entering my password doesn't fix the problem) It seems like a pretty basic program, so could someone help me out? Thsanks!
@interface:
Code:
#import <Foundation/Foundation.h>
@interface Calculator_Methods : NSObject {
int volume;
int mass;
int density;
}
@property int volume, mass, density;
-(void) regObjCalc;
-(void) irregObjCalc;
-(void) densityCalc;
@end
@implementation:
Code:
#import "Calculator Methods.h"
@implementation Calculator_Methods
@synthesize volume, mass, density;
//Calculates the mass of a regular object
-(void) regObjCalc
{
int length, height, width;
NSLog(@"Please enter mass:");
scanf("%i", mass);
NSLog(@"Please enter length:");
scanf("%i", length);
NSLog(@"And height:");
scanf("%i", height);
NSLog(@"And width:");
scanf("%i", width);
volume = length * width * height;
}
//Calculates the mass of an irregular object
-(void) irregObjCalc
{
int startHeight, endHeight;
NSLog(@"Please enter mass:");
scanf("%i", mass);
NSLog(@"Please enter the starting height of the water:");
scanf("%i", startHeight);
NSLog(@"Please enter the ending height of the water:");
scanf("%i", endHeight);
volume = endHeight - startHeight;
}
-(void) densityCalc
{
density = mass / volume;
NSLog(@"Density = mass/volume\n Mass = %i\nVolume = %i\n Density = %i/%i\n%i/%i = %i\nDensity = %i g/mL", mass, volume, mass, volume, mass, volume, density, density);
}
@end
main:
Code:
#import "Calculator Methods.h"
int main (int argc, const char * argv[]) {
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
Calculator_Methods *myObject = [[Calculator_Methods alloc] init];
char usrInput;
NSLog(@"Please enter'a' to find the density of an irregular object and 'b' to ifnd the density of a regular object");
scanf("%c", &usrInput);
switch (usrInput) {
case 'a':
[myObject irregObjCalc];
break;
case 'b':
[myObject regObjCalc];
break;
}
[myObject densityCalc];
[pool drain];
return 0;
}
(don't ask why I named the class Calculator_Methods...I was being a little literal
Thanks again!
-iMaster