BeerSong error?


Hey, I’ve input the code for BeerSong as it says in the book (or at least I thought so!) but it keeps giving a different output than that said in the book. The output ends at “There are simply no more bottles of beer on the wall” with no mention of any “Put a bottle in the recycling, __ empty bottles in the bin”?

I’ve gone through the code again and again to try and work it out but I can’t seem to see anywhere where I have miscopied. I’ve copied the code below, could someone show me where I’ve gone wrong? (Sorry about it not being indented, I’m not sure on how to do it on a forum post. Hope it doesn’t get in the way)

#include <stdio.h>

void singTheSong(int numberOfBottles)
if (numberOfBottles == 0) {
printf(“There are simply no more bottles of beer on the wall.\n”);
} else {
printf("%d bottles of beer on the wall. %d bottles of beer.\n",
numberOfBottles, numberOfBottles);
int oneFewer = numberOfBottles - 1;
printf(“take one down and pass it around, %d bottles of beer on the wall.\n”,
singTheSong(oneFewer); // This function calls itself!
printf(“Put a bottle in the recycling, %d empty bottles in the bin.\n”,

int main (int argc, const char * argv[])
return 0;

Thank you


I copied your code into Xcode, and it ran perfectly.
Will you try building and running it again? The code is fine.


To test, you can also try with 9 bottles first (or even with 4), instead of 99, that way it’s much easier to scroll back to see if everything is ok.


hmm very strange. Here is a screenshot of the same code I entered (with the only revision being 4 bottles as opposed to 99):

and here is the output I am getting:



Would you try something for me? Before you return from main(), call fflush like this:

int main (int argc, const char * argv[]) { singTheSong(99); fflush(stdio); return 0; }

The output is buffered, and sometimes characters get stuck in the buffer. This would flush those characters out.

fflush() is defined in <stdio.h>


It says build failed as there is “Use of an undeclared identifier ‘stdio’”:

There was another small problem I had earlier along in the functions chapter, which now leads me to think maybe I have a bad copy of Xcode or something? Is that possible?
In classCertificates, when you said to add <stdlib.h> in order to get the computer to stop complaining that sleep() is implicitly declared, it did no such thing:

It seems my copy of Xcode likes to misbehave…


I just want to add by the way that, my own personal technical problems aside, your book is absolutely fantastic. I am in awe of both your prowess as a programmer as well as your clear ability to teach. That coupled with your own personal dedication towards resolving issues on your forum makes me entirely grateful to have found The Big Nerd Ranch as a learning resource, and more than confident that it shall provide me with all I need along my journey to to developing the skills and proficiencies I desire in regards to programming and eventually developing my own app. So thank you.


Thanks! You are very kind.
Especially when I make a dorky error like that one. It should be stderr:

int main (int argc, const char * argv[]) { singTheSong(99); fflush(stderr); return 0; }


still no luck :frowning:


Is it cool to just carry on regardless do you think? My confidence has been shook slightly, but I’m sure the issue is not that much of an big deal in the long run right? especially considering the stage of learning I am currently at?


Yes, please keep going.


Change of plan! No worries! I just put the code into a new project entirely and it worked just fine. Sorry for the hassle and thanks again for the help. Onwards and upwards! :smiley:


Hey sorry to keep on, but I really think there may be something wrong with my Xcode, and I really want to get this right. After moving on to the Degree sections of functions, I put in the code you wrote to turn celsius into fahrenheit, and as far as I could tell it was exact, and it told me it wouldn’t run because there was basically some error I can’t remember. So I thought ‘ah, whatever, maybe I wrote it wrong. Lets just start again.’ So I closed the window, deleted the file, emptied the trash, created a new one, and instantly it showed me this: …?

Is it me or is there something horribly wrong here. I’ve just started a new fresh project, there shouldn’t be any errors at all surely? Especially that that carry over from a dead file.

Or am I missing something ridiculously advanced?


It looks like you haven’t yet removed your breakpoint. Look at the 2nd paragraph on page 37 again.

[quote]Now we need to remove the breakpoint so that the program will run normally. You can simply drag the blue indicator off the gutter.


:smiley: yeah I realised it might have been down to that yesterday when I got to the part about breakpoints. I hadn’t actually made it there at the point of writing that code but I must have just clicked it by mistake :S