I am trying towrite a C program which will print yearly calendars, given as input the year numbers. Input data will be taken from a text file, which will contain an unknown number of data lines, each containing a single integer giving a year,

I first created a function to find the first day of the year:

int find_jan_first 
(int year)               /* the input year, must be >= MIN_YEAR */

/* 

Calculates the day of the week on which January 1 falls for given year.
We start the formula at 5, because January 1, 1582 was a Friday.
Then the day of the week is advanced by one for each year, adding the 
number of included leap years, because in leap years January 1st
advances by 2 days and in regular years it advances by 1 day. 

*/

{  /* find_jan_first */

   int firstDayInJan; /* 0 is Sunday, 1 is Monday, ..., 6 is Saturday */

   /* starting on January 1, 1582, add days to find the first day */
   /* in January of the year chosen */
   firstDayInJan = ((5 + (year - MIN_YEAR) + 
   count_leap_years(year)) % DAYS_PER_WEEK);

   return (firstDayInJan);

}  /* find_jan_first */

and also a function to find leap year:

int count_leap_years 
(int year)               /* user's choice of year, must be >= MIN_YEAR */

/*

Calculates the number of leap years since 1582 to the current year. 
This function returns the numbers of leap years since the given year. 
Count does not include the given year if it is a leap year. 

*/

{  /* count_leap_years */

   int leapYears;     /* number of leap years to return */
   int hundreds;      /* number of years multiple of a hundred */
   int fourHundreds;  /* number of years multiple of four hundred */

   /* determine number of years in interval that are a multiple of 4 */
   leapYears = (year - (MIN_YEAR - 1)) / 4;

   /* determine number of years in interval that are a multiple of 100 */
   /* and subtract, because they are not leap years */
   hundreds = (year - 1501) / 100;
   leapYears -= hundreds;

   /* determine number of years in interval that are a multiple of 400 */
   /* and add these back in, since they are leap years */
   fourHundreds = (year - 1201) / 400;
   leapYears += fourHundreds;

   return (leapYears);

}  /* count_leap_years */

I know the MIN_Year is 1582

and I think that you would put in input by inFile = fopen (fileName, "r");.

What I am having trouble with is the actual loop to begin show the final product looks like this.

1985           

          January

     S  M  T  W  T  F  S

     --------------------

            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
 

 

          February

     S  M  T  W  T  F  S

     --------------------

                     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

Well looping would be a simple way to draw it! Does this have to be a console app? Many Win32 Functions are available for arranging text or characters at co-ordinates on a window....

It does not have to be a looping funciton I just thought it was the best way to do it, what was your idea I would like to hear it, 1o0oBhP.

Sorry for the late reply Ive been away at uni working hard! As for drawing the calendar why not have a nested loop to draw the whole lot?

char days[] = { "S", "M", "T", "W", "T", "F", "S" }; // day abbrs

cout << "your month here" << "\n\n";

for(int i = 0; i < 7; i++)
    cout << days[i] << " ";
cout << "\n";

for(int i = 0; i < 14; i++)
    cout << "-";
cout << "\n";

This will print the header. Then you want to work out how many rows the calendar month will have. This will depend on what day it starts on and how long the month is. Have a go and see what you come up with. When you have the number of rows you can use a loop to put the correct numbers in the right places on the screen!

This is how I ended up writing the program.

#include <stdio.h>
#include <string.h>


#define MIN_YEAR 1582
#define MAX_YEAR 9999
#define DAYS_PER_WEEK 7
#define MAX_FILE_NAME_SIZE 25
#define MAX_MONTH_NAME_SIZE 10
#define PREVIOUSDAYSPACE 3

//function declarations
int count_leap_years(int);
int find_jan_first(int);
int print_month(int, int, int);

void print_calendar(int);
void print_dayNames();


int main()
{

	int year;
  
	char fileName[MAX_FILE_NAME_SIZE];
    FILE * inFile;                         //file pointer
	printf("Please enter a date file: "); // user enters text file
	scanf("%s",fileName);
	inFile = fopen (fileName,"r");
	while(!inFile)                   //error check if not valid file
	{
		printf("Try Again ");
		printf("Please enter a date file: ");
		scanf("%s",fileName);
		inFile = fopen (fileName,"r");
	}
	while ( fscanf ( inFile, "%d", &year )!= EOF ) //error if date not in Gregorian calendar
	{
		if (year < MIN_YEAR || year > MAX_YEAR)
		{
			printf("\nCalendar may be incorrect,\n"); 
			printf("the year may not exist in\n");
			printf("the Gregorian Calendar range.\n");
		}
		print_calendar (year);
	}
	
	fclose (inFile);
	
}

void print_calendar(int year)  //calling first day and year
{
	int firstday; //first day of month	
    int month;    // month of the year

	printf("\n       %d\n\n", year);
	firstday = find_jan_first(year);

	for(month= 1; month <= 12; month++)
	{
		firstday= print_month (month, firstday, year);
	}

}

int print_month (int month, int day, int year) //length of month and name of month
{
	
	int days, numspacefirst, spaces, datetoprint;

	char monthName[MAX_MONTH_NAME_SIZE];
	switch(month)
	{
	case 1: 
        days = 31;
		strcpy(monthName,"January");
        break;
    case 3: 
        days = 31;
		strcpy(monthName,"March");
        break;
    case 5: 
        days = 31;
		strcpy(monthName,"May");
        break;
    case 7: 
        days = 31;
		strcpy(monthName,"July");
        break;
    case 8: 
        days = 31;
		strcpy(monthName,"August");
        break;
    case 10: 
        days = 31;
		strcpy(monthName,"October");
        break;
    case 12: 
        days = 31;
		strcpy(monthName,"December");
        break;
    case 4: 
        days = 30;
		strcpy(monthName,"April");
        break;
    case 6: 
        days = 30;
		strcpy(monthName,"June");
        break;
    case 9: 
        days = 30;
		strcpy(monthName,"September");
        break;
    case 11:
        days = 30;
		strcpy(monthName,"November");
        break;
    case 2: 
        days = 28 +
			   1 * (count_leap_years(year+1) -count_leap_years(year));
        strcpy(monthName,"February");
	}

	printf("     %s\n", monthName);
	print_dayNames();
	numspacefirst = PREVIOUSDAYSPACE * day;   //spacing of date numbers
	
	for (spaces=0;spaces<numspacefirst;spaces++)
	{
		printf("%c",' ');
	}
	for (datetoprint=1;datetoprint<=days;datetoprint++)
	{
		printf("%2d",datetoprint);
		printf("%c", ' ');
		day++;
		if (day == DAYS_PER_WEEK)
		{
			day = 0;
			printf("\n");
		}
	}
	printf("\n\n");

	return day;

}// find the day

void print_dayNames() // print day identations
    {
    printf("S  M  T  W  T  F  S \n");
	printf("--------------------\n");
    }

int find_jan_first
(int year)              // the input year, must be >= MIN_YEAR 

{  // find_jan_first 

   int firstDayInJan; // 0 is Sunday, 1 is Monday, ..., 6 is Saturday 

   // starting on January 1, 1582, add days to find the first day 
   // in January of the year chosen 
   firstDayInJan = ((5 + (year - MIN_YEAR) + 
   count_leap_years (year)) % DAYS_PER_WEEK);

   return (firstDayInJan);

}  // find_jan_first 

int count_leap_years
(int year)               // user's choice of year, must be >= MIN_YEAR 

{  // count_leap_years 

   int leapYears;     // number of leap years to return 
   int hundreds;      // number of years multiple of a hundred 
   int fourHundreds;  // number of years multiple of four hundred 

   // determine number of years in interval that are a multiple of 4 
   leapYears = (year - (MIN_YEAR - 1)) / 4;

   // determine number of years in interval that are a multiple of 100 
   // and subtract, because they are not leap years 
   hundreds = (year - 1501) / 100;
   leapYears -= hundreds;

   // determine number of years in interval that are a multiple of 400 
   // and add these back in, since they are leap years 
   fourHundreds = (year - 1201) / 400;
   leapYears += fourHundreds;

   return (leapYears);

}  // count_leap_years
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.