Thread: need help with a c program (12 days of christmas)

  1. #1
    Registered User
    Join Date
    Sep 2013
    Posts
    23

    need help with a c program (12 days of christmas)

    Code:
    int main ()
    {
        int day;
    
        printf("Enter the number of the required verse\n");
        scanf("%d, &day");
    
        if (day < 1 || day > 13)
           {
            printf("No such verse");
            }
        else {
        
         printf("On the");
        
    
        switch(day)
        {
        case 1:
        printf("1st");
        break;
        case 2:
        printf("2nd");
        break;
        case 3:
        printf("3rd");
        break;
        default:
        printf(day+"th");
        }
    
        printf("day of Christmas my true love gave to me,");
    
        switch(day)
        {
        case 12:
        printf("Twelve Drummers Drumming");
        case 11:
        printf("Eleven Pipers Piping" );
        case 10:
        printf("Ten Lords A-leaping" );
        case 9:
        printf("Nine Ladies Dancing" );
        case 8:
        printf("Eight Maids A-milking" );
        case 7:
        printf("Seven Swans A-swimming");
        case 6:
        printf("Six Geese A-laying" );
        case 5:
        printf("Five Golden Rings" );
        case 4:
        printf("Four Calling Birds" );
        case 3:
        printf("Three French Hens" );
        case 2:
        printf("Two Turtle Doves, and" );
        case 1:
        printf("A partridge in a pear tree");
        }
    
        return 0;
    }
    i am not sure what i am doing wrong. i tried running the program and it keeps crashing. Please Help! (I'm dieing here!) any help would be much appreciated. thank you for all your help in advance.

  2. #2
    Ultraviolence Connoisseur
    Join Date
    Mar 2004
    Posts
    555
    line 30 you're trying to add an integer to a string. C doesn't allow concatenation that way.

    In your case you probably just want something like: printf("%dth", day). Another issue is that you're not printing a \n (newline) character ever, and so your output is probably not getting printed (buffer is not flushed out).

  3. #3
    Registered User
    Join Date
    Sep 2013
    Posts
    23
    Thanks for the reply.
    but I tried your advise and the program is still crashing.
    This is the updated code:

    Code:
    int main ()
    {
        int day;
    
        printf("Enter the number of the required verse\n");
        scanf("%d, &day");
    
        if (day < 1 || day > 13)
           {
            printf("No such verse");
            }
        else
        {
        printf("On the");
    
    
        switch(day)
        {
        case 1:
        printf("1st");
        break;
        case 2:
        printf("2nd");
        break;
        case 3:
        printf("3rd");
        break;
        case 4:
        printf("4th");
        break;
        case 5:
        printf("5th");
        break;
        case 6:
        printf("6th");
        break;
        case 7:
        printf("7th");
        break;
        case 8:
        printf("8th");
        break;
        case 9:
        printf("9th");
        break;
        case 10:
        printf("10th");
        break;
        case 11:
        printf("11th");
        break;
        case 12:
        printf("12th");
        }
    
        printf("day of Christmas my true love gave to me,\n");
    
        switch(day)
        {
        case 12:
        printf("Twelve Drummers Drumming\n");
        case 11:
        printf("Eleven Pipers Piping\n");
        case 10:
        printf("Ten Lords A-leaping\n");
        case 9:
        printf("Nine Ladies Dancing\n");
        case 8:
        printf("Eight Maids A-milking\n");
        case 7:
        printf("Seven Swans A-swimming\n");
        case 6:
        printf("Six Geese A-laying\n");
        case 5:
        printf("Five Golden Rings\n");
        case 4:
        printf("Four Calling Birds\n");
        case 3:
        printf("Three French Hens\n");
        case 2:
        printf("Two Turtle Doves, and\n");
        case 1:
        printf("A partridge in a pear tree\n");
        }
    
        return 0;
    }
    if you could tell me my mistakes that would be great. thanks so much for your help again.

  4. #4
    Ultraviolence Connoisseur
    Join Date
    Mar 2004
    Posts
    555
    line 6 your scanf is wrong. You have the second argument as part of your quote.

  5. #5
    Registered User
    Join Date
    Sep 2013
    Posts
    23
    I'm sorry. I'm not sure what you mean. I am a beginner at coding and am not sure what you mean by saying I have the second argument as part of my quote. If you could elaborate for me, it would be much appreciated.

  6. #6
    Registered User
    Join Date
    Sep 2013
    Posts
    23
    can anybody help me, I'm seriously ripping out my hair here. PLEASE HELP!

  7. #7
    Ultraviolence Connoisseur
    Join Date
    Mar 2004
    Posts
    555
    Here are the warnings you should be seeing on your compiler:
    Code:
    fail.c: In function ‘main’:
    fail.c:8:5: warning: format ‘%d’ expects a matching ‘int *’ argument [-Wformat]
    fail.c:31:5: warning: format not a string literal and no format arguments [-Wformat-security]
    fail.c:65:1: error: expected declaration or statement at end of input
    fail.c:65:1: warning: control reaches end of non-void function [-Wreturn-type]
    If you used proper indentation (and an editor that enforces it) you'd notice that you're also missing a closing } bracket on your else condition. Read and understand the above warnings and you should be able to figure out whats wrong with your scanf() statement.

  8. #8
    Registered User
    Join Date
    Sep 2013
    Posts
    23
    ok, i got it working. thank you so much for all of your help. here is the new code.

    Code:
    int main ()
    {
        int day;
    
        day=0;
        printf("Enter the number of the required verse\n");
        scanf("%d", &day);
    
        if (day < 1 || day > 12)
        {
            printf("No such verse");
            return 0;
        }
        else
        {
        printf("On the");
        }
    
    
        switch(day)
        {
        case 1:
        printf("1st");
        break;
        case 2:
        printf("2nd");
        break;
        case 3:
        printf("3rd");
        break;
        case 4:
        printf("4th");
        break;
        case 5:
        printf("5th");
        break;
        case 6:
        printf("6th");
        break;
        case 7:
        printf("7th");
        break;
        case 8:
        printf("8th");
        break;
        case 9:
        printf("9th");
        break;
        case 10:
        printf("10th");
        break;
        case 11:
        printf("11th");
        break;
        case 12:
        printf("12th");
        }
    
        printf("day of Christmas my true love gave to me,\n");
    
        switch(day)
        {
        case 12:
        printf("Twelve Drummers Drumming\n");
        case 11:
        printf("Eleven Pipers Piping\n");
        case 10:
        printf("Ten Lords A-leaping\n");
        case 9:
        printf("Nine Ladies Dancing\n");
        case 8:
        printf("Eight Maids A-milking\n");
        case 7:
        printf("Seven Swans A-swimming\n");
        case 6:
        printf("Six Geese A-laying\n");
        case 5:
        printf("Five Golden Rings\n");
        case 4:
        printf("Four Calling Birds\n");
        case 3:
        printf("Three French Hens\n");
        case 2:
        printf("Two Turtle Doves, and\n");
        case 1:
        printf("A partridge in a pear tree\n");
        }
    
        return 0;
    }
    I just have one quick question. when i print say verse 2
    i get
    On the2ndday of Christmas my true love gave to me,
    Two Turtle Doves, and
    A partridge in a pear tree

    my question is how can i space out the "the" and the "day" so they are not bunched up.
    again thank you so much for your help.

  9. #9
    Registered User HelpfulPerson's Avatar
    Join Date
    Jun 2013
    Location
    Over the rainbow
    Posts
    288
    Quote Originally Posted by tshort82392 View Post
    ok, i got it working. thank you so much for all of your help. here is the new code.

    Code:
    int main ()
    {
        int day;
    
        day=0;
        printf("Enter the number of the required verse\n");
        scanf("%d", &day);
    
        if (day < 1 || day > 12)
        {
            printf("No such verse");
            return 0;
        }
        else
        {
        printf("On the");
        }
    
    
        switch(day)
        {
        case 1:
        printf("1st");
        break;
        case 2:
        printf("2nd");
        break;
        case 3:
        printf("3rd");
        break;
        case 4:
        printf("4th");
        break;
        case 5:
        printf("5th");
        break;
        case 6:
        printf("6th");
        break;
        case 7:
        printf("7th");
        break;
        case 8:
        printf("8th");
        break;
        case 9:
        printf("9th");
        break;
        case 10:
        printf("10th");
        break;
        case 11:
        printf("11th");
        break;
        case 12:
        printf("12th");
        }
    
        printf("day of Christmas my true love gave to me,\n");
    
        switch(day)
        {
        case 12:
        printf("Twelve Drummers Drumming\n");
        case 11:
        printf("Eleven Pipers Piping\n");
        case 10:
        printf("Ten Lords A-leaping\n");
        case 9:
        printf("Nine Ladies Dancing\n");
        case 8:
        printf("Eight Maids A-milking\n");
        case 7:
        printf("Seven Swans A-swimming\n");
        case 6:
        printf("Six Geese A-laying\n");
        case 5:
        printf("Five Golden Rings\n");
        case 4:
        printf("Four Calling Birds\n");
        case 3:
        printf("Three French Hens\n");
        case 2:
        printf("Two Turtle Doves, and\n");
        case 1:
        printf("A partridge in a pear tree\n");
        }
    
        return 0;
    }
    I just have one quick question. when i print say verse 2
    i get
    On the2ndday of Christmas my true love gave to me,
    Two Turtle Doves, and
    A partridge in a pear tree

    my question is how can i space out the "the" and the "day" so they are not bunched up.
    again thank you so much for your help.
    To answer your question, you simply add a space. I'm guessing you made it more complex than it really is in your mind.

    Really though, this program would be a lot better if you used functions, arrays, and a constant value. Something like this is how I would write it :

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    
    #define NUMBER_OF_VERSES 12
    
    
    void check_input( int day )
    {
        if ( day < 1 || day > 12 )
        {
            printf("Invalid input : Must be between 1 and 12!\n");
            exit( 1 );
        }
    
    
        return;
    }
    
    
    int get_input( )
    {
        int day = 0;
    
    
        printf("Enter the number of the verse : ");
        scanf("%d", &day);
    
    
        check_input( day );
    
    
        return day;
    }
    
    
    void print_lyrics( const int day )
    {
        int lyric_number = day;
    
    
        const char * number_suffixes[3] =
        {
            "st",
            "nd",
            "rd"
        };
    
    
        const char * verses[NUMBER_OF_VERSES] =
        {
            "A partridge in a pear tree\n",
            "Two Turtle Doves, and\n",
            "Three French Hens\n"
            "Four Calling Birds\n",
            "Five Golden Rings\n",
            "Six Geese A-laying\n",
            "Seven Swans A-swimming\n",
            "Eight maids A-milking\n",
            "Nine Ladies Dancing\n",
            "Ten Lords A-leaping\n",
            "Eleven Pipers Piping\n",
            "Twelve Drummers Drumming\n"
        };
    
    
        printf("\nOn the %d%s day of Christmas my true love gave to me,\n", day, day < 4 ? number_suffixes[day - 1] : "th" );
    
    
        for ( ; lyric_number; lyric_number-- )
            printf( "%s", verses[lyric_number - 1]);
    }
    
    
    int main( )
    {
        print_lyrics( get_input( ) );
    
    
        return 0;
    }
    Notice I also used the ternary operator to correctly format each day, and used arrays to take care of printing out each lyric. You're probably not at this level of C yet though, I just wanted to show you how it would look if it was more properly done.
    "Some people think they can outsmart me, maybe. Maybe. I've yet to meet one that can outsmart bullet" - Meet the Heavy, Team Fortress 2

  10. #10

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. 12 days of Christmas
    By CrEatuRe_X in forum C++ Programming
    Replies: 2
    Last Post: 06-05-2013, 09:22 AM
  2. Replies: 5
    Last Post: 12-04-2011, 03:32 AM
  3. Days of the week program
    By Chinnie15 in forum C Programming
    Replies: 4
    Last Post: 11-15-2011, 05:11 PM
  4. learn C In 21 days Appendix F ERRORS (bug in program)
    By Paul Tidwell in forum C Programming
    Replies: 6
    Last Post: 06-19-2011, 11:00 PM
  5. What I Wan't For Christmas!
    By no-one in forum A Brief History of Cprogramming.com
    Replies: 34
    Last Post: 09-26-2001, 05:00 PM