I wrote a C++ program for 99 Bottles of Beer, but it's kind of messy. Is there any way I can make it short and cleaner and more easier to read?
/*
* Write a program that prints out the entire lyrics to a full rendition of "99 bottles of beer"
*/
#include <iostream>
using namespace std;
int main (void)
{
int bottle = 99;
while (bottle > 0)
{
cout << bottle << " bottles of beer on the wall, "
<< bottle << " bottles of beer." << endl;
bottle--;
cout << "Take one down and pass it around, " << bottle << " "
<< "bottles of beer on the wall.\n" << endl;
if (bottle == 1)
{
break;
}
}
cout << "1 bottle of beer on the wall, 1 bottle of beer." << endl
<< "Take one down and pass it around, no more bottles of beer on the wall.\n" << endl
<< "No more bottles of beer on the wall, no more bottles of beer." << endl
<< "Go to the store and buy some more, 99 bottles of beer on the wall.";
return 0;
}
"\n"
tostd::endl
. \$\endgroup\$"\n"
vs.std::endl
...the point being raised by @LokiAstari is that endl forces a flush of the stream. This can be useful for debugging information, so you get output right up to the crash, but it interferes with the natural performance profile of the buffering otherwise. (Just wanting to be clear it's not a matter of dictating aesthetic preference, because I thinkendl
is more "literate-looking".) \$\endgroup\$for
loop instead of a while, it would save having to manually decrementfor (int i = 99; i > 1; i--){...}
\$\endgroup\$