I am writing a calendar in C. I'm still very new to the language. I think i wrote most of the functions/algorithms correctly (it would be nice if you guys could check for any errors too). Now I'm having a tough time printing the calendar out In a correct format. For example, if I enter this data: 12/x/2010 to getMonthYear(). I am trying to get my calendar to start in the correct day, so the 1st is on Wednesday, 2nd Thursday, 3rd Friday... I should be able to enter any month and year and get the day to correspond with the correct weekday. All of this is going to have to be in the prototype printCalendar(). Any help is much appreciated.

#include <stdio.h>
#define MAX 31
#define MID 30
#define MIN 28
#define FOUR 4
#define ONEHUN 100
#define FOHUN 400
#define BEGIN 1900
#define YEAR 365

//Prototypes:
void getMonthYear(int *month, int *year);
int toJulian(int month, int day, int year);
int dayInMonth(int month, int year);
int leapYear(int year);
long yearsToDays(int year);
void printCalendar(int startDay, int numDays);
void printHeader();

void main(void)
{
	int day, month, year, startDay, numDays;
	
	startDay = 1;
	numDays = 0;
	day = 0;

	getMonthYear(&month, &year);
	toJulian(month, day, year);
	yearsToDays(year);
	printCalendar(startDay, numDays);
}
//Ask user to imput data
void getMonthYear(int *month, int *year)
{
	printf("Enter Month: ");
	scanf_s("%d", month);
	printf("Enter Year: ");
	scanf_s("%d", year);
	printf("\n\n");
}
//Takes a calendar date and calculates its julian day within that year
int toJulian(int month, int day, int year)
{
	--month;
	while(month != 0)
	{
		day += daysInMonth(month, year);
		--month;
	}
	
	return day;
}
//Takes a month and year, calculates how many days are in this particular month
int daysInMonth(int month, int year)
{
	switch (month)
	{
		case 1 : case 3 : case 5 : case 7 : case 8 : case 10 : case 12 :
			return MAX;
			break;
		case 4 : case 6 : case 9 : case 11 :
			return MID;
			break;
		case 2 :
			return MIN + leapYear(year);
			break;
	}
}
//Takes a year, returns 1 if this is a leap year, 0 otherwise
int leapYear(int year)
{
	if (year % FOUR == 0 && (year % ONEHUN != 0 || year % FOHUN == 0))
		return 1;
	else 
		return 0;
}
//Takes a year, returns the number of days from 1/1/1900 to the end of the previous year
long yearsToDays(int year)
{
	long days = 0;

	while(year != BEGIN)
	{
		--year;
		days += YEAR + leapYear(year);
	}

	return days;
}
//Outputs the calendar to the screen.
void printCalendar(int startDay, int numDays)
{
	printHeader();



}
void printHeader()
{
	printf("%4s%4s%4s%4s%4s%4s%4s \n", "Su", "M", "Tu", "W", "Th", "F", "Sa");
}

Recommended Answers

All 7 Replies

I tried compiling your code but you have MS specific functionality....scanf_s doesn't work with Linux...

For printing the calendar for the month, you can use this pseudo code:

print your days of the week on a line with a couple spaces between each day

day equals 1
for row=0, and each row less than 5, increment row
   if row is 0
     for col=0 and each col less than 7, increment col
       if col less than firstNum
         print 4-5 spaces: "     "
       else
         print day 
   else
     for col=0 and each col less than 7, increment col
       increment day
       if day greater than daysInMonth
          break out   
       print field two digits wide, day + a space or two
     
     print newline '\n'
   end of else
 end of for

end of function

That will get you started

For printing the calendar for the month, you can use this pseudo code:

print your days of the week on a line with a couple spaces between each day

day equals 1
for row=0, and each row less than 5, increment row
   if row is 0
     for col=0 and each col less than 7, increment col
       if col less than firstNum
         print 4-5 spaces: "     "
       else
         print day 
   else
     for col=0 and each col less than 7, increment col
       increment day
       if day greater than daysInMonth
          break out   
       print field two digits wide, day + a space or two
     
     print newline '\n'
   end of else
 end of for

end of function

That will get you started

thank you for all the help. i understand now. and sorry i forgot to mention that i as using MS.

You're welcome. ;)

You're welcome. ;)

sorry for all the extra posts, but i didn't fully understand the beginning of the pseudo code.

the calendar should look something like this for this particular month/year
1st days start on tuesday

i use visual studio BTW
compiling errors:
warning C4013: 'daysInMonth' undefined; assuming extern returning int
warning C4715: 'daysInMonth' : not all control paths return a value

Enter month: 5
Enter year: 2018
  Su   M   Tu  W  Th   F   Sa
           1   2   3   4    5
   6   7   8   9   10  11  12
  13  14   15  16  17  18  19
  20  21   22  23  24  25  26
  27  28   29  30  31

this i what i have so far for printCalendar() but it doesnt print out the spaces the way they need to be.

void printCalendar(int startDay, int numDays)
{
	int i, col, day;

	printf("days: %d\n", numDays);
	printHeader();
	day = 1;

	for(i = 0; i < 5; ++i)
	{
		if(i == 0)
		{
			for(col = 0; col < 7; ++col)
				if(col < numDays)
					printf("");
				else
					printf("%4d", startDay);
		}
		else 
		{
			for(col = 0; col < 7; ++col)
			{
				++startDay;
				if(startDay > numDays)
					break;
				printf("%4d", startDay);
			}
			printf("\n");
		}

	}

}

Your spacing is a bit different than the spacing for the last calendar post, so I'll need to adjust that.

Let's see what you have for your spacing, ...

/* prints up a monthly calender

*/

#include<stdio.h>

int main() {
  int totalDays,day, i, r, c, firstDay;

  printf("\n\n\n");
  printf("Input Number of days in a month: ");
  //scanf("%d",&totalDays);
  //(void) getchar();
  totalDays=31;
  printf("Input the first day of the month: \n");
  //scanf("%d",&firstDay);
  //(void) getchar();
  firstDay=5;
  printf(" Su   M  Tu   W  Th   F  Sa\n");

  for(r=0,day=0;r<5;r++) {
    if(r==0) {
      for(c=0;c<7;c++) {
        //if(c==1 || c==3 || c==5)
         // putchar(' ');
        if(c<firstDay) 
          printf("    ");
        else
          printf(" %2d ", ++day);
      }
      putchar('\n');
    }
    for(c=0;c<7;c++) {
      if(++day>totalDays)
        break;
      //if(c==1||c==3 ||c==5)
       // putchar(' ');
      printf(" %2d ", day); 
    }
    printf("\n");
  } 
  printf("\n\n\t\t\t    press enter when ready");
  (void) getchar();
  return 0;
}

run this, and let me know how the spacing looks to you.

ok this is what i have came up with so far. now the only problem is that the spacing is off by one. For example, the moth/year that ive been testing is 12/x/2010. on this particular month the 1st day starts on wednesday, but my calendar starts on tuesday. i can't figure it out. pls help.

//Patrick Gutierrez

#include <stdio.h>
#define MAX 31
#define MID 30
#define MIN 28
#define FOUR 4
#define ONEHUN 100
#define FOHUN 400
#define BEGIN 1900
#define YEAR 365

//Prototypes:
void getMonthYear(int *month, int *year);
int toJulian(int month, int day, int year);
int dayInMonth(int month, int year);
int leapYear(int year);
long yearsToDays(int year);
void printCalendar(int startDay, int numDays);
void printHeader();

void main(void)
{
	int day, month, year, startDay, numDays;
	
	startDay = 0;
	day = 0;

	getMonthYear(&month, &year);
	startDay = (toJulian(month, day, year) + yearsToDays(year)) % 7;
	numDays = daysInMonth(month, year);
	printCalendar(startDay, numDays);
}
//Ask user to imput data
void getMonthYear(int *month, int *year)
{
	printf("Enter Month: ");
	scanf_s("%d", month);
	printf("Enter Year: ");
	scanf_s("%d", year);
	printf("\n\n");
}
//Takes a calendar date and calculates its julian day within that year
int toJulian(int month, int day, int year)
{
	while(month != 1)
	{
		--month;
		day += daysInMonth(month, year);
	}
	
	return day;
}
//Takes a month and year, calculates how many days are in this particular month
int daysInMonth(int month, int year)
{
	switch (month)
	{
		case 1 : case 3 : case 5 : case 7 : case 8 : case 10 : case 12 :
			return MAX;
			break;
		case 4 : case 6 : case 9 : case 11 :
			return MID;
			break;
		case 2 :
			return MIN + leapYear(year);
			break;
		default :
			printf("Not a Month!");
			break;
	}
}
//Takes a year, returns 1 if this is a leap year, 0 otherwise
int leapYear(int year)
{
	if (year % FOUR == 0 && (year % ONEHUN != 0 || year % FOHUN == 0))
		return 1;
	else 
		return 0;
}
//Takes a year, returns the number of days from 1/1/1900 to the end of the previous year
long yearsToDays(int year)
{
	long days = 0;

	while(year != BEGIN)
	{
		--year;
		days += YEAR + leapYear(year);
	}

	return days;
}
//Outputs the calendar to the screen.
void printCalendar(int startDay, int numDays)
{
	int i, col = 0;
	printHeader();
	for(i = 0; i < startDay; i++)
	{
		printf("%4s", " ");
		++col;
	}
	for(i = 1; i <= numDays; i++)
	{
		++col;
		printf("%4d", i);
		if(col % 7 == 0)
		{
			printf("\n");
			col = 0;
		}
	}
	printf("\n");
}
void printHeader()
{
	printf("%4s%4s%4s%4s%4s%4s%4s \n", "Su", "M", "Tu", "W", "Th", "F", "Sa");
}
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.