Amazon.com Widgets Fraction Calculator result keeps equaling zero
Welcome, Guest. Please login or register.
Did you miss your activation email?
November 01, 2014, 12:28:42 AM
Home Help Search chat Login Register 
News: Read this please.The Great Kangaroo Escape Looking for reviews of the 4th ed on Amazon!   Twitter:  @skochan
                     

+ Official Forum for Programming in Objective-C (the iPhone Programming Language) - Stephen Kochan
|-+ Old Stuff
| |-+ Chapter Study
| | |-+ Chapter 21 - Writing iPhone Applications
| | | |-+ Fraction Calculator result keeps equaling zero
Pages: [1] Go Down
Print
Author Topic: Fraction Calculator result keeps equaling zero (Read 2169 times)
benne199
Newbie
*
Posts: 17


Email




on: August 08, 2011, 01:27:19 PM

Ok so I keep getting this error in the debugging terminal and the problem is that whatever I write it keeps equaling 0, when I press the equals button.

GNU gdb 6.3.50-20050815 (Apple version gdb-1705) (Fri Jul  1 10:50:06 UTC 2011)
Copyright 2004 Free Software Foundation, Inc.
GDB is free software, covered by the GNU General Public License, and you are
welcome to change it and/or distribute copies of it under certain conditions.
Type "show copying" to see the conditions.
There is absolutely no warranty for GDB.  Type "show warranty" for details.
This GDB was configured as "x86_64-apple-darwin".Attaching to process 12213.
Fraction_Calculator(12213,0xad2582c0) malloc: *** error for object 0x4b88f20: pointer being freed was not allocated
*** set a breakpoint in malloc_error_break to debug

myCalc is the only one released in the dealloc method so I don't get what it is trying to release

Here is the code please provide some pointers if you recognize the error, or else I guess I will just have to start from scratch and try to run through the whole code with the book again:

Thanks!

Code: (Objective-C)
#import <UIKit/UIKit.h>
#import "Calculator.h"

@interface Fraction_CalculatorViewController : UIViewController {
    UILabel     *display;
    char        op;
   
    NSMutableString *displayString;
    int         currentNumber;
    BOOL        firstOperand, isNumerator;
    Calculator  *myCalc;
}

@property (nonatomic, retain) IBOutlet UILabel *display;
@property (nonatomic, retain) IBOutlet NSMutableString *displayString;

-(void) ProcessDigit: (int) digit;
-(void) ProcessOperator: (char) theOp;
-(void) storeFracPart;

// Numeric keys
 
-(IBAction) clickDigit: (id) sender;

// Operator keys

-(IBAction) clickPlus: (id) sender;
-(IBAction) clickMinus: (id) sender;
-(IBAction) clickMultiply: (id) sender;
-(IBAction) clickDivide: (id) sender;

// Misc. keys

-(IBAction) clickEquals: (id) sender;
-(IBAction) clickClear: (id) sender;
-(IBAction) clickOver: (id) sender;

@end

Code: (Objective-C)
#import "Fraction_CalculatorViewController.h"

@implementation Fraction_CalculatorViewController

@synthesize displayString, display;

- (void)didReceiveMemoryWarning
{
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
   
    // Release any cached data, images, etc that aren't in use.
}

#pragma mark - View lifecycle

// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad
{
    isNumerator = YES;
    firstOperand = YES;
    self.displayString = [NSMutableString stringWithCapacity: 40];
    myCalc = [[Calculator alloc] init];
    [super viewDidLoad];
}

-(void) ProcessDigit: (int) digit
{
    currentNumber = currentNumber * 10 + digit;
   
    [displayString appendString: [NSString stringWithFormat: @"%i", digit]];
    [display setText: displayString];
}

// Numeric keys
-(IBAction) clickDigit: (id) sender
{
    int digit = [sender tag];
   
    [self ProcessDigit: digit];
}

-(void) ProcessOperator: (char) theOp
{
    NSString *opStr;
   
    op = theOp;
   
    switch (theOp) {
        case '+':
            opStr = @" + ";
            break;
        case '-':
            opStr = @" - ";
            break;
        case '*':
            opStr = @" ";
            break;
        case '/':
            opStr = @" ";
            break;
    }
   
    [self storeFracPart];
    [displayString appendString: opStr];
    [display setText: displayString];
}

-(void) storeFracPart
{
    if (firstOperand) {
        if (isNumerator) {
            myCalc.operand1.numerator = currentNumber;
            myCalc.operand1.denominator = 1; // e.g 3 4/5
        }
        else
            myCalc.operand1.denominator = currentNumber;
    }
    else if (isNumerator) {
        myCalc.operand2.numerator = currentNumber;
        myCalc.operand2.denominator = 1; // e.g 3 4/5
        }
    else {
        myCalc.operand2.denominator = currentNumber;
        firstOperand = YES;
    }
   
    currentNumber = 0;
}

-(IBAction) clickOver: (id) sender
{
    [self storeFracPart];
    isNumerator = NO;
    [displayString appendString: @"/"];
    [display setText: displayString];
}

// Arithmetic operation keys

-(IBAction) clickPlus: (id) sender
{
    [self ProcessOperator: '+'];
}

-(IBAction) clickMinus: (id) sender
{
    [self ProcessOperator: '-'];
}

-(IBAction) clickMultiply: (id) sender
{
    [self ProcessOperator: '*'];
}

-(IBAction) clickDivide: (id) sender
{
    [self ProcessOperator: '/'];
}

// Misc keys

-(IBAction) clickEquals: (id) sender
{
    [self storeFracPart];
    [myCalc performOperation: op];
   
    [displayString appendString: @" = "];
    [displayString appendString: [myCalc.accumulator convertToString]];
    [display setText: displayString];
   
    currentNumber = 0;
    isNumerator = YES;
    firstOperand = YES;
    [displayString setString: @""];
}

-(IBAction) clickClear: (id) sender
{
    currentNumber = 0;
    isNumerator = YES;
    firstOperand = YES;
    [myCalc clear];
   
    [displayString setString: @""];
    [display setText: displayString];
}


- (void)dealloc
{
    [myCalc release];
    [super dealloc];
}

- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

@end
Logged
benne199
Newbie
*
Posts: 17


Email




Reply #1 on: August 09, 2011, 03:08:40 AM

I went through the entire code and it was all due to typos. Is debugging always this painful?
Logged
skochan
Administrator
Hero Member
*****
Posts: 3114







Reply #2 on: August 09, 2011, 09:47:00 AM

Smiley
Logged
Pages: [1] Go Up
Print
Jump to:



Login with username, password and session length

Powered by MySQL Powered by PHP Powered by SMF 1.1.11 | SMF © 2006-2009, Simple Machines LLC Valid XHTML 1.0! Valid CSS!
Entire forum contents (c) 2009 classroomM.com. All rights reserved.