99 beers

I'm new to programming and here is my assignment.
Problem:

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!

The program shouldn't ask a user to input a number between 1 and 99. it is supposed to just display the saying in only words from ninety-nine to zero. this is what I have so far and I know this part is right since the professor helped me with it. I am stuck on the for part. How to I display the all the numbers that wrote? I know I don't know what to put for cout? Any help is appreciated. Also I know the program is not finished, I'm trying to show the numbers as words first before I add the saying.

Code:
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
#include <iostream>
#include <cstring>

using namespace std;

string tens(int x){
    switch(x) {
        case 9: return "Ninety"; break;
        case 8: return "Eighty"; break;
        case 7: return "Seventy"; break;
        case 6: return "Sixty"; break;
        case 5: return "Fifty"; break;
        case 4: return "Forty"; break;
        case 3: return "Thirty"; break;
        case 2: return "Twenty"; break;
        case 1: return ""; break;
        case 0: return ""; break;
    }}
string ones(int x){
    switch(x) {
        case 9: return "Nine"; break;
        case 8: return "Eight"; break;
        case 7: return "Seven"; break;
        case 6: return "Six"; break;
        case 5: return "Five"; break;
        case 4: return "Four"; break;
        case 3: return "Three"; break;
        case 2: return "Two"; break;
        case 1: return "One"; break;
        case 0: return ""; break;
    }}

int main(void)

{
    int ones, tens;
for ( int i = 99; i>= 20; i--){

    ones  = i % 10;
    tens = i / 10;
    cout<<
return 0;
}
}

Last edited on
Hi,

Please edit your post and use code tags to make it readable - http://www.cplusplus.com/articles/jEywvCM9/

Could you tell us what exactly you are having trouble with?
Hi. I am having trouble with outputting the numbers in words. I don't know what to put for cout to display the words ninety-nine, ninety-eight,... etc.
Last edited on
closed account (2UD8vCM9)
Well one problem is you have a function the same name as your variables. Don't name the int ones, tens and have functions called ones, tens. Also you should've been using the string library, not cstring.

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
#include <iostream>
#include <string>

using namespace std;

string tenstring(int x) 
{
	switch (x) 
	{
	case 9: return "Ninety"; break;
	case 8: return "Eighty"; break;
	case 7: return "Seventy"; break;
	case 6: return "Sixty"; break;
	case 5: return "Fifty"; break;
	case 4: return "Forty"; break;
	case 3: return "Thirty"; break;
	case 2: return "Twenty"; break;
	case 1: return ""; break;
	case 0: return ""; break;
	}
}
string onestring(int x) 
{
	switch (x)
	{
	case 9: return "Nine"; break;
	case 8: return "Eight"; break;
	case 7: return "Seven"; break;
	case 6: return "Six"; break;
	case 5: return "Five"; break;
	case 4: return "Four"; break;
	case 3: return "Three"; break;
	case 2: return "Two"; break;
	case 1: return "One"; break;
	case 0: return ""; break;
	}
}

int main(void)
{
	int ones, tens;
	for (int i = 99; i >= 20; i--) 
	{
		ones = i % 10;
		tens = i / 10;
		cout << tenstring(tens) << " " << onestring(ones) << " bottles of beer on the wall.\n";
	}

	return 0;
}
Last edited on
Pindrought, you are a lifesaver! I was thinking the same thing but didn't know how to change it. So I finished the problem but have a simple formatting error for anyone that knows how to fix this. The program output doesn't start like:

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.
...

It starts like:
Ninety-nine bottles of beer,
Take one down, pass it around,
Ninety-eight bottles of beer on the wall.
...

Why is it not outputting the first line "Ninety-nine bottles of beer on the wall,"? How should I fix it? Also a weird thing I noticed, I tried to add an empty after each phrase by adding a \n or endl and after doing that it started from 93 not 99 anymore. I don't know why it would start from 93 for adding a new line. I would like to fix both issues but the first issue is more important if you cant figure out the second issue. Thanks

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
#include <iostream>
#include <string>

using namespace std;

string tenstring(int x)
{
	switch (x)
	{
	case 9: return "Ninety"; break;
	case 8: return "Eighty"; break;
	case 7: return "Seventy"; break;
	case 6: return "Sixty"; break;
	case 5: return "Fifty"; break;
	case 4: return "Forty"; break;
	case 3: return "Thirty"; break;
	case 2: return "Twenty"; break;
	case 1: return ""; break;
	case 0: return ""; break;
	}
}
string onestring(int x)
{
	switch (x)
	{
	case 9: return "-nine"; break;
	case 8: return "-eight"; break;
	case 7: return "-seven"; break;
	case 6: return "-six"; break;
	case 5: return "-five"; break;
	case 4: return "-four"; break;
	case 3: return "-three"; break;
	case 2: return "-two"; break;
	case 1: return "-one"; break;
	case 0: return ""; break;
	}
}
string teenstring(int x)
{
	switch (x)
	{
    case 19: return "Nineteen"; break;
	case 18: return "Eighteen"; break;
	case 17: return "Seventeen"; break;
	case 16: return "Sixteen"; break;
	case 15: return "Fifteen"; break;
	case 14: return "Fourteen"; break;
	case 13: return "Thirteen"; break;
	case 12: return "Twelve"; break;
	case 11: return "Eleven"; break;
	case 10: return "Ten"; break;
	}
}
string singlestring(int x)
{
	switch (x)
	{
    case 9: return "Nine"; break;
	case 8: return "Eight"; break;
	case 7: return "Seven"; break;
	case 6: return "Six"; break;
	case 5: return "Five"; break;
	case 4: return "Four"; break;
	case 3: return "Three"; break;
	case 2: return "Two"; break;
	case 1: return "One"; break;
	case 0: return ""; break;
	}
}
int main(void)
{
	int ones, tens;
	for (int i = 99; i >= 20; i--)
	{
		ones = i % 10;
		tens = i / 10;
		cout << tenstring(tens) << onestring(ones) << " bottles of beer on the wall.\n";
		cout << tenstring(tens) << onestring(ones) << " bottles of beer,\n"<<"Take one down, pass it around,"<<endl;
	}

	int teens;
	for (int i = 19; i >= 10; i--)
	{
		teens = i;
		cout << teenstring(teens) << " bottles of beer on the wall.\n";
		cout << teenstring(teens) << " bottles of beer,\n"<<"Take one down, pass it around,"<<endl;
	}
	int single;
	for (int i = 9; i >= 1; i--)
	{
		single = i;
		cout << singlestring(single) << " bottles of beer on the wall.\n";
		cout << singlestring(single) << " bottles of beer,\n"<<"Take one down, pass it around,"<<endl;

	}
        cout << "Zero bottles of beer on the wall.";
	return 0;
}

Last edited on
closed account (2UD8vCM9)
The problem is that you aren't outputting the last line for the song.

So like..

99 bottles of beer on the wall
99 bottles of beer
Take one down pass it around
98 bottles of beer on the wall <-- this line
98 bottles of beer on the wall
98 bottles of beer
Take one down pass it around
97 bottles of beer on the wall <-- this line


You can get this effect by changing your first for loop to something like

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
	for (int i = 99; i >= 97; i--)
	{
		ones = i % 10;
		tens = i / 10;
		cout << tenstring(tens) << onestring(ones) << " bottles of beer on the wall.\n";
		cout << tenstring(tens) << onestring(ones) << " bottles of beer,\n" << "Take one down, pass it around," << endl;
		if (i > 20) //If we are at 21 or more bottles of beer on the wall
		{
			ones = (i - 1) % 10;
			tens = (i - 1) / 10;
			cout << tenstring(tens) << onestring(ones) << " bottles of beer on the wall.\n"; //Print next number down
		}
		else //If we are at 20 bottles of beer on the wall, next # is 19
		{
			cout << teenstring(19) << " bottles of beer on the wall.\n"; //Print 19 bottles of beer on the wall before going to next for loop
		}
	}


Keep in mind you will have to do the similar changes to your second for loop to properly repeat when there are 9 bottles of beer on the wall by using an if-else statement in the loop or similar logic that will perform the same output.
Last edited on
Got it thank you for your help
Topic archived. No new replies allowed.