First time I run, doesn't loop through entire array?


#1

The first time I ran my code for the challenge problem, the log only showed results for the first two stocks:

2012-06-19 23:23:07.191 Stocks[39112:403] The value of this stock is 88.00
2012-06-19 23:23:07.194 Stocks[39112:403] The value of this stock is -146.70

However, running it a moment later I got the full results:

2012-06-19 23:23:19.004 Stocks[39126:403] The value of this stock is 88.00
2012-06-19 23:23:19.007 Stocks[39126:403] The value of this stock is -146.70
2012-06-19 23:23:19.007 Stocks[39126:403] The value of this stock is 926.10

Thinking back, I think I have noticed this behavior in the past when I have had other for in loops… Any idea what’s going on here? Am I making a subtle mistake in my programming that is causing this?

Here is my main.m:

#import <Foundation/Foundation.h>
#import "StockHolding.h"

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

    @autoreleasepool {
        
        //initialize the three stocks
        StockHolding *stock1 = [[StockHolding alloc] init];
        StockHolding *stock2 = [[StockHolding alloc] init];
        StockHolding *stock3 = [[StockHolding alloc] init];
        
        //put in values
        [stock1 setPurchaseSharePrice:2.3];
        [stock1 setCurrentSharePrice:4.5];
        [stock1 setNumberOfShares:40];
        [stock2 setPurchaseSharePrice:12.19];
        [stock2 setCurrentSharePrice:10.56];
        [stock2 setNumberOfShares:90];
        [stock3 setPurchaseSharePrice:45.1];
        [stock3 setCurrentSharePrice:49.51];
        [stock3 setNumberOfShares:210];
        
        //create an array with these
        NSArray *stockArray = [NSArray arrayWithObjects:stock1, stock2, stock3, nil];
        
        //print the value of each stock holding
        for (StockHolding *stock in stockArray) {
            NSLog(@"The value of this stock is %.2f", [stock valueInDollars]-[stock costInDollars]);
        }
        
    }
    return 0;
}

And my class files:

#import <Foundation/Foundation.h>

@interface StockHolding : NSObject
{
    float purchaseSharePrice;
    float currentSharePrice;
    int numberOfShares;
}

//Simplify & automate accessors
@property float purchaseSharePrice;
@property float currentSharePrice;
@property int numberOfShares;

- (float)costInDollars;
- (float)valueInDollars;

@end

and

#import "StockHolding.h"

@implementation StockHolding

@synthesize purchaseSharePrice, currentSharePrice, numberOfShares;

- (float)costInDollars
{
    return [self purchaseSharePrice] * [self numberOfShares];
}

- (float)valueInDollars
{
    return [self currentSharePrice] * [self numberOfShares];
}

@end

#2

One possible reason: Your program is probably exiting too quickly before the logs make it to the console.

Try flushing the output streams before the return statement in your main ():

...
fflush (NULL);
return 0;

From man fflush from the command line in Terminal:


#3

[quote=“ibex10”]One possible reason: Your program is probably exiting too quickly before the logs make it to the console.

Try flushing the output streams before the return statement in your main ():

...
fflush (NULL);
return 0;

[/quote]

Is it a good practice is flush output streams all the time? Or is this just a quirky thing when you are working on your first, very basic programs?


#4

You flush the output streams to ensure that the data gets to the intended destination.
If you don’t care about this, there is no need.


#5

Most of the time this doesn’t matter – your program runs long enough that the output gets properly flushed. The problem occurs when the program completes nearly instantaneously, as this one does.

Also, most debuggers will force the output to flush properly when the program terminates, but the LLDB debugger that you are using is relatively new.

In general, you will not need to flush the output buffers.