Hello!
I am having a problem with my program.I have to write a program that uses repitition and
switch statements to print the song "The Twelve Days of Christmas." One switch statement
should be used to print the days (i.e "First","Second" etc). A separate switch statement
should be used to print the remainder of each verse.
But the program I have written do not fulfill the above conditions. I don't know how to
use the two switch statements that alternate after one another. I am in a need of help.
Can any one help? I'll be thankful.

#include<iostream.h>
#include<conio.h>
void main()
    {
    int i,j;
    for(i=1;i<=24;i++)
     for(j=1;j<=24;j++)
        {
        if(i%2==1)
        switch(i)
                {
                case 1:
                cout<<"On the first day of Christmas\nmy true love sent to me:"<<endl;
                break;
                case 3:
                cout<<"On the second day of Christmas\nmy true love sent to me:"<<endl;
                break;
                case 5:
                cout<<"On the third day of Christmas\nmy true love sent to me:"<<endl;
                break;
                case 7:
                cout<<"On the fourth day of Christmas\nmy true love sent to me:"<<endl;
                break;
                case 9:
                cout<<"On the fifth day of Christmas\nmy true love sent to me:"<<endl;
                break;
                case 11:
                cout<<"On the sixth day of Christmas\nmy true love sent to me:"<<endl;
                break;
                case 13:
                cout<<"On the seventh day of Christmas\nmy true love sent to me:"<<endl;
                break;
                case 15:
                cout<<"On the eighth day of Christmas\nmy true love sent to me:"<<endl;
                break;
                case 17:
                cout<<"On the ninth day of Christmas\nmy true love sent to me:"<<endl;
                break;
                case 19:
                cout<<"On the tenth day of Christmas\nmy true love sent to me:"<<endl;
                break;
                case 21:
                cout<<"On the eleventh day of Christmas\nmy true love sent to me:"<<endl;
                break;
                case 23:
                cout<<"On the twelfth day of Christmas\nmy true love sent to me:"<<endl;
                break;
                default:
                break;
                }
                }
       
              if(j%2==0)
        {
                switch(j)
                {
                case 2:
                cout<<"A Partridge in a Pear Tree"<<endl;
                break;
                case 4:
                cout<<"Two Turtle Doves and\na Partridge in a Pear Tree"<<endl;
                break;
                case 6:
                cout<<"Three French Hens\nTwo Turtle Doves and\na Partridge in a Pear Tree"<<endl;
                break;
                case 8:
                cout<<"Four Calling Birds\nThree French Hens\nTwo Turtle Doves\nand a Partridge in a Pear Tree"<<endl;
                break;
                case 10:
                cout<<"Five Golden Rings\nFour Calling Birds\nThree French Hens\nTwo Turtle Doves\nand a Partridge in a Pear Tree"<<endl;
                break;
                case 12:
                cout<<"Six Geese a Laying\nFive Golden Rings\nFour Calling Birds\nThree French Hens\nTwo Turtle Doves\nand a Partridge in a Pear Tree"<<endl;
                break;
                case 14:
                cout<<"Seven Swans a Swimming\nSix Geese a Laying\nFive Golden Rings\nFour Calling Birds\nThree French Hens\nTwo Turtle Doves\nand a Partridge in a Pear Tree"<<endl;
                break;
                case 16:
                cout<<"Eight Maids a Milking\nSeven Swans a Swimming\nSix Geese a Laying\nFive Golden Rings\nFour Calling Birds\nThree French Hens\nTwo Turtle Doves\nand a Partridge in a Pear Tree"<<endl;
                break;
                case 18:
                cout<<"Nine Ladies Dancing\nEight Maids a Milking\nSeven Swans a Swimming\nSix Geese a Laying\nFive Golden Rings\nFour Calling Birds\nThree French Hens\nTwo Turtle Doves\nand a Partridge in a Pear Tree"<<endl;
                break;
                case 20:
                cout<<"Ten Lords a Leaping\nNine Ladies Dancing\nEight Maids a Milking\nSeven Swans a Swimming\nSix Geese a Laying\nFive Golden Rings\nFour Calling Birds\nThree French Hens\nTwo Turtle Doves\nand a Partridge in a Pear Tree"<<endl;
                break;
                case 22:
                cout<<"Eleven Pipers Piping\nTen Lords a Leaping\nNine Ladies Dancing\nEight Maids a Milking\nSeven Swans a Swimming\nSix Geese a Laying\nFive Golden Rings\nFour Calling Birds\nThree French Hens\nTwo Turtle Doves\nand a Partridge in a Pear Tree"<<endl;
                break;
                case 24:
                cout<<"12 Drummers Drumming\nEleven Pipers Piping\nTen Lords a Leaping\nNine Ladies Dancing\nEight Maids a Milking\nSeven Swans a Swimming\nSix Geese a Laying\nFive Golden Rings\nFour Calling Birds\nThree French Hens\nTwo Turtle Doves\nand a Partridge in a Pear Tree"<<endl;
                break;
                default:
                break;
                }
        }
    getch();
    }

Recommended Answers

All 2 Replies

Perhaps more details on the exact program spec and your programming experience? It may have an impact on the advice to give.

I am wondering whether this is one of those rare cases where one takes advantage of the fact that switch statements can "fall through" without a break command and display everything. And perhaps you are also supposed to use arrays? Maybe not, but they could help. I imagine I'd use for-loops rather than switch statements, but switch is doable.

string numbers[13] = {"Zero", "One", "Two", "Three", ...};
string numbersPossessive[13] = {"", "first", "second", "third", ...};
string gifts[13] =
{
    "",
    "partridge in a pear tree",
    "turtle doves",
    ...
};

Putting it in arrays sorts of cleans it up, in my opinion, but you don't have to. A switch would work, but the code is longer:

cout << "On the ";
switch (i)
{
    case 1: cout << "first"; break;
    case 2: cout << "scond"; break;
    // etc.
}

cout << " day of Christmas, my true love gave to me...\n";

// now do your second switch, but without the break statements.

switch (i)
{
    case 12: cout << "12 drummers drumming\n";
    case 11: cout << "11 Pipers piping\n";
    // etc.
}

The switch WITHOUT the break statements will display everything AFTER its particular case. Still, I think the program might be better with for-loops rather than switch statements. And consider using arrays for the text.

Thanks for the help. I'll try it again with the changes you have said

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.