Amazon.com Widgets exercise 6.4
Welcome, Guest. Please login or register.
Did you miss your activation email?
May 20, 2013, 06:44:30 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
| |-+  Help!!!
| | |-+  exercise 6.4
Pages: [1]   Go Down
Print
Author Topic: exercise 6.4  (Read 420 times)
dharr19
Full Member
***
Posts: 175


Email




« on: July 11, 2011, 06:54:32 PM »

Hi Stephen,

I am working practice exercise 6.4.  If I type in an 'E' or 'e' it still goes thru the switch statement.  Here is part of the program that does the work in blue.  And below this is the output in orange

    NSLog(@"Type in your expression.");
    scanf("%lf %c", &value, &operator);
   
    [deskcalc setAccumulator:value];
   
    while (operator != 'E' || operator != 'e')
    {
        switch ( operator )
        {
            case '+':
               [deskcalc add:value];
                break;
            case '-':
               [deskcalc subtract:value];
               break;
            case '*':
            case 'x':
               [deskcalc multiply:value];
               break;
            case '/':
               [deskcalc divide:value];
               break;
            case 'S':
            case 's':
                [deskcalc setAccumulator:value];
                break;
            default:
               Error = 1;
               NSLog(@"Unknown operator %c.", operator);
               break;
        }
   
        NSLog(@"%f", [deskcalc accumulator]);       
        scanf("%lf %c", &value, &operator);
    }
   
    NSLog(@"%lf %c End of program = ", [deskcalc accumulator], operator);
    NSLog(@"End of calculations.");
   
   2011-07-11 12:59:08.377 Calculator[1383:903] Type in your expression.
    0 E
    2011-07-11 12:59:16.250 Calculator[1383:903] Unknown operator E.
    2011-07-11 12:59:16.250 Calculator[1383:903] 0.000000
Logged
skochan
Administrator
Hero Member
*****
Posts: 3103







« Reply #1 on: July 11, 2011, 06:57:19 PM »

Look at the logic in your while statement.

Cheers,

Steve
Logged
dharr19
Full Member
***
Posts: 175


Email




« Reply #2 on: July 11, 2011, 08:06:49 PM »

If I write the statement as follows it works fine

while (operator != 'E')

but if I do the following it won't

while ((operator != 'E') || (operator != 'e'))
Logged
skochan
Administrator
Hero Member
*****
Posts: 3103







« Reply #3 on: July 11, 2011, 08:28:08 PM »

You want to use a logical AND and write the expression like this:

Code: (Objective-C)
while (operator != 'E' && operator != 'e')
   ...

Cheers,

Steve
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.