Voting

Category

real language

Bookmarking

Del.icio.us Digg Diigo DZone Earthlink Google Kick.ie
Windows Live LookLater Ma.gnolia Reddit Rojo StumbleUpon Technorati

Language C++

(hacking style)

Date:04/20/05
Author:Tim Robinson
URL:n/a
Comments:14
Info:n/a
Score: (2.95 in 119 votes)
// C++ version of 99 Bottles of beer
// programmer: Tim Robinson timtroyr@ionet.net
// Corrections by Johannes Tevessen

#include <iostream>
using namespace std;

int main()
    {
    int bottles = 99;
    while ( bottles > 0 )
        {
        cout << bottles << " bottle(s) of beer on the wall," << endl;
        cout << bottles << " bottle(s) of beer." << endl;
        cout << "Take one down, pass it around," << endl;
        cout << --bottles << " bottle(s) of beer on the wall." << endl;
        }
    return 0;
    }

Download Source | Write Comment

Alternative Versions

VersionAuthorDateCommentsRate
extreme template metaprogrammingRichard Wolf04/20/055
meta programmingArion Lei04/20/055
object-oriented versionTim Robinson04/20/054
Preprocessor & self-include recursionChocapic02/27/076
feature creepJono04/27/091
most extreme template metaprogrammingHenrik Theiling12/04/110
7Jono04/15/090
ob fuscatedTapi (Paddy O'Brien)08/11/054
GUI versionMartyn Davies05/28/051

Comments

>>  Zak said on 05/17/05 21:53:23

Zak nice, may be shorter

>>  ksdkjcjdas said on 01/10/06 11:28:23

ksdkjcjdas Well, just add in the last part and you will recreate the entire song...

>>  Mick said on 04/06/06 21:24:23

Mick I like the never ending thing.

>>  PhreakerD7 said on 04/08/06 20:44:32

PhreakerD7 Yeah. I saw the first one that was long as hell and was thinking "WHILE loop, anyone?"

>>  freacker4life said on 04/29/06 10:16:24

freacker4life Pretty Good. There's just one problem. After executing this program, it quits automatically. I think its better to add a "cin.get();" after the loop at least.

>>  P1xel said on 03/29/07 02:27:48

P1xel Yes, you should add it at the end...

>>  prushik said on 12/03/07 20:26:05

prushik Whats up with the while loop? Why does nobody think to use a for loop for this?

>>  catnappa said on 10/22/08 22:00:29

catnappa no simply add

system ("pause";);

one line before the return to make it wait for a keyboard button press.

>>  Michael said on 03/18/09 07:49:35

Michael No, catnappa, cin.get() is correct. Not only is system("pause";) a gigantic security hole, it only works on windows/MS-DOS, while C++ is portable. system("pause";) should NEVER be used outside of crappy little homework assignments or code that is not going to be released. Granted, in something like this, it's not that big of a deal, but why form bad coding habits? Use cin.get() (there are actually other tricks for this that work better, but cin.get() works ok), not system("ANYTHING, EVER";)

>>  Jono said on 04/17/09 02:30:22

Jono it doesn't write the last verse.

>>  rahman said on 05/19/09 12:50:55

rahman hmm...
it's easy!

>>  Pasaria said on 01/06/10 16:42:01

Pasaria Got this version that I programmed. Includes ending.

#include <iostream>
#include <windows.h>

using namespace std;

int a=99;

int main()
{
cout << "Welcome to the 99 Bottles of Beer Song Program!" << endl;
cout << "Progam programmed in 2010 by Pasaria." << endl;
system("PAUSE";);
while(1)
{
if( a >= 3) {
cout << a << " bottles of beer on the wall " << a << " bottles of beer." << endl;
cout << " " << endl;
a--;
Sleep(3100);
cout << "Take one down and pass it around, " << a << " bottles of beer on the wall." << endl;
}
else {
a--;
cout << "Take one down and pass it around, " << a << " bottle of beer on the wall." << endl;
cout << " " << endl;
cout << "No more bottles of beer on the wall, no more bottles of beer. " << endl;
cout << "Go to the store and buy some more, 99 bottles of beer on the wall." << endl;
system("PAUSE";);
break;
}
}
}

>>  penoo said on 05/31/10 06:35:03

penoo Complete version here: (19/20 lines)

#include <iostream>
#include <string>

int main() {
   std::string s[9] = {" bottle", " bottles", " of beer", " on the wall",
      "Take one down and pass it around, ", "No more", " no more", "1",
      "Go to the store and buy some more, 99"};
   for(int i = 99; i > 0; i--) {
      if (i < 3)
         std::cout << i << s[i - 1] << s[2] << s[3] << ", " << i << s[i- 1] << s[2]
         << ".\n" << s[4] << s[i + 5] << s[(i * -1) + 2] << s[2] << s[3] << ".\n\n";
      else
         std::cout << i << s[1] << s[2] << s[3] << ", " << i << s[1] << s[2]
         << ".\n" << s[4] << i - 1 << s[1] << s[2] << s[3] << ".\n\n";
   }
   std::cout << s[5] << s[1] << s[2] << s[3] << ',' << s[6] << s[1] << s[2]
   << ".\n" << s[8] << s[1] << s[2] << s[3] << '.';
   std::cin.get();
   return 0;
}

>>  Lurveleven said on 05/31/10 11:57:17

Lurveleven Pretty far from giving the correct result.

Download Source | Write Comment

Add Comment

Please provide a value for the fields Name, Comment and Security Code.
This is a gravatar-friendly website.
E-mail addresses will never be shown.
Enter your e-mail address to use your gravatar.

Please don't post large portions of code here! Use the form to submit new examples or updates instead!

Name:

eMail:

URL:

Security Code:
  
Comment: