0

I followed a tutorial to make an iOS Calculator, and I am now wanting to add a decimal place button to it. Here is my code

header file:

#import <UIKit/UIKit.h>

@interface CalcViewController : UIViewController {

float result;
float selectedNumber;

int selectedOperation;

IBOutlet UILabel *calcScreen;
}

-(IBAction)numberPressed:(id)sender;
-(IBAction)operationPressed:(id)sender;
-(IBAction)clearNumber:(id)sender;
-(IBAction)clearOperation:(id)sender;

@end

Here is my implementation file

#import "CalcViewController.h"

@implementation CalcViewController

-(IBAction)numberPressed:(id)sender {
selectedNumber = selectedNumber * 10 + (float) [sender tag];
calcScreen.text = [NSString stringWithFormat:@"%2g", selectedNumber];
}

-(IBAction)operationPressed:(id)sender {
if (selectedOperation == 0) {
    result = selectedNumber;

} else {
    switch (selectedOperation) {
        case 1:
            result = result + selectedNumber;
            break;
        case 2:
            result = result - selectedNumber;
            break;
        case 3:
            result = result * selectedNumber;
            break;
        case 4:
            result = result / selectedNumber;
            break;
        case 5:
            selectedOperation = 0;
            break;



    }
}
selectedNumber = 0; 
calcScreen.text = [NSString stringWithFormat:@"%2g", result];
if ([sender tag] == 0) result = 0;
selectedOperation = [sender tag];

} 


-(IBAction)clearNumber:(id)sender {
selectedNumber = 0;
calcScreen.text = @"0";
}

-(IBAction)clearOperation:(id)sender {
selectedNumber = 0;
calcScreen.text = @"0";
selectedOperation = 0;
}

I saw on another thread that someone had suggesting using the method:

- (IBAction)Decimal:(id)sender 
{
 NSString *currentText = calcScreen.text;
 if ([currentText rangeOfString:@"." options:NSBackwardsSearch].length == 0) {
    calcScreen.text = [calcScreen.text stringByAppendingString:@"."];  
}

However this did not seem to work for me. It would indeed let me enter a decimal point, but when a second number was entered the decimal point would be lost. Can anyone suggest a method I could implement to achieve what I am trying to?

Cheers in advance!

George
  • 23
  • 5

2 Answers2

0

My quick thought: keep the number as text on the screen, adding additional digits and/or decimal point to it as text. Only convert it to a float (I would use double myself, btw) at the moment that an operation is performed. The suggested method should then work fine.

Something like:

-(IBAction)numberPressed:(id)sender {
    calcScreen.text = [calcScreen.text stringByAppendingString:[sender tag]];
}

-(IBAction)operationPressed:(id)sender {
    selectedNumber = [calcScreen.text floatValue];
    ...
}
fishinear
  • 6,101
  • 3
  • 36
  • 84
  • Is there any chance you could perhaps write up some code demonstrating what you mean? – George Feb 15 '12 at 15:10
  • Added code example. I have not tested it, so it is probably not entirely correct. – fishinear Feb 15 '12 at 15:29
  • Thanks, but unfortunately it is creating an error which is causing the app to crash. The line calcScreen.text = [calcScreen.text stringByAppendingString: [sender tag]]; Is sending the error: Incompatible integer to pointer conversion sending 'NSInteger' (aka 'int) to parameter of type 'NSString *' Any thoughts? – George Feb 15 '12 at 15:46
  • Mmmm.... apparently [sender tag] is an integer, while we use it to build up a string. what do you think you may be able to do about that? – fishinear Feb 16 '12 at 11:02
0

One quick tip, instead of using int selectedOperation;... and later in code

switch (selectedOperation) {
    case 1:
        result = result + selectedNumber;
        break;
    case 2:
        result = result - selectedNumber;
    ...

Try using typedef enum

    typedef enum {
    OperationTypeAdd = 0,
    OperationTypeSub,
    ...
    } OperationType

and in switch block

    switch (operationType) {
    case OperationTypeAdd:
        result = result + selectedNumber;
        break;
    case operationTypeSub:
        result = result - selectedNumber;
        break;
pawelropa
  • 1,409
  • 1
  • 14
  • 20
  • Where do I declare the typedef enum? – George Feb 16 '12 at 09:37
  • You declare the enum type in your header file. In your case it will be `CalcViewController.h` before the `@interface CalcViewController`, and than later instead `int selectedOperation;` you write OperationType selectedOperation. Read something about enum types [here](http://stackoverflow.com/questions/707512/what-is-a-typedef-enum-in-objective-c). I've read you have problem converting integer to string, try using something like `[NSString stringWithFormat:@"%i", intValue];`@George – pawelropa Feb 16 '12 at 18:14