Thread: Printing a Calendar - Indenting the First Line

  1. #1
    Registered User
    Join Date
    Mar 2011
    Posts
    16

    Printing a Calendar - Indenting the First Line

    Write a program to generate a calendar for a year. The program should accept the year and the day of the week for January 1 of that year (0 = Sunday, 1 = Monday, etc.).

    I've written the whole program and it works, except I can't get the first week of the month to indent the correct amount of spaces. Can anyone help me out?

    Code:
    #include <stdio.h>
    
    #define SUNDAY 0
    #define MONDAY 1
    #define TUESDAY 2
    #define WEDNESDAY 3
    #define THURSDAY 4
    #define FRIDAY 5
    #define SATURDAY 6
    
    void instructions(void);
    int CalYear(void);
    void PrintCal(int year);
    void PrintCalMonth(int month, int year);
    char* MonthName(int month);
    int MonthDays(int month, int year);
    int FirstDay(int month, int year);
    void Indent(int days);
    int LeapYear(int year);
    
    
    int main(void)
    {
    	int year;
    
    	instructions();
    	year = CalYear();
    	PrintCal(year);
    
    	return 0;
    }
    
    
    void instructions(void)
    {
        printf("This program displays a calendar for a full year. The year must be from 1900 or later.\n");
    }
    
    
    int CalYear(void)
    {
    	int year;
    	printf("Enter the year: \n");
    	scanf("%d", &year);
    	if (year >= 1900)
    	return (year);
    	else
    	printf("The year must be 1900 or later.\n");
    }
    
    
    void PrintCal(int year)
    {
    	int month;
    
    	for (month = 1; month <= 12; month++)
    	{
    		PrintCalMonth(month, year);
    		printf("\n");
    	}
    }
    
    
    void PrintCalMonth(int month, int year)
    {
    	int i, weekday, dayofmonth, daysinmonth;
    
    	printf("%s %d\n", MonthName(month), year);
    	printf("Su Mo Tu We Th Fr Sa\n");
    	daysinmonth = MonthDays(month, year);
    	weekday = firstday(month, year);
    	Indent(weekday);
    	for (dayofmonth = 1; dayofmonth <= daysinmonth; dayofmonth++)
    	{
    		if (dayofmonth < 10)
    		printf("%2d ", dayofmonth);
    		else
    		printf("%d ", dayofmonth);
    		if (weekday == SATURDAY)
    		printf("\n");
    		weekday = (weekday + 1) % 7;
    	}
    	if (weekday != SUNDAY)
    	printf("\n");
    }
    
    
    char* MonthName(int month)
    {
    	switch (month)
    	{
    		case  1:
    		return ("January");
    		case  2:
    		return ("February");
    		case  3:
    		return ("March");
    		case  4:
    		return ("April");
    		case  5:
    		return ("May");
    		case  6:
    		return ("June");
    		case  7:
    		return ("July");
    		case  8:
    		return ("August");
    		case  9:
    		return ("September");
    		case 10:
    		return ("October");
    		case 11:
    		return ("November");
    		case 12:
    		return ("December");
    		default:
    		return ("Please enter a month between 1 and 12");
    	}
    }
    
    
    int MonthDays(int month, int year)
    {
    	switch (month)
    	{
    		case 2:
    			if (leapyear(year))
    			return (29);
    			return (28);
    		case 4:	case 6: case 9: case 11:
    		return (30);
    		default:
    		return (31);
    	}
    }
    
    
    
    int firstday(int month, int year)
    {
    	int weekday, i;
    
    	weekday = MONDAY;
    	for (i = 1900; i < year; i++)
    	{
    		weekday = (weekday + 365) % 7;
    		if (leapyear(i))
    		weekday = (weekday + 1) % 7;
    	}
    
    	for (i = 1; i < month; i++)
    	{
    		weekday = (weekday + MonthDays(i, year)) % 7;
    	}
    
    	return (weekday);
    }
    
    
    void Indent(int weekday)
    {
    	int i;
    
    	for (i = 0; i < weekday; i++);
    	{
    		printf(" ");
    	}
    }
    
    
    
    int leapyear(int year)
    {
    	if (year % 400 == 0)
    	return 1;
    	if (year % 100 == 0)
    	return 0;
    	if (year % 4 == 0)
    	return 1;
    	return 0;
    }
    Last edited by jacobsh47; 03-31-2011 at 08:28 AM.

  2. #2
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,834
    Since your days are formatted "%2d " that means you are allowing 3 spaces per day.
    In the Indent() loop, you should therefore print three spaces per.
    Code:
    printf("   "); /* three spaces */

  3. #3
    Registered User
    Join Date
    May 2010
    Posts
    4,633
    When I compile this program I get the following error/warning messages:
    main.c||In function ‘PrintCalMonth’:|
    main.c|223|error: implicit declaration of function ‘firstday’|
    main.c|218|warning: unused variable ‘i’|
    main.c||In function ‘MonthDays’:|
    main.c|279|error: implicit declaration of function ‘leapyear’|
    main.c|291|warning: no previous declaration for ‘firstday’|
    main.c|324|warning: no previous declaration for ‘leapyear’|
    ||=== Build finished: 2 errors, 3 warnings ===|
    Remember that C is case sensitive firstday != Firstday != FirstDay .


    Jim

  4. #4
    Registered User
    Join Date
    Mar 2011
    Posts
    16
    Jim - I fixed the program so that FirstDay is throughout.

    nonoob - I've tried using the three spaces in the program but it still doesn't work. The program just indents every line only three spaces instead of three for every day.

  5. #5
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    On your first row, the formula is to indent 3 spaces for every day more than Su, minus 1, +1 space.

    for example, the first day of the month is Wednesday:

    Wednesday is the fourth day, so it's indentation should be 3 * 3 +1

  6. #6
    Registered User
    Join Date
    Mar 2011
    Posts
    16
    I don't understand what you're referring to by my "first row."

    Could you explain a little bit more what you mean?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C# Printing Problem
    By silverlight001 in forum C# Programming
    Replies: 0
    Last Post: 03-23-2009, 01:13 AM
  2. Printing Lines to .txt File
    By Programmer3922 in forum C Programming
    Replies: 2
    Last Post: 08-02-2008, 12:45 PM
  3. adding line numbers and concatenating a filename
    By durrty in forum C Programming
    Replies: 25
    Last Post: 06-28-2008, 03:36 AM
  4. Trouble replacing line of file
    By Rpog in forum C Programming
    Replies: 4
    Last Post: 04-19-2004, 10:22 AM
  5. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 01:27 PM