# Getting singular "bottle" for 1

#1

I’ve worked through Chapter 26, but decided to dip back into earlier chapters to make sure I understand everything so far. As I was redoing the recursion example, I decided I wanted the word “bottle” to appear instead of “bottles” when there is only one bottle. So I added two “else if” statements to cover the problem. I’m sure there is a better way to do this. But it did satisfy the grammar stickler in me.

[code]#include <stdio.h>

void singTheSong (int numberOfBottles)
{
if (numberOfBottles == 0) {
printf(“There are simply no more bottles of beer on the wall.\n”);

} else if (numberOfBottles == 1) {
printf("%d bottle of beer on the wall. %d bottle of beer.\n", numberOfBottles, numberOfBottles);
int oneFewer = numberOfBottles - 1;
printf("Take one down, pass it around, %d bottles of beer on the wall.\n", oneFewer);
singTheSong(oneFewer);
printf("Put a bottle in the recycling, %d empty bottle in the bin.\n", numberOfBottles);

} else if (numberOfBottles == 2) {
printf("%d bottles of beer on the wall. %d bottles of beer.\n", numberOfBottles, numberOfBottles);
int oneFewer = numberOfBottles - 1;
printf("Take one down, pass it around, %d bottle of beer on the wall.\n", oneFewer);
singTheSong(oneFewer);
printf("Put a bottle in the recycling, %d empty bottles in the bin.\n", numberOfBottles);

} else {
printf("%d bottles of beer on the wall. %d bottles of beer.\n", numberOfBottles, numberOfBottles);
int oneFewer = numberOfBottles - 1;
printf("Take one down, pass it around, %d bottles of beer on the wall.\n", oneFewer);
singTheSong(oneFewer);
// This function calls itself! Frames start casting off at 0, completing this last step
printf("Put a bottle in the recycling, %d empty bottles in the bin.\n", numberOfBottles);
}

}

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

return 0;

}
[/code]

#2

I knew if I posted that, I’d think of more ways to improve it.

I noticed in the debugger that two instances of oneFewer were being generated in every frame. So I tried to remedy that by declaring oneFewer outside of the if/else statements. That seems to have worked.

#include <stdio.h>

void singTheSong (int numberOfBottles)
{
int oneFewer = numberOfBottles;

if (numberOfBottles == 0) {
printf("There are simply no more bottles of beer on the wall.\n");

} else if (numberOfBottles == 1) {
printf("%d bottle of beer on the wall. %d bottle of beer.\n", numberOfBottles, numberOfBottles);
oneFewer = numberOfBottles - 1;
printf("Take one down, pass it around, %d bottles of beer on the wall.\n", oneFewer);
singTheSong(oneFewer);
printf("Put a bottle in the recycling, %d empty bottle in the bin.\n", numberOfBottles);

} else if (numberOfBottles == 2) {
printf("%d bottles of beer on the wall. %d bottles of beer.\n", numberOfBottles, numberOfBottles);
oneFewer = numberOfBottles - 1;
printf("Take one down, pass it around, %d bottle of beer on the wall.\n", oneFewer);
singTheSong(oneFewer);
printf("Put a bottle in the recycling, %d empty bottles in the bin.\n", numberOfBottles);

} else {
printf("%d bottles of beer on the wall. %d bottles of beer.\n", numberOfBottles, numberOfBottles);
oneFewer = numberOfBottles - 1;
printf("Take one down, pass it around, %d bottles of beer on the wall.\n", oneFewer);
singTheSong(oneFewer);
// This function calls itself! Frames start casting off at 0, completing this last step
printf("Put a bottle in the recycling, %d empty bottles in the bin.\n", numberOfBottles);
}
}

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

return 0;
}

#3

hi there.
i did it with a ternary operator. i didn’t like the use for two times then. so i tried it also within a function, but it seems i’m not into returning strings yet, i will propably get back to this later. — or maybe someone take it and show it here as function? :geek:

#include <stdio.h>

void singTheSong(int numberOfBottles)
{
if (numberOfBottles == 0) {
printf("There are simply no more bottles of beer on the wall.\n");
}
else {
char *singularPlural = (numberOfBottles>1) ? "bottles" : "bottle";
printf ("%d %s of beer on the wall. %d %s of beer. \n", numberOfBottles, singularPlural, numberOfBottles, singularPlural);
int oneFewer = numberOfBottles -1;
singularPlural = (oneFewer>1) ? "bottles" : "bottle";
printf("Take one down, pass it around, %d %s of beer on the wall.\n", oneFewer, singularPlural);
singTheSong(oneFewer);  //Diese Funktion ruft sich selber auf!
printf("Put a bottle in the recycling. %d empty bottles in the bin.\n", numberOfBottles);
}
}

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

singTheSong(99);
return 0;
}

maybe it would be helpful to drink that much beer for real first, and then…mh