Buy Beer Cans, Bottles for Sale

Posts Tagged ‘bottles’

IMMEDIATE C++ 99 BOTTLES OF BEER HELP!?

Saturday, October 15th, 2011

Question by KIMBERLY C: IMMEDIATE C++ 99 BOTTLES OF BEER HELP!?

so far this is my code. it’s long i know, and it works perfectly fine, but
can someone tell me how can i make it look so it keeps printing until it hits 0? because when i enter a number, for example 55, it will print

fifty-five bottles of beer on the wall,
fifty-five bottles of beer,
take one down, pass it around,
fifty-four bottles of beer on the wall

and it stops. i want to make it so it prints all the way to 0.

i have tried the while and for loop, both don’t work. ;[

help me please! ;[ this is due tmrw...lol

#include
#include

using namespace std;
int main()
{

cout << "Please enter the number of bottles of beer you want: ";
int num;
cin>>num;

while (num > 0)
{
if(num<=99&&num>0)
{ int x,y;
x=num/10;
y=num%10;
switch(num)
{
case 1:cout<<"One";
break;
case 2:cout<<"Two";
break;
case 3:cout<<"Three";
break;
case 4:cout<<"Four";
break;
case 5:cout<<"Five";
break;
case 6:cout<<"Six";
break;
case 7:cout<<"Seven";
break;
case 8:cout<<"Eight";
break;
case 9:cout<<"Nine";
break;
}
switch(num)
{
case 10:cout<<"Ten";
break;
case 11:cout<<"Eleven";
break;
case 12:cout<<"Twelve";
break;
case 13:cout<<"Thirteen";
break;
case 14:cout<<"Fourteen";
break;
case 15:cout<<"Fifteen";
break;
case 16:cout<<"Sixteen";
break;
case 17:cout<<"Seventeen";
break;
case 18:cout<<"Eighteen";
break;
case 19:cout<<"Nineteen";
break;}
switch(x)
{
case 2:cout<<"Twenty";
break;
case 3:cout<<"Thirty";
break;
case 4:cout<<"Forty";
break;
case 5:cout<<"Fifty";
break;
case 6:cout<<"Sixty";
break;
case 7:cout<<"Seventy";
break;
case 8:cout<<"Eighty";
break;
case 9:cout<<"Ninety";
break;
}
if(num>20)
{ switch(y)
{
case 1:cout<<"-one";
break;
case 2:cout<<"-two";
break;
case 3:cout<<"-three";
break;
case 4:cout<<"-four";
break;
case 5:cout<<"-five";
break;
case 6:cout<<"-six";
break;
case 7:cout<<"-seven";
break;
case 8:cout<<"-eight";
break;
case 9:cout<<"-nine";
break;
}}
if(num==1)
cout<<" bottle of beer on the wall,\n";
else
cout<<" bottles of beer on the wall,\n";
switch(num)
{
case 1:cout<<"One";
break;
case 2:cout<<"Two";
break;
case 3:cout<<"Three";
break;
case 4:cout<<"Four";
break;
case 5:cout<<"Five";
break;
case 6:cout<<"Six";
break;
case 7:cout<<"Seven";
break;
case 8:cout<<"Eight";
break;
case 9:cout<<"Nine";
break;
}
switch(num)
{
case 10:cout<<"Ten";
break;
case 11:cout<<"Eleven";
break;
case 12:cout<<"Twelve";
break;
case 13:cout<<"Thirteen";
break;
case 14:cout<<"Fourteen";
break;
case 15:cout<<"Fifteen";
break;
case 16:cout<<"Sixteen";
break;
case 17:cout<<"Seventeen";
break;
case 18:cout<<"Eighteen";
break;
case 19:cout<<"Nineteen";
break;}
switch(x)
{
case 2:cout<<"Twenty";
break;
case 3:cout<<"Thirty";
break;
case 4:cout<<"Forty";
break;
case 5:cout<<"Fifty";
break;
case 6:cout<<"Sixty";
break;
case 7:cout<<"Seventy";
break;
case 8:cout<<"Eighty";
break;
case 9:cout<<"Ninety";
break;
}
if(num>20)
{ switch(y)
{
case 1:cout<<"-one";
break;
case 2:cout<<"-two";
break;
case 3:cout<<"-three";
break;
case 4:cout<<"-four";
break;
case 5:cout<<"-five";
break;
case 6:cout<<"-six";
break;
case 7:cout<<"-seven";
break;
case 8:cout<<"-eight";
break;
case 9:cout<<"-nine";
break;
}}
if(num==1)
cout<<" bottle of beer,\n"


Best answer:

Answer by The Dude
yikes. lol.

here's an example in c# that I wrote personally:

-------

using System;

namespace NinetyNineBottles
{
internal class Beer
{
[STAThread]
static void Main(string[] args)
{
for (int i = 99; i > -1; i–)
{
IBeerTurns turn = BeerTurnFactory.GetBeerTurn(i);
Console.WriteLine(turn.TakeTurn(i));
}
Console.ReadLine();
}
}
internal static class BeerTurnFactory
{
internal static IBeerTurns GetBeerTurn(int i)
{
switch (i)
{
case 0:
return new ZeroBeersLeftBeerTurn();
case 1:
return new OneBeerLeftBeerTurn();
case 2:
return new TwoBeersLeftBeerTurn();
default:
return new TypicalBeerTurn();
}
}
}
public interface IBeerTurns
{
string TakeTurn(int bottles);
string FirstBobotw { get; }
string FirstBob { get; }
string SecondBobotw { get; }
}
internal abstract class BeerTurnsBase : IBeerTurns
{
public virtual string FirstBobotw
{
get
{
return “bottles of beer on the wall”;
}
}
public virtual string FirstBob
{
get
{
return “bottles of beer”;
}
}
public virtual string SecondBobotw
{
get
{
return “bottles of beer on the wall”;
}
}
public virtual string TakeTurn(int bottles)
{
return string.Format(“{0} {1}, {0} {3}.{5}Take one down and pass it around, {2} {4}.{5}”,
bottles, FirstBobotw, bottles – 1, FirstBob, SecondBobotw, Environment.NewLine);
}
}
internal class TypicalBeerTurn : BeerTurnsBase
{
}
internal class TwoBeersLeftBeerTurn : BeerTurnsBase
{
public override string SecondBobotw
{
get
{
return “bottle of beer on the wall”;
}
}
}
internal class OneBeerLeftBeerTurn : BeerTurnsBase
{
public override string FirstBobotw
{
get
{
return “bottle of beer on the wall”;
}
}
public override string FirstBob
{
get
{
return “bottle of beer”;
}
}
public override string SecondBobotw
{
get
{
return “no more bottles of beer on the wall”;
}
}
public override string TakeTurn(int bottles)
{
return string.Format(“{0} {1}, {0} {3}.{5}Take one down and pass it around, {4}.{5}”,
bottles, FirstBobotw, bottles – 1, FirstBob, SecondBobotw, Environment.NewLine);
}
}
internal class ZeroBeersLeftBeerTurn : BeerTurnsBase
{
public override string TakeTurn(int bottles)
{
return “No more bottles of beer on the wall, no more bottles of beer.” +
Environment.NewLine +
“Go to the store and buy some more, 99 bottles of beer on the wall.”;
}
}
}

——-

and here’s a c++ example:

——-

#include

template
class Loop {
public:
static inline void f () {
cout << I << " bottles of beer on the wall," << endl
<< I << " bottles of beer." << endl
<< "Take one down, pass it around," << endl
<< I-1 << " bottles of beer on the wall." << endl;
Loop::f();
}
};

class Loop<0> {
public:
static inline void f () {
cout << "Go to the store and buy some more," << endl
<< "99 bottles of beer on the wall." << endl;
}
};

int main () {
Loop<3>::f();
return 0;
}

==============================

And more c++ examples:

http://99-bottles-of-beer.net/language-c++-109.html

——-

Add your own answer in the comments!