Note that there are some explanatory texts on larger screens.

plurals
  1. POObjective C:Cant understand the logic of looping
    primarykey
    data
    text
    <p>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.</p> <h2>main.m</h2> <pre><code>#import &lt;Foundation/Foundation.h&gt; #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&lt;2;n++){ [aTransaction createTransaction:n*100 ofType:credit]; [transactions addObject:aTransaction]; } int n=1; while (n&lt;3) { [aTransaction createTransaction:n*100 ofType:cash]; [transactions addObject:aTransaction]; n++; } do{ [aTransaction createTransaction:n*100 ofType:credit]; [transactions addObject:aTransaction]; n++; }while (n&lt;=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; } </code></pre> <hr> <h2>BudObj.m</h2> <pre><code>#import &lt;Foundation/Foundation.h&gt; #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 </code></pre> <hr> <h2>BudObj.h</h2> <pre><code>#import &lt;Foundation/Foundation.h&gt; @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 </code></pre> <hr> <h2>Transaction.h</h2> <pre><code>#import &lt;Cocoa/Cocoa.h&gt; typedef enum{cash,credit} transactionType; @interface Transaction : NSObject { transactionType type; double amount; } -(void)createTransaction:(double)theAmount ofType:(transactionType)theType; -(double)returnAmount; -(transactionType)returnType; @end </code></pre> <hr> <h2>Transaction.m</h2> <pre><code>#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 </code></pre> <hr> <h2>OUTPUT:</h2> <pre><code>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 </code></pre> <h2>But the expected output is</h2> <pre><code>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 </code></pre> <hr>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload