#include <cstdlib>
#include <conio.h>
#include <iostream>
#include <stdlib.h>


using namespace std;

int main(int argc, char *argv[])
{
    int x,ctr;

    cout<< "Enter a day: ";
    cin>> x;
    for (ctr=x;x>0;x--)
    for (ctr=1;ctr<=12;ctr++)

    {

    switch (x)
    {
     case 1:cout<< "On the first day of Christmas\nMy true love sent to me...\n"; break;
     case 2:cout<< "On the second day of Christmas\nMy true love sent to me...\n"; break;
     case 3:cout<< "On the third day of Christmas\nMy true love sent to me...\n"; break;
     case 4:cout<< "On the fourth day of Christmas\nMy true love sent to me...\n"; break;
     case 5:cout<< "On the fifth day of Christmas\nMy true love sent to me...\n"; break;
     case 6:cout<< "On the sixth day of Christmas\nMy true love sent to me...\n"; break;
     case 7:cout<< "On the seventh day of Christmas\nMy true love sent to me...\n"; break;
     case 8:cout<< "On the eight day of Christmas\nMy true love sent to me...\n"; break;
     case 9:cout<< "On the ninth day of Christmas\nMy true love sent to me...\n"; break;
     case 10:cout<< "On the tenth day of Christmas\nMy true love sent to me...\n"; break;
     case 11:cout<< "On the eleventh day of Christmas\nMy true love sent to me...\n"; break;
     case 12:cout<< "On the twelfth day of Christmas\nMy true love sent to me...\n"; break;

    }


    switch (ctr)
    {
     case 12:cout<< "12 drummers drumming\n";
     case 11:cout<< "11 Pipers piping \n";
     case 10:cout<< "10 Lords a leaping\n";
     case 9:cout<< "9 Ladies dancing\n";
     case 8:cout<< "8 Maids milking\n";
     case 7:cout<< "7 Swans swimming\n";
     case 6:cout<< "6 Geese laying\n";
     case 5:cout<< "5 Golden rings\n";
     case 4:cout<< "4 Calling birds\n";
     case 3:cout<< "3 French hens\n";
     case 2:cout<< "2 Turtle doves\n";
     case 1:cout<< "And a partridge in a pear tree\n";



     }
     }


    getch();

    system("PAUSE");
    return EXIT_SUCCESS;
}

can anyone help me fix my program?

obaskinzo commented: pls sir what is this i like to be a digsiner +0

my program runs but the only problem here is that my output seems to be wrong can someone help me fix it?

Member Avatar for Rahul47

What is your output ? And what is going wrong ?

try to put "break;" statement after each "case" ends. and then try to run your code again. hope it helps.

for ex:

switch(x)
{
       case 1:
               // your statements
               break;
       case 2:

               // your statements
               break;
}

thanks.

can anyone help me fix my program?

What is wrong with it?
Is it not compiling?
Not executing?
Executing, but outputting unexpected results?
...

What input are you feeding in to the program?

A couple aspects of your program caught my eye:
1) Line 15 is a standalone for loop. Is this loop supposed to have a body? Effectively, all it does is set x to 0. But since x = 0, when the program reaches the switch blocks nothing happens.
2) Switch statements usually include a default statement on the last line. Your program might not need it, but I think it is good to be thorough.

for loop. Is this loop supposed to have a body? Effectively, all it does is set x to 0. But since x = 0, when the program reaches the switch blocks nothing happens.
The next for loop forms the body of this loop.

1) Line 15 is a standalone for loop. Is this loop supposed to have a body? Effectively, all it does is set x to 0. But since x = 0, when the program reaches the switch blocks nothing happens.

try to put "break;" statement after each "case" ends. and then try to run your code again. hope it helps.

You want the fallthrough in this case.

@OP, I think you're looking for something like this:

#include <iostream>

using namespace std;

int main()
{
    const int    DAYS_OF_CHRISTMAS               = 12;
    const string DAY_NUMERALS[DAYS_OF_CHRISTMAS] =
        {"first"  , "second", "third", "fourth", "fifth"   , "sixth",
         "seventh", "eight" , "ninth", "tenth" , "eleventh", "twelfth"};

    // Print the result for every day. If you want it for a single day just replace
    // the loop with alternative code that determines the say. (e.g. user input)
    for (int day = 0; day < DAYS_OF_CHRISTMAS; day++)
    {
        cout<< "On the " << DAY_NUMERALS[day] << " day of Christmas\n"
            << "My true love sent to me...\n";

        switch (day)
        {
            case 11:    cout << "12 drummers drumming,\n";
            case 10:    cout << "11 Pipers piping, \n";
            case 9:     cout << "10 Lords a leaping,\n";
            case 8:     cout << "9 Ladies dancing,\n";
            case 7:     cout << "8 Maids milking,\n";
            case 6:     cout << "7 Swans swimming,\n";
            case 5:     cout << "6 Geese laying,\n";
            case 4:     cout << "5 Golden rings,\n";
            case 3:     cout << "4 Calling birds,\n";
            case 2:     cout << "3 French hens,\n";
            case 1:     cout << "2 Turtle doves,\nAnd ";
            case 0:     cout << "a partridge in a pear tree.\n";
        }
        cout << "\n\n"; // Seperate the different days.
     }

     return 0;
}

Under the assumption that you're trying to print days "x" till 1 (outer for loop) you could modify your code as follows. The main change is that the inner loop is removed, it is not needed.

#include <iostream>
#include <stdlib.h>

using namespace std;

int main(int argc, char *argv[])
{
    int x;

    cout<< "Enter a day: ";
    cin>> x;

    // Print every day, counting down from the one entered.
    while (x > 0)
    {
        switch (x)
        {
            case 1:cout<< "On the first day of Christmas\nMy true love sent to me...\n"; break;
            case 2:cout<< "On the second day of Christmas\nMy true love sent to me...\n"; break;
            case 3:cout<< "On the third day of Christmas\nMy true love sent to me...\n"; break;
            case 4:cout<< "On the fourth day of Christmas\nMy true love sent to me...\n"; break;
            case 5:cout<< "On the fifth day of Christmas\nMy true love sent to me...\n"; break;
            case 6:cout<< "On the sixth day of Christmas\nMy true love sent to me...\n"; break;
            case 7:cout<< "On the seventh day of Christmas\nMy true love sent to me...\n"; break;
            case 8:cout<< "On the eight day of Christmas\nMy true love sent to me...\n"; break;
            case 9:cout<< "On the ninth day of Christmas\nMy true love sent to me...\n"; break;
            case 10:cout<< "On the tenth day of Christmas\nMy true love sent to me...\n"; break;
            case 11:cout<< "On the eleventh day of Christmas\nMy true love sent to me...\n"; break;
            case 12:cout<< "On the twelfth day of Christmas\nMy true love sent to me...\n"; break;
            default: cout << "On this cursed day of Christmas\nMy true love sent to me...\n"; break;
        }

        switch (x)
        {
            default: cout << "Fruit from some poisonous tree\n"; return 0;
            case 12:cout<< "12 drummers drumming\n";
            case 11:cout<< "11 Pipers piping \n";
            case 10:cout<< "10 Lords a leaping\n";
            case 9:cout<< "9 Ladies dancing\n";
            case 8:cout<< "8 Maids milking\n";
            case 7:cout<< "7 Swans swimming\n";
            case 6:cout<< "6 Geese laying\n";
            case 5:cout<< "5 Golden rings\n";
            case 4:cout<< "4 Calling birds\n";
            case 3:cout<< "3 French hens\n";
            case 2:cout<< "2 Turtle doves\nAnd ";
            case 1:cout<< "a partridge in a pear tree\n";
         }
         x--;
         cout << endl;
    }
    return EXIT_SUCCESS;
}
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.