1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210
|
/*Write a program that outputs all 99 stanzas of the “Ninety-Nine Bottles of Beer
on the Wall” song. Your program should print the number of bottles in English,
not as a number:
Ninety-nine bottles of beer on the wall,
Ninety-nine bottles of beer,
Take one down, pass it around,
Ninety-eight bottles of beer on the wall.
…
One bottle of beer on the wall,
One bottle of beer,
Take one down, pass it around,
Zero bottles of beer on the wall.
Your program should not use ninety-nine different output statements!*/
#include <iostream>
#include <string>
using std::cout;
using std::cin;
using std::endl;
using std::string;
string funcFirstDigit(int, int);
string funcSecondDigit(int);
int main()
{
int firstDigit, secondDigit, bottles = 99;
string line1, line2, line3, line4, altLine1, altLine2, altLine4, secondDigitText, ones, twenty, thirty, forty, fifty, sixty, seventy, eighty, ninety;
altLine1 = " bottle of beer on the wall,"; //used when down to 1 bottle rather than use plural
altLine2 = " bottle of beer,"; //used when down to 1 bottle rather than use plural
line3 = "Take one down, pass it around,";
altLine4 = " bottle of beer on the wall."; //used when down to 1 bottle rather than use plural
for(int a = bottles; a >= 0; a--)
{
firstDigit = bottles / 10; //get the first digit of the number of bottles remaining
secondDigit = bottles % 10; //get the second digit of the number of bottles remaining
line1 = " bottles of beer on the wall.";
line2 = " bottles of beer,";
line4 = " bottles of beer on the wall.";
//specify correct text rather than a blank string when bottles reached zero
if(bottles != 0) secondDigitText = funcSecondDigit(secondDigit);
else secondDigitText = "Zero";
//examine the first digit and display the correct output. Calls function to calculate text to display for 2nd digit
switch(firstDigit)
{
case 0:
{
//use singular version if only 1 bottle left
if(funcSecondDigit(secondDigit) == "One")
{
line1 = altLine1;
line2 = altLine2;
}
cout << secondDigitText << line1 << endl << secondDigitText <<
line2 << endl << line3 << endl << secondDigitText << line4 << endl;
break;
}
case 1:
{
ones = funcFirstDigit(firstDigit, secondDigit);
cout << ones << line1 << endl << ones << line2 << endl << line3 << endl << ones << line4 << endl;
break;
}
//in cases 2 through 9, "if" statement required as we don't need the dash if the remaining bottles is divisible by 10
case 2:
{
if(secondDigit == 0) twenty = "Twenty";
else twenty = "Twenty-";
cout << twenty << secondDigitText << line1 << endl << twenty << secondDigitText <<
line2 << endl << line3 << endl << twenty << funcSecondDigit(secondDigit) << line4 << endl;
break;
}
case 3:
{
if(secondDigit == 0) thirty = "Thirty";
else thirty = "Thirty-";
cout << thirty << secondDigitText << line1 << endl << thirty << secondDigitText <<
line2 << endl << line3 << endl << thirty << secondDigitText << line4 << endl;
break;
}
case 4:
{
if(secondDigit == 0) forty = "Forty";
else forty = "Forty-";
cout << forty << secondDigitText << line1 << endl << forty << secondDigitText <<
line2 << endl << line3 << endl << forty << secondDigitText << line4 << endl;
break;
}
case 5:
{
if(secondDigit == 0) fifty = "Fifty";
else fifty = "Fifty-";
cout << fifty << secondDigitText << line1 << endl << fifty << secondDigitText <<
line2 << endl << line3 << endl << fifty << secondDigitText << line4 << endl;
break;
}
case 6:
{
if(secondDigit == 0) sixty = "Sixty";
else sixty = "Sixty-";
cout << sixty << secondDigitText << line1 << endl << sixty << secondDigitText <<
line2 << endl << line3 << endl << sixty << secondDigitText << line4 << endl;
break;
}
case 7:
{
if(secondDigit == 0) seventy = "Seventy";
else seventy = "Seventy-";
cout << seventy << secondDigitText << line1 << endl << seventy << secondDigitText <<
line2 << endl << line3 << endl << seventy << secondDigitText << line4 << endl;
break;
}
case 8:
{
if(secondDigit == 0) eighty = "Eighty";
else eighty = "Eighty-";
cout << eighty << secondDigitText << line1 << endl << eighty << secondDigitText <<
line2 << endl << line3 << endl << eighty << secondDigitText << line4 << endl;
break;
}
case 9:
{
if(secondDigit == 0) ninety = "Ninety";
else ninety = "Ninety-";
cout << ninety << secondDigitText << line1 << endl << ninety << secondDigitText <<
line2 << endl << line3 << endl << ninety << secondDigitText << line4 << endl;
break;
}
}
bottles--;
cout << endl; //separate each verse with a blank line
}
return 0;
}
//bring in first digit from number of bottles remaining to check for special case of teens, etc
//returns that digit in correct text form
string funcFirstDigit(int firstDigit, int secondDigit)
{
string text;
if(firstDigit == 1) //only need to do the following if the number of bottles left is between 10 and 19 (inclusive)
{
switch(secondDigit)
{
case 0: text = "Ten";
break;
case 1: text = "Eleven";
break;
case 2: text = "Twelve";
break;
case 3: text = "Thirteen";
break;
case 4: text = "Fourteen";
break;
case 5: text = "Fifteen";
break;
case 6: text = "Sixteen";
break;
case 7: text = "Seventeen";
break;
case 8: text = "Eighteen";
break;
case 9: text = "Nineteen";
}
return text;
}
}
//brings in second digit from number of bottles remaining
//returns that digit in text form
string funcSecondDigit(int digit)
{
string text;
switch(digit)
{
case 0: text = "";
break;
case 1: text = "One";
break;
case 2: text = "Two";
break;
case 3: text = "Three";
break;
case 4: text = "Four";
break;
case 5: text = "Five";
break;
case 6: text = "Six";
break;
case 7: text = "Seven";
break;
case 8: text = "Eight";
break;
case 9: text = "Nine";
}
return text;
}
|