Jumping into C++ - 12 Days of Christmas problem

This is the question:
Write a program that outputs the results of the 12 days of Christmas using switch-case (hint:
you might want to take advantage of fall-through cases)

This is my solution:
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
#include <iostream>
#include <string>

using namespace std;
string num_day[] = {"-", "First", "Second", "Third", "Fourth", "Fifth", "Sixth", "Seventh", "Eigth", "Ninth", "Tenth", "Eleventh", "Twelfth"};
string present_day[] = {"-", "a Partridge in a Pear Tree", "Two Turtle Doves", "Three French Hens", "Four Calling Birds", "Five Gold Rings", "Six Geese a-Laying", "Seven Swans a-Swimming", "Eight Maids a-Milking", "Nine Ladies Dancing", "Ten Lords a-Leaping", "Eleven Pipers Piping", "Twelve Drummers Drumming"};

int main()
{
	for (int i = 0; i < 13; i++)	
	{
		switch (i)
		{
			case 1:
				cout << "On the " + num_day[i] + " day of Christmas my true love sent to me\n";
				for (int j = i; j > 0;)
				{
					if (j > 1)
					{					
						cout << present_day[j] + ",\n";
					}
					else 
					{
						cout << present_day[j] + ".\n\n";
					}
					j--;
				}
				continue;
			case 2:
				cout << "On the " + num_day[i] + " day of Christmas my true love sent to me\n";
				for (int j = i; j > 0;)
				{
					if (j > 1)
					{					
						cout << present_day[j] + ",\n";
					}
					else 
					{
						cout << "and " << present_day[j] + ".\n\n";
					}
					j--;
				}
				continue;
			case 3:
				cout << "On the " + num_day[i] + " day of Christmas my true love sent to me\n";
				for (int j = i; j > 0;)
				{
					if (j > 1)
					{					
						cout << present_day[j] + ",\n";
					}
					else 
					{
						cout << "and " << present_day[j] + ".\n\n";
					}
					j--;
				}
				continue;
			case 4:
				cout << "On the " + num_day[i] + " day of Christmas my true love sent to me\n";
				for (int j = i; j > 0;)
				{
					if (j > 1)
					{					
						cout << present_day[j] + ",\n";
					}
					else 
					{
						cout << "and " << present_day[j] + ".\n\n";
					}
					j--;
				}
				continue;
			case 5:
				cout << "On the " + num_day[i] + " day of Christmas my true love sent to me\n";
				for (int j = i; j > 0;)
				{
					if (j > 1)
					{					
						cout << present_day[j] + ",\n";
					}
					else 
					{
						cout << "and " << present_day[j] + ".\n\n";
					}
					j--;
				}
				continue;
			case 6:
				cout << "On the " + num_day[i] + " day of Christmas my true love sent to me\n";
				for (int j = i; j > 0;)
				{
					if (j > 1)
					{					
						cout << present_day[j] + ",\n";
					}
					else 
					{
						cout << "and " << present_day[j] + ".\n\n";
					}
					j--;
				}
				continue;
			case 7:
				cout << "On the " + num_day[i] + " day of Christmas my true love sent to me\n";
				for (int j = i; j > 0;)
				{
					if (j > 1)
					{					
						cout << present_day[j] + ",\n";
					}
					else 
					{
						cout << "and " << present_day[j] + ".\n\n";
					}
					j--;
				}
				continue;
			case 8:
				cout << "On the " + num_day[i] + " day of Christmas my true love sent to me\n";
				for (int j = i; j > 0;)
				{
					if (j > 1)
					{					
						cout << present_day[j] + ",\n";
					}
					else 
					{
						cout << "and " << present_day[j] + ".\n\n";
					}
					j--;
				}
				continue;
			case 9:
				cout << "On the " + num_day[i] + " day of Christmas my true love sent to me\n";
				for (int j = i; j > 0;)
				{
					if (j > 1)
					{					
						cout << present_day[j] + ",\n";
					}
					else 
					{
						cout << "and " << present_day[j] + ".\n\n";
					}
					j--;
				}
				continue;
			case 10:
				cout << "On the " + num_day[i] + " day of Christmas my true love sent to me\n";
				for (int j = i; j > 0;)
				{
					if (j > 1)
					{					
						cout << present_day[j] + ",\n";
					}
					else 
					{
						cout << "and " << present_day[j] + ".\n\n";
					}
					j--;
				}
				continue;
			case 11:
				cout << "On the " + num_day[i] + " day of Christmas my true love sent to me\n";
				for (int j = i; j > 0;)
				{
					if (j > 1)
					{					
						cout << present_day[j] + ",\n";
					}
					else 
					{
						cout << "and " << present_day[j] + ".\n\n";
					}
					j--;
				}
				continue;
			case 12:
				cout << "On the " + num_day[i] + " day of Christmas my true love sent to me\n";
				for (int j = i; j > 0;)
				{
					if (j > 1)
					{					
						cout << present_day[j] + ",\n";
					}
					else 
					{
						cout << "and " << present_day[j] + ".\n\n";
					}
					j--;
				}
				break;			
		}
	}	
}


It works but it could be done much easier I guess.
I tried to put the whole case statement in a function, but it didn't work, and I couldn't figure out how.
Last edited on
As far as I can tell, there is no need for the switch, you've put the same thing in every case. Also, you miss out the last command in the for loops and then put a decrement at the end of the loop, just put it in the for statement itself (and use prefix).
hint: you might want to take advantage of fall-through cases

I think they are looking for something like this:
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
#include <iostream>
#include <string>

using std::cout;

const char *num_day[] = {"-", "First", "Second", "Third",
			 "Fourth", "Fifth", "Sixth", "Seventh",
			 "Eighth", "Ninth", "Tenth", "Eleventh", "Twelfth"};
int main()
{

    for (int day = 1; day <= 12; ++day) {
	cout << "On the " << num_day[day]
	     << " day of Christmas, my true love gave to me:\n";
	switch (day) {
	case 12: cout << "Twelve Drummers Drumming\n";
	case 11: cout << "Eleven Pipers Piping\n";
	case 10: cout << "Ten Lords a-Leaping\n";
	case 9: cout << "Nine Ladies Dancing\n";
	case 8: cout << "Eight Maids a-Milking\n";
	case 7: cout << "Seven Swans a-Swimming\n";
	case 6: cout << "Six Geese a-Laying\n";
	case 5: cout << "Five Gold Rings\n";
	case 4: cout << "Four Calling Birds\n";
	case 3: cout << "Three French Hens\n";
	case 2: cout << "Two Turtle Doves, and\n";
	case 1: cout << "A Partridge in a Pear Tree\n\n";
	}
    }
    return 0;
}

Topic archived. No new replies allowed.