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

PriapusZA

macrumors 6502a
Original poster
Oct 21, 2011
677
1
England
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:
Main 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

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:
Hi guys,

Okay, so after posting this code, I went back to the project and went through the code step by step. I also, looked at the walkthrough and realized that one part of the code was not needed and that I had set the history display to behave the same way as my main display - only difference was that main display was not detecting :userIsInMiddleOfEnteringNumber.

So, I made the following changes (See code in red) in my implementation file:

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]]; 
        
    }
    else //Don't append. 
    {
        [display setText:digit];
        userIsInMiddleOfEnteringNumber = YES;
    }
 [COLOR="Red"]   [historyDisplay setText:[[historyDisplay text] stringByAppendingFormat:digit]]; [/COLOR]

}


-(IBAction)operationPressed: (UIButton *)sender

{    
    
    NSString *operationPressed = sender.currentTitle;   
    if (userIsInMiddleOfEnteringNumber)
    {
       [historyDisplay setText:[[historyDisplay text] stringByAppendingFormat:operationPressed]]; 
         
        [[self brain ] setOperand:[[display text] doubleValue]];
[COLOR="red"]        userIsInMiddleOfEnteringNumber = NO; [/COLOR]
    
    }

    
    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

Now both displays behave as they are intend to. :cool:

(P.S: Im starting to have fun with code!! ) :D
 
Yes, thank you. Working as I expect it.

Now if I can just figure out how to do back space and adding a period button, I'm good to go!
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.