Mac Noob needing help with first "error-free" program

greg1067

macrumors newbie
Original poster
Nov 30, 2009
17
0
Hello to all. I am a very new xcode, objective-c programmer. I have done some limited programming in other languages. I have written my first, what I will call, successful program in xcode. By this, as the thread title suggest, I mean that there are no errors generated when I run the debugger.
First I will explain what I am trying to accomplish and then I will explain what the issue is.

The program goal:
Just to create a simple math processing program, I decided to write a cooking measurement conversion app. The first set of instructions-converting teaspoon into any of the other measurements goes fine. (first if statement) This works as it should

The issue:
After adding the second set of conversions-from tablespoon to the other measurements (second if statement) When I put a value in, it only calculates one conversion and then places a zero in the rest including the original value that I want to convert. If I do a block comment on either if statement it works correctly. I do not have the knowledge yet to see where my issue is.

Below is the code and sorry for being so long winded.

CMCcontroller.h file below

Code:
#import <Cocoa/Cocoa.h>


float tsptxt;
float tbstxt;
float qtstxt;
float pntstxt;
float oztxt;
float mltrtxt;
float ltrtxt;
float galtxt;
float cuptxt;




@interface CMCcontroller : NSObject {
    IBOutlet id cup;
    IBOutlet id gal;
    IBOutlet id ltr;
    IBOutlet id mltr;
    IBOutlet id oz;
    IBOutlet id pnts;
    IBOutlet id qts;
    IBOutlet id tbs;
    IBOutlet id tsp;
	
	
	

}
- (IBAction)calc:(id)sender;
@end
CMCcontroller.m file

Code:
#import "CMCcontroller.h"


@implementation CMCcontroller
- (IBAction)calc:(id)sender 

{
	float tsptemp = [tsp floatValue];
	float tbstemp = [tbs floatValue];
	
	if (tsp > 0) 
	{

		//THIS IS TO CONVERT TEASPOON TO TABLESPOON
				
		float tsp_tbsresult;
		tsp_tbsresult =  tsptemp / 3 ;
		tbstxt = tsp_tbsresult;
		[tbs setFloatValue:tbstxt];
		tsptemp = [tsp floatValue];

		
		//THIS IS TO CONVERT TEASPOON TO fl. oz.
		
		float tsp_ozresult;
		tsp_ozresult = tsptemp / 6;
		oztxt = tsp_ozresult;
		[oz setFloatValue: oztxt];
		
		//THIS IS TO CONVERT TEASPOON TO CUPS
		
		float tsp_cupresult;
		tsp_cupresult = tsptemp / 48;
		cuptxt = tsp_cupresult;
		[cup setFloatValue: cuptxt];
		
		//THIS IS TO CONVERT TEASPOON TO PINTS
		
		float tsp_pintresult;
		tsp_pintresult = tsptemp / 96;
		pntstxt = tsp_pintresult;
		[pnts setFloatValue: pntstxt];
		
		
		//THIS IS TO CONVERT TEASPOON TO QUART
		
		float tsp_qtsresult;
		tsp_qtsresult = tsptemp / 192;
		qtstxt = tsp_qtsresult;
		[qts setFloatValue: qtstxt];
		
		//THIS IS TO CONVERT TEASPOON TO GALLON
		
		float tsp_galresult;
		tsp_galresult = tsptemp / 768;
		galtxt = tsp_galresult;
		[gal setFloatValue: galtxt];
		
		//THIS IS TO CONVERT TEASPOON TO MILLILITER
		
		float tsp_mltrresult;
		tsp_mltrresult = tsptemp / .202884136;
		mltrtxt = tsp_mltrresult;
		[mltr setFloatValue: mltrtxt];
		
		//THIS IS TO CONVERT TEASPOON TO LITER
		
		float tsp_ltrresult;
		tsp_ltrresult = tsptemp / 202.884136;
		ltrtxt = tsp_ltrresult;
		[ltr setFloatValue: ltrtxt];
		
	}
	
	if (tbs > 0)
	{
		//THIS IS TO CONVERT TABLESPOON TO TEASPOON
		
		float tbs_tspresult;
		tbs_tspresult = tbstemp * 3;
		tsptxt = tbs_tspresult;
		[tsp setFloatValue: tsptxt];
		
		//THIS IS TO CONVERT TABLESPOON TO fl. oz
		
		float tbs_flozresult;
		tbs_flozresult = tbstemp / 2;
		oztxt = tbs_flozresult;
		[oz setFloatValue:oztxt];
		
								
		//THIS IS TO CONVERT TABLESPOON TO CUP
		
		float tbs_cupresult;
		tbs_cupresult = tbstemp / 16;
		cuptxt = tbs_cupresult;
		[cup setFloatValue:cuptxt];
		
		//THIS IS TO CONVERT TABLESPOON TO PINT
		float tbs_pntresult;
		tbs_pntresult = tbstemp / 32;
		pntstxt = tbs_pntresult;
		[pnts setFloatValue:pntstxt];
		
		//THIS IS TO CONVERT TABLESPOON TO QUART
		float tbs_qtsresult;
		tbs_qtsresult = tbstemp / 64;
		qtstxt = tbs_qtsresult;
		[qts setFloatValue:qtstxt];
		
		//THIS IS TO CONVERT TABLESPOON TO GALLON
		float tbs_galresult;
		tbs_galresult = tbstemp / 256;
		galtxt = tbs_galresult;
		[gal setFloatValue:galtxt];
		
		//THIS IS TO CONVERT TABLESPOON TO MILLILITER
		float tbs_mltrresult;
		tbs_mltrresult = tbstemp / 0.0676280454;
		mltrtxt = tbs_mltrresult;
		[mltr setFloatValue:mltrtxt];
		
		//THIS IS TO CONVERT TABLESPOON TO LITER
		float tbs_ltrresult;
		tbs_ltrresult = tbstemp / 67.6280454;
		ltrtxt = tbs_ltrresult;
		[ltr setFloatValue:ltrtxt];
	 
		
		
	}
	

	
	
	
    
}
@end
 

lee1210

macrumors 68040
Jan 10, 2005
3,182
1
Dallas, TX
Check that tbstemp > 0. and tsptemp > 0., right now you are comparing 0 to pointers, which is no good. Chances are both of your ifs are firing.

-Lee
 
Comment

greg1067

macrumors newbie
Original poster
Nov 30, 2009
17
0
Check that tbstemp > 0. and tsptemp > 0., right now you are comparing 0 to pointers, which is no good. Chances are both of your ifs are firing.

-Lee
Thank you Lee! Both for seeing my error and the quick response. I didn't think that the "temp" variables would get populated before it ran the if statements. But I guess you are right that they are just pointers in the UI rather than actual variables. This is a little different from doing the little Visual C# programming I have done, where the textbox is actually the variable name as well.

Hopefully everyone here will have patience with me as I make my baby steps towards full-fledged Mac and iPhone apps.
 
Comment
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.