Amazon.com Widgets Chapter 6, Exercise 4 Help
Welcome, Guest. Please login or register.
Did you miss your activation email?
October 20, 2014, 06:02:51 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 6 - Making Decisions
| | | |-+ Chapter 6, Exercise 4 Help
Pages: [1] Go Down
Print
Author Topic: Chapter 6, Exercise 4 Help (Read 1242 times)
lukenewman
Newbie
*
Posts: 7






on: September 22, 2011, 07:25:59 PM

I can get most things working, but when I try dividing two integers, I just get 1 or 0. I understand what casting does but I just don't know where to do it. Also, anything else that needs fixing would be greatly appreciated.

Here's my code (I named it indieCalculator because he made us do it on our own!!!):


#import <Foundation/Foundation.h>

@interface indieCalculator: NSObject
{
    double accumulator;
}

- (void) setAccumulator: (double) number;
- (void) clear;
- (double) print;
- (void) add;
- (void) subtract;
- (void) multiply;
- (void) divide;
@end

@implementation indieCalculator
- (void) setAccumulator: (double) number
{
    accumulator = number;
}

- (void) clear
{
    accumulator = 0;
}

- (double) print
{
    return accumulator;
}

- (void) add: (double) number
{
    accumulator += number;
}

- (void) subtract: (double) number
{
    accumulator -= number;
}

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

- (void) divide: (double) number
{
    if (number != 0)
        accumulator /= number;
    else {
        NSLog(@"Division by zero.");
        accumulator = NAN;
    }
}
@end

int main (int argc, const char * argv[])
{

    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
    indieCalculator *calc = [[indieCalculator alloc] init];
   
    char operation;
    double number;
   
    while (operation != 'E') {
        NSLog(@"Please enter your calculation:");
        scanf("%f %c", &number, &operation);
        [calc setAccumulator: number];
        switch (operation) {
            case '+':
                [calc add: number];
                NSLog(@"= %f", [calc print]);
                break;
            case '-':
                [calc subtract: number];
                NSLog(@"= %f", [calc print]);
                break;
            case '*':
                [calc multiply: number];
                NSLog(@"= %f", [calc print]);
                break;
            case '/':
                [calc divide: number];
                NSLog(@"= %f", [calc print]);
                break;
            case 'S':
                [calc setAccumulator: number];
                NSLog(@"= %f", [calc print]);
                break;
            case 'E':
            default:
                NSLog(@"Unknown operator. Only accepts + , - , * , / , S , and E.");
                break;
        }
    }
    [calc release];

    [pool drain];
    return 0;
}
Logged
skochan
Administrator
Hero Member
*****
Posts: 3114







Reply #1 on: September 23, 2011, 07:05:53 AM

You need to use %lf (that's the letter l) when reading in a double value with scanf (technically, you should also be using %lf format with NSLog to display the value of a double as well).

Cheers,

Steve
Logged
lukenewman
Newbie
*
Posts: 7






Reply #2 on: September 24, 2011, 10:57:18 AM

Thanks for the tips, but whenever I divide, it gives me wrong answers. For example, I set the accumulator to 15 and then divide by 5 and it gives me 1. Where do I need to insert casting to make sure this doesn't happen?
Logged
mitchb
Full Member
***
Posts: 143






Reply #3 on: October 02, 2011, 10:48:25 PM

The [calc setAccumulator: number]; statement right after the scanf needs to be deleted.  consider entering a number of 10 and then / in the scanf statement. The next statement sets the accumulator to 10 then at the case '/' statement the accumulator (10) will be divided by the number 10 which will equal 1.

Mitch
Logged

If you give a man a program, you will frustrate him for a day;
If you teach him how to program, you will frustrate him for a lifetime;
     - Anonymous
Soonryidono
Newbie
*
Posts: 4

168544312 ryyvrfrrrrwr@gmail.com
WWW




Reply #4 on: June 22, 2012, 08:20:18 AM

Hi Ron:
I have a question on your 2nd edition book page 98 of chapter4 where talking about "Add" basic image manipuation.
Does the O = I x 0.2 could be O = 10.2 while we talking the add function?
If not, it must be some where that I mis-understanding...


Best
Jimmy
P.s. !!! !!!!
!! ! :(   :(
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.