Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

TotalLuck

macrumors newbie
Original poster
Jan 3, 2009
21
0
Moreno Vallley
Hi and thanks for reading.

consider me a total Noob to programming. ( I did some basic for a few years back in 84 )

I am going through the "Programming in Obj. C" by Kochan. in chapter 6 program 6.7 and 6.8 are calculator samples programs.

when i enter the programs into Xcode (leopard 10.5), the program will not work unless i specifically enter a value with 2 decimal places ie: 10.44 + 4.44
if i enter it the way it wants it executes correctly on all parts. xcode complies without any errors or warning.

if i don't then the program does nothing and doesn't advance at all.
i have looked at my code and the code in the book and cant figure out what i am doing wrong.

So my question is: how can i get the program to accept any number accepted regardless of the decimal places?

here is my code:
Code:
#import <objc/Object.h>
#import <stdio.h>

@interface Calculator: Object
{
	double accumulator;
	double memory;
}

// accumulator methods
-(void) setAccumulator: (double) value;
-(void) clear;
-(double) accumulator;

// arithmitic functions
-(void) add: (double) value; 
-(void) subtract: (double) value;
-(void) multiply: (double) value;
-(void) divide: (double) value;
-(double) changeSign;
-(double) reciprocal; r
-(double) xSquared;

// memory functions
-(double) memoryClear;  
-(double) memoryStore;  
-(double) memoryRecall; 
-(double) memoryAdd;  
-(double) memorySubtract; 
-(double) memory; 

	

@end

@implementation Calculator;

-(void) setAccumulator: (double) value
{
	accumulator = value;
	
}

-(void) clear
{
	accumulator = 0;
}

-(double) accumulator

{
	return accumulator;

}


-(void) add: (double) value
{

	accumulator += value;
	
}

-(void) subtract: (double) value
{
	
	accumulator -= value;

}

-(void) multiply: (double) value
{
	
	accumulator *= value;
	
}

-(void) divide: (double) value
{	
	
	accumulator /= value;

}

-(double) changeSign
// change sign of accumulator
{
	
	accumulator = -accumulator;
	return accumulator;
}

-(double) reciprocal
// 1/accumulator
{
	
	accumulator = 1/accumulator;
	return accumulator;
}

-(double) xSquared
{
	return accumulator = accumulator * accumulator;
}

-(double) memory
{
	return memory;
}

-(double) memoryClear 
{
	return  memory = 0;
	
}

-(double) memoryStore 

{
	return memory = accumulator;		
}


-(double) memoryRecall
{
	
	return memory;
	
}
-(double) memoryAdd 
{
	return accumulator = memory + accumulator;

}


-(double) memorySubtract 
{
	return accumulator = memory - accumulator;
	
}



@end



int main(int argc, char *argv[])
{
	
	double		value1, value2;
	char		operator;
	
	Calculator	*deskCalc  =	[[ Calculator alloc] init];
	
	printf("type in your expression:\n");
	scanf("%lf %c %lf ", &value1, &operator, &value2);
	
	[deskCalc	clear];
	[deskCalc setAccumulator: value1];
	
	if ( operator == '+' )
		[deskCalc add: value2];
	else if (operator == '-')
		[deskCalc subtract: value2];
	else if (operator == '*')
		[deskCalc multiply: value2];
	else  if (operator == '/')
			if ( value2 == 0)
				printf("Division by  0!!\n");
			else
				[deskCalc divide:value2];
	else
		printf(" Unknown Operator!\n");
			
	printf("%.2f\n", [deskCalc accumulator]);
	
	[deskCalc free];
	
	return 0;
	
	
}


Thank you for any help you can offer,
Greg
 

HiRez

macrumors 603
Jan 6, 2004
6,265
2,630
Western US
I don't know but the first thing I would suspect is scanf. I've had lots of scanf (and other standard C input functions) issues on OS X. Stuff gets left in the input buffer and it's just generally flakey, not even behaving the same way with identical code and input on successive runs of a program. Here's a function I wrote to flush the input buffer (pass stdin to it):

Code:
/* ---- Flush the input buffer. Use after calling scanf(). ---- */
void flushInput(FILE *fp) {
	while((fgetc(fp)) != '\n' && (fgetc(fp)) != EOF);
}

What happens if you print the values of your inputs directly after taking them? Maybe try it with 3 separate statements instead of one scanf(). I know I've had some problems with character input in scanf, although it generally works OK for numbers.
 

TotalLuck

macrumors newbie
Original poster
Jan 3, 2009
21
0
Moreno Vallley
geeez

Thanks mac,

ugh

that was it. works fine now.

i didn't think that the extra space would matter.

ok... back to the book.

and

happy new year
 

TotalLuck

macrumors newbie
Original poster
Jan 3, 2009
21
0
Moreno Vallley
Scanf()

thanks Hirez,

I was going to wait for the new kochan book to come out that is more xcode friendly but i found the old book in a 1/2 off bin.

i already ordered the new one from amazon.

i am really a total noob so scanf() is what i got for now.

i am totally amazed that this forum is so helpful to new people.

thank a lot and i am sure i will be back for more help.

Greg
 

HiRez

macrumors 603
Jan 6, 2004
6,265
2,630
Western US
Take a look at this for scanf. It might give you some better insight into it.

http://c-faq.com/stdio/scanfprobs.html

Yeah. That covers a lot of my frustration. Unfortunately, there are no really great solutions to the problem, especially if you want or need to use strict ANSI C (ie. not using a third-party library for input, of which there are a few). It's annoying that ANSI didn't fix all these ugly problems when they update the ANSI C spec every decade or so. they did it in '99 and I think there's a new one coming out soon(ish?). Why can't they rewrite these unholy monstrosities and put them into the standard library? Sure, it'll take years for compilers to catch up to be compatible (just as with all the ANSI spec), but you have to start somewhere and the sooner the better, I say.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.