Originally Posted by
tshort82392
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.