Hi Guys,
So, I am busy with Stanford's online courses (CS193P)
With the first assignment, we're tasked with adding a 'history display' to our calculator. The display must hold all digits pressed and retain them, until I press clear. (Clear was also a homework assignment)
Problem I have is:
If I type in : 44 + 99
This shows up on history display - but the main display will not clear the text after pressing the operation button.
So history display will show:
I'm using an instance variable "userIsinMiddleOfEnteringNumber" do tell the compiler when it should / should not append text strings.
Here is the code I have for CalculatorViewController.m
This is CalculatorViewController.h
Can someone please tell me what silly mistake I am making here?
So, I am busy with Stanford's online courses (CS193P)
With the first assignment, we're tasked with adding a 'history display' to our calculator. The display must hold all digits pressed and retain them, until I press clear. (Clear was also a homework assignment)
Problem I have is:
If I type in : 44 + 99
This shows up on history display - but the main display will not clear the text after pressing the operation button.
So history display will show:
Main display will show:44+99
4499
I'm using an instance variable "userIsinMiddleOfEnteringNumber" do tell the compiler when it should / should not append text strings.
Here is the code I have for CalculatorViewController.m
Code:
#import "CalculatorViewController.h"
@interface CalculatorViewController ()
@end
@implementation CalculatorViewController
//If brain is has not start it - start it - otherwise don't. (We only want, one copy of brain)
- (CalculatorBrain *)brain
{
if (!brain) brain = [ [CalculatorBrain alloc] init];
return brain;
}
//Implimentation for digipressed:
-(IBAction)digitPressed:(UIButton *)sender
{
//Get the current digit pressed
NSString *digit = sender.currentTitle;
//If user is in the middle of entering number - append the next number to current number
if (userIsInMiddleOfEnteringNumber) {
[display setText:[[display text] stringByAppendingFormat:digit]];
[historyDisplay setText:[[historyDisplay text] stringByAppendingFormat:digit]];
}
else //Don't append.
{
[display setText:digit];
[historyDisplay setText:digit];
userIsInMiddleOfEnteringNumber = YES;
}
}
-(IBAction)operationPressed: (UIButton *)sender
{
NSString *operationPressed = sender.currentTitle;
if (userIsInMiddleOfEnteringNumber)
{
[historyDisplay setText:[[historyDisplay text] stringByAppendingFormat:operationPressed]];
[[self brain ] setOperand:[[display text] doubleValue]];
}
else
{
[historyDisplay setText:operationPressed];
}
NSString *operation = sender.currentTitle;
double result = [[self brain] preformOperation:operation];
[display setText:[NSString stringWithFormat:@ "%g", result]];
}
- (IBAction)clearPressed
{
display.text = @"";
historyDisplay.text = @"";
brain = nil;
}
@end
This is CalculatorViewController.h
Code:
//
// CalculatorViewController.h
// Calculator
//
// Created by Robert Clegg on 2012/07/10.
// Copyright (c) 2012 __MyCompanyName__. All rights reserved.
//
#import <UIKit/UIKit.h>
#import "CalculatorBrain.h"
@interface CalculatorViewController : UIViewController
{
//To provide YES/NO answer.
BOOL userIsInMiddleOfEnteringNumber;
//Main display for calculator
IBOutlet UILabel *display;
//History Display (Number entrered)
IBOutlet UILabel *historyDisplay;
CalculatorBrain *brain; //** Connection to model**
}
//Method declerations for pressing numbers.
-(IBAction)digitPressed:(UIButton *)sender;
-(IBAction)operationPressed: (UIButton *)sender;
- (IBAction)clearPressed;
@end
Can someone please tell me what silly mistake I am making here?
Last edited: