Challenge #2: Interesting Question (I think)


#1

I’ve noticed that sometimes when I’ve first compiled and run my programs that the log file output is different (abridged) from when I run it subsequently. For example in Challenge #2 I entered the code supplied on page 90, hit Run, and here’s what I got:

2012-06-03 15:51:16.389 Chap 15 Challenge 2[5932:403] Aaron
2012-06-03 15:51:16.391 Chap 15 Challenge 2[5932:403] Isaac

Without changing anything, I hit Run again (recompiling, of course) and now I see this in the log file:

2012-06-03 15:51:28.769 Chap 15 Challenge 2[5940:403] Aaron
2012-06-03 15:51:28.772 Chap 15 Challenge 2[5940:403] Isaac
2012-06-03 15:51:28.772 Chap 15 Challenge 2[5940:403] Lievaart
2012-06-03 15:51:28.773 Chap 15 Challenge 2[5940:403] Maarten
2012-06-03 15:51:28.773 Chap 15 Challenge 2[5940:403] Raanan
2012-06-03 15:51:28.774 Chap 15 Challenge 2[5940:403] Saad
2012-06-03 15:51:28.774 Chap 15 Challenge 2[5940:403] Sjaak

I tried recompiling a few more times and always had the same result. I’ve noticed this with some of the other programs too. Is there some reason for this? Am I crazy?


#2

I have seen some cases where the program ran quickly and terminated before the output got to the debugger. It seems like it shouldn’t happen, but it has happened in some versions of Xcode. Try adding this line to the end of the program to flush the output before it terminates:

fflush(NULL);

#3

Hi Aaron,

Thanks very much for this tip; I’ve added it to the previous code just before ‘return 0’ and it works fine. I’ll keep using it in other command line programs and see if this clears up the issue for me.

And thanks very much for your book. I appreciate your approach, of providing your reader with a more working knowledge of the tool (XCode) and the language while progressively teaching the basics. Lots more fun than the traditional methods. To me it’s like learning conversational French that I can use today rather then spending time learning how to conjugate the verb “to be” and then learning a bunch of nouns.

Best regards,

Barry