0

Am running this code but am getting wrong console output. Should be getting:

Converting 100.00 US dollars into foreign currency leaves $900.00
Charging 100.00 in foreign currency leaves $775.00 Converting 100.00 US dollars into foreign currency leaves
$1900.00
Charging 100.00 in foreign currency leaves $1750.00

But am only getting:

Converting 100.00 US dollars into foreign currency leaves $900.00

Here is the code:

#import <Foundation/Foundation.h>

@interface Budget:NSObject {
    float exchangeRate;
    double budget;
    double exchangeTransaction;
}

- (void) createBudget:(double)aBudget withExchangeRate:(float) anExchangeRate;

- (void) spendDollars:(double)dollars;

- (void) chargeForeignCurrency:(double)foreignCurrency;

@end

@implementation Budget

- (void) createBudget:(double)aBudget withExchangeRate:(float) anExchangeRate {
    exchangeRate = anExchangeRate;
    budget = aBudget;
}

- (void) spendDollars:(double)dollars {
    budget -= dollars;
    NSLog(@"Converting %.2f US dollars into foreign currency leaves $%.2f", dollars, budget);
}

-(void)chargeForeignCurrency:(double)foreignCurrency {
    exchangeTransaction = foreignCurrency * exchangeRate;
    budget -= exchangeTransaction;
    NSLog(@"Charging %.2f in foreign currency leaves $%.2f", foreignCurrency, budget);
}



@end

int main(int argc, const char * argv[])
{
    double numberDollarsInEuroland = 100;
    double numberEuros = 100;
    double numberDollarsInPoundland = 100;
    double numberPounds = 100;

    Budget *europeBudget = [Budget new];
    [europeBudget createBudget:1000.00 withExchangeRate:1.2500];
    [europeBudget spendDollars:numberDollarsInEuroland];
    [europeBudget chargeForeignCurrency:numberEuros];

    Budget *englandBudget = [Budget new];
    [englandBudget createBudget:2000.00 withExchangeRate:1.5000];
    [englandBudget spendDollars:numberDollarsInPoundland];
    [englandBudget chargeForeignCurrency:numberPounds];



    return 0;
}

What's wrong? Thanks in advance.

pdenlinger
  • 3,897
  • 10
  • 60
  • 92
  • I don't see anything wrong. I compiled and ran it and got exactly what you were expecting. – Brendon Cheves Apr 14 '12 at 17:50
  • This is my output: Converting 100.00 US dollars into foreign currency leaves $900.00 Charging 100.00 in foreign currency leaves $775.00 Converting 100.00 US dollars into foreign currency leaves $1900.00 Charging 100.00 in foreign currency leaves $1750.00 – Brendon Cheves Apr 14 '12 at 17:52
  • Very strange. I tried your code and every work fine. Output I have as you want. – RomanHouse Apr 14 '12 at 18:37
  • Found the problem: came out fine but didn't see it all in the console. DOH! Thank you! – pdenlinger Apr 14 '12 at 19:59

1 Answers1

0

Just a guess, but is this code in a command line tool? If so, check maybe this is your problem.

Community
  • 1
  • 1
Jay Wardell
  • 2,230
  • 1
  • 14
  • 13