Hi everyone,this program prompts a user for a leap yr code and a day code resulting in an entire year's calendar.The result displays months one below another.Can some one tell me how to make it print 2 months next to each other,that is JAN FEB then MAR APRIL below them.

#include <stdio.h>

main()

 //int print_calendar(year,day_code,leap_year)
 //{
{

    int day_code,days_in_month,leap_year,day,month;
    do
    {
        printf("Enter day and leap year codes:");
        scanf("%d%d",&day_code,&leap_year);
    }

    while (day_code <0 || day_code >6);

    for (month=1;month<=12;month++)
    {
        switch (month){/*print name and set days_in_month*/
           
        case 1:
            printf("\n\nJanuary");
            days_in_month=31;
            break;
       
        case 2:
            printf("\t\nFebruary");
            days_in_month= leap_year ?29 :28;
            break;
        case 3:
            printf("\n\nMarch");
            days_in_month=31;
            break;
        case 4:
            printf("\n\nApril");
            days_in_month=30;
            break;
        case 5:
            printf("\n\nMay");
            days_in_month=31;
            break;
        case 6:
            printf("\n\nJune");
            days_in_month=30;
            break;
        case 7:
            printf("\n\nJuly");
            days_in_month=31;
            break;
        case 8:
            printf("\n\nAugust");
            days_in_month=31;
            break;
        case 9:
            printf("\n\nSeptember");
            days_in_month=30;
            break;
        case 10:
            printf("\n\nOctober");
            days_in_month=31;
            break;
        case 11:
            printf("\n\nNovember");
            days_in_month=30;
            break;
        case 12:
            printf("\n\nDecember");
            days_in_month=31;
            break;
        }
        printf("\n\nSun Mon Tue Wed Thurs Fri Sat\n");
        /* advance printer to correct position for first date*/
        for(day =1;day <=1 +day_code*5;day++)
            printf("\t");
        /* print the dates for one month*/
        for(day=1;day<=days_in_month;day++)
        {
            printf("%2d",day);
            if((day+day_code)%7>0)/*before Sat ?*/
                /* move to next day in same week*/
                printf("  ");
            else/* skip to next line to start with Sun*/
                printf("");
        }
        /*set day_code for next month to begin*/
		
        day_code =(day_code +days_in_month)%7;
		
    }
}

yeah, i could probably help you with this, but unfortunately you didnt use code tags, so it's all but completely unreadable.

next time, try this:

[code=c] your code goes here

[/code]

okay, someone added code tags for you.

for one thing, your program output is all messed up, even for one month at a time. it looks like you took the original code and made a stab at modifiyting it, then posted the broken result here?

i guess before we go any further, have you considered using the built-in calendar and date library C? its called <time.h> and chock full of useful stuff.

anyhow, if you're going to continue down this path the only way you can print multiple column of months is to calculate each of the month on any particular row at the same time. so you'll need multiple variables for each column. like "days_in_month1" and "days_in_month2"

i got kind of curious and wound up doing it myself. with three months in each row. you can see the output here as an example

Attachments
rob@rob:~/Desktop/examples$ ./calandar
Enter day and leap year codes:4 0

_________January____________   _________February___________   _________March______________
Sun Mon Tue Wed Thu Fri Sat    Sun Mon Tue Wed Thu Fri Sat    Sun Mon Tue Wed Thu Fri Sat 
                  1   2   3      1   2   3   4   5   6   7      1   2   3   4   5   6   7 
  4   5   6   7   8   9  10      8   9  10  11  12  13  14      8   9  10  11  12  13  14 
 11  12  13  14  15  16  17     15  16  17  18  19  20  21     15  16  17  18  19  20  21 
 18  19  20  21  22  23  24     22  23  24  25  26  27  28     22  23  24  25  26  27  28 
 25  26  27  28  29  30  31                                    29  30  31                 

_________April______________   _________May________________   _________June_______________
Sun Mon Tue Wed Thu Fri Sat    Sun Mon Tue Wed Thu Fri Sat    Sun Mon Tue Wed Thu Fri Sat 
              1   2   3   4                          1   2          1   2   3   4   5   6 
  5   6   7   8   9  10  11      3   4   5   6   7   8   9      7   8   9  10  11  12  13 
 12  13  14  15  16  17  18     10  11  12  13  14  15  16     14  15  16  17  18  19  20 
 19  20  21  22  23  24  25     17  18  19  20  21  22  23     21  22  23  24  25  26  27 
 26  27  28  29  30             24  25  26  27  28  29  30     28  29  30                 

_________July_______________   _________August_____________   _________September__________
Sun Mon Tue Wed Thu Fri Sat    Sun Mon Tue Wed Thu Fri Sat    Sun Mon Tue Wed Thu Fri Sat 
              1   2   3   4                              1              1   2   3   4   5 
  5   6   7   8   9  10  11      2   3   4   5   6   7   8      6   7   8   9  10  11  12 
 12  13  14  15  16  17  18      9  10  11  12  13  14  15     13  14  15  16  17  18  19 
 19  20  21  22  23  24  25     16  17  18  19  20  21  22     20  21  22  23  24  25  26 
 26  27  28  29  30  31         23  24  25  26  27  28  29     27  28  29  30             

_________October____________   _________November___________   _________December___________
Sun Mon Tue Wed Thu Fri Sat    Sun Mon Tue Wed Thu Fri Sat    Sun Mon Tue Wed Thu Fri Sat 
                  1   2   3      1   2   3   4   5   6   7              1   2   3   4   5 
  4   5   6   7   8   9  10      8   9  10  11  12  13  14      6   7   8   9  10  11  12 
 11  12  13  14  15  16  17     15  16  17  18  19  20  21     13  14  15  16  17  18  19 
 18  19  20  21  22  23  24     22  23  24  25  26  27  28     20  21  22  23  24  25  26 
 25  26  27  28  29  30  31     29  30                         27  28  29  30  31         

rob@rob:~/Desktop/examples$

You basically have to
1) create both months at the same time
or
2) use a 2d array and load the calendar into the array. When done, print the array.

sorry about these errors in the above program.
line 75 and line 84 respectively

printf(" ") not \t
 printf("\n")

@jephthah-Thank you,I will look into <time.h>

This article has been dead for over six months. Start a new discussion instead.