Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4332718
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T10:17:42+00:00 2026-05-21T10:17:42+00:00

I am new to Objective C. Currently, I am trying out some examples in

  • 0

I am new to Objective C. Currently, I am trying out some examples in Objective C. I am not getting the correct output. I could not understand the logic behind the correct output which I have included here.

main.m

#import <Foundation/Foundation.h>
#import "BudObj.h"
#import "Transaction.h"

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

        Budget* europeBudget=[Budget new];
        NSMutableArray *transactions=[[NSMutableArray alloc] initWithCapacity:10];

        [europeBudget createBudget:1000.00 withExchangeRate:1.2500];

        Transaction* aTransaction;
        aTransaction = [Transaction new];
        for(int n=1;n<2;n++){

                [aTransaction createTransaction:n*100 ofType:credit];
                [transactions addObject:aTransaction];

        }

        int n=1;
        while (n<3) {

                [aTransaction createTransaction:n*100 ofType:cash];
                [transactions addObject:aTransaction];
                n++;
        }

        do{
                [aTransaction createTransaction:n*100 ofType:credit];
                [transactions addObject:aTransaction];
                n++;
        }while (n<=3);



        for(Transaction *aaTransaction in transactions){
         switch ([aTransaction returnType]) {
                case cash:
                    [europeBudget spendDollars:[aaTransaction returnAmount]];
                    break;
                case credit:
                    [europeBudget changeForeignCurrency:[aaTransaction returnAmount]];
                    break;
                default:
                    break;
         }
        }


        return 0;
}

BudObj.m

#import <Foundation/Foundation.h>
#import "BudObj.h"
#import "Transaction.h"

@implementation Budget

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


- (void) spendDollars:(double)dollars{
    budget = budget - dollars;
        NSLog(@"Converting %0.2f US Dollars into Foreign Currency leaves $%0.2f",dollars,budget);
}

- (void) changeForeignCurrency:(double)foreignCurrency{
    exchangeTransaction = foreignCurrency * exchangeRate;
        budget = budget - exchangeTransaction;
        NSLog(@"Charging %0.2f in Foreign Currency leaves $%0.2f",foreignCurrency,budget);
}

@end

BudObj.h

#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) changeForeignCurrency: (double) foreignCurrency;

@end

Transaction.h

#import <Cocoa/Cocoa.h>

typedef enum{cash,credit} transactionType;

@interface Transaction : NSObject {

        transactionType type;
        double amount;

}

-(void)createTransaction:(double)theAmount ofType:(transactionType)theType;
-(double)returnAmount;
-(transactionType)returnType;

@end

Transaction.m

#import "Transaction.h"

@implementation Transaction

-(void)createTransaction:(double)theAmount ofType:(transactionType)theType{

        type=theType;
        amount=theAmount;

}

-(double)returnAmount{

        return amount;

}

-(transactionType)returnType{

        return type;

}

@end

OUTPUT:

2011-04-10 17:31:28.717 BudObj.m[3751:a0f] Charging 300.00 in Foreign Currency leaves $625.00
2011-04-10 17:31:28.719 BudObj.m[3751:a0f] Charging 300.00 in Foreign Currency leaves $250.00
2011-04-10 17:31:28.720 BudObj.m[3751:a0f] Charging 300.00 in Foreign Currency leaves $-125.00
2011-04-10 17:31:28.720 BudObj.m[3751:a0f] Charging 300.00 in Foreign Currency leaves $-500.00

But the expected output is

Converting 100.00 US dollars into foreign currency leaves $900.00
Charging 100.00 in foreign currency leaves $775.00
Charging 200.00 in foreign currency leaves $525.00
Charging 300.00 in foreign currency leaves $150.00
Converting 100.00 US dollars into foreign currency leaves $1900.00
Charging 100.00 in foreign currency leaves $1750.00

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-21T10:17:43+00:00Added an answer on May 21, 2026 at 10:17 am

    Your ultimate problem is you are using the same transaction object. You set aTransaction once on this line and never set it anywhere else. I do see from your comments it was once right though.

    Transaction* aTransaction;
    aTransaction = [Transaction new];
    

    When you add the the transaction to the array all of them point to the same object and what ever change you make to what you are thinking is 1 aTransaction is really changing them all. So when it comes time to print you have multiple references to the same transaction all of type credit and you keep sending the same return amount.

    Also remember that indexes are usually 0 based and int n = 1; may not get you the correct number elements you are looking for.

    To achieve your expected output with as close to your orignal code as I can try this. (may need some tweaking)

    #import <Foundation/Foundation.h>
    #import "BudObj.h"
    #import "Transaction.h"
    
    int main(int argc, char *argv[]) {
            Budget* europeBudget=[[Budget alloc] init];
    
            [europeBudget createBudget:1000.00 withExchangeRate:1.2500];
    
            Transaction* aTransaction;
            for(int n=1;n<2;n++){
    
                    aTransaction = [[Transaction alloc] init];
                    [aTransaction createTransaction:n*100 ofType:cash];
                    [transactions addObject:aTransaction];
                    [aTransaction release];
            }
    
            int n=0;
            while (n<3) {
                    aTransaction = [[Transaction alloc] init];
                    [aTransaction createTransaction:n*100 ofType:credit];
                    [transactions addObject:aTransaction];
                    [aTransaction release];
                    n++;
            }
            //n = 3 at this point
            do{
                    aTransaction = [[Transaction alloc] init];
                    [aTransaction createTransaction:n*100 ofType:cash];
                    [transactions addObject:aTransaction];
                    [aTransaction release];
                    n++;
            }while (n<=3);
    
            //n = 4 at this point
            aTransaction = [[Transaction alloc] init];
            [aTransaction createTransaction:n*100 ofType:credit];
            [transactions addObject:aTransaction];
            [aTransaction release];
    
            for(Transaction *aaTransaction in transactions){
             switch ([aTransaction returnType]) {
                    case cash:
                            [europeBudget spendDollars:[aaTransaction returnAmount]];
                            break;
                    case credit:
                            [europeBudget changeForeignCurrency:[aaTransaction returnAmount]];
                            break;
                    default:
                            break;
             }
            }
            [transactions release];
            [europeBudget release];
            return 0;
    }
    

    Now I did not check that your dollar amounts are correct but this should print out the correct order of Converting/Charging. I also didn’t assume there was Garbage Collection so I just added all the releases.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm fairly new to Objective-C and trying to figure out best practices and the
I am currently working on an iPhone 2.1 application. I am new to Objective
Currently, my Objective C classes use C++ objects by doing a new when the
Still new to Objective C, and I'm having some trouble that I just can't
I'm new to Objective-C and iPhone development, and I'm trying to store floating-point values
I'm new with Objective-C, so there probably is a simple solution to this. I
How do I implement this method (see below)? I'm new to Objective-C and I'm
Hey all! I'm fairly new to objective-c so I have never had to tackle
I am currently moving from C to Objective-C and, to me, this code seems
I am pretty new to Objective C, and even brand new to using SQL

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.