Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Incrementing case number in a function?

I'm just learning C and I'm having trouble making my code increment the dayName on the same printf function.

Here's my code:

#include <stdio.h>
#define LENGH_OF_WEEK 7

int main()
{
    int daysOfWeek[LENGH_OF_WEEK] = {0,1,2,3,4,5,6};
    char* dayName = NULL;

int i;
for (i = 0; i < LENGH_OF_WEEK; i++)
{
    switch (daysOfWeek[i])
    {
    case 0: dayName = "Sunday"; break;
    case 1: dayName = "Monday"; break;
    case 2: dayName = "Tuesday"; break;
    case 3: dayName = "Wednesday"; break;
    case 4: dayName = "Thursday"; break;
    case 5: dayName = "Friday"; break;
    case 6: dayName = "Saturday"; break;
    }

    printf("%s, %s Happy Days\n", dayName, dayName);
}

return 0;
}

As you can see, I'm trying to get my console to sing Happy Days, but it just says the dayName twice and I would like to figure out how to make it say "Saturday, what a day! Groovin' all week with you!" for case 6.

like image 598
LauraKnight Avatar asked Dec 03 '25 22:12

LauraKnight


1 Answers

Here's my version of the classic theme song. Not using a switch statement, sorry

#include <stdio.h>


char *day[] = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};
int main()
{

int i;
for (i = 0; i < 5; i+=2)
{
    printf("%s, %s Happy Days\n", day[i], day[i+1]);
}
printf("Saturday, what a day\n Groovin' all week with you!\n");

return 0;
}
like image 71
Vorsprung Avatar answered Dec 06 '25 14:12

Vorsprung