Calendar Creating Program

xavier666 -3 Tallied Votes 371 Views Share

The program generates a calendar for one year. The program only asks for the day on which 1st January falls from user.
Notes :

  • Compiler - Turbo C++ 4.5
  • Leap year has been considered
  • The only reason for including conio.h is because of clrscr() . If somebody can give me a one-line alternative, I would be happy to modify the code
  • The user can either view a particular month or the whole year
  • Error handling has not been implemented (feeling lazy :yawn:)

Please don't flame me for using global variables. I don't know about the problems of global variables so enlighten me :P
PM me if you find any bugs or if you think it can be improved

jephthah commented: c code snippets are supposed to be useful. anything compiled in TurboC is useless for most of western civilization. if you wont stop using it, then at least stop subjecting others to it. -1
#include <stdio.h>
#include <conio.h>

typedef struct calender
{
	int month[6][7];
}cal;

cal obj[12];// CONTAINS ALL THE 12 MONTHS

int i;   	//
int j;   	// LOOP CONTROL VARIABLES
int k;   	//
int gfl; 	// GLOBAL FLAG VARIABLES

int month_value[] =	{
						31,28,31,30,
						31,30,31,31,
						30,31,30,31
					};

char month_name[][10] = {
							"January",	"February",	"March",	"April",
							"May",		"June",		"July",		"August",
							"September","October",	"November",	"December"
						};

void create_calendar(int, int);
void print_calendar(int ,int, int);
void leap_check(int);

int main()
{
	int year;
	int day_number;
	int option;
	int month_no;

	do
	{
		clrscr();
		printf("\n\t\t====================");
		printf("\n\t\t      CALENDAR");
		printf("\n\t\t====================");
		printf("\n\n\tDefine The Year .............. [1]");
		printf("\n\n\tView A Particular Month ...... [2]");
		printf("\n\n\tView The Whole Year .......... [3]");
		printf("\n\n\tExit ......................... [0]");
		printf("\n\n\tEnter Option : ");
		scanf("%d",&option);
		switch(option)
		{
			case 1 :
				clrscr();
				gfl = 1;
				printf("\n\n\tEnter The Year : ");
				scanf("%d", &year);
				printf("\n\n\tEnter The Day Number Of 1st January : ");
				scanf("%d", &day_number);
				create_calendar(year,day_number);
				break;
			case 2 :
				clrscr();
				if(gfl == 0)
				{
					printf("\n\n\tYear Not Defined!");
					printf("\n\n\tPress Enter To Continue ... ");
					fflush(stdin);
					getchar();
					break;
				}
				printf("\n\n\tEnter The Month Number That You Would Like To See : ");
				scanf("%d", &month_no);
				print_calendar(month_no - 1,month_no,year);
				break;
			case 3 :
				clrscr();
				if(gfl == 0)
				{
					printf("\n\n\tYear Not Defined!");
					printf("\n\n\tPress Enter To Continue ... ");
					fflush(stdin);
					getchar();
					break;
				}
				print_calendar(0,12,year);
				break;
			case 0 :
				printf("\n\n\tThank You ... ");
				break;
			default :
				printf("\n\n\tWrong Choice!");
				printf("\n\n\tPress Enter To Continue ... ");
				fflush(stdin);
				getchar();
				break;
		}
	}while(option != 0);

	return 0;
}

void leap_check(int year)
{
	if(year%400 == 0)
		month_value[1] = 29;
	else
	{
		if(year%100 != 0 && year%4 == 0)
			month_value[1] = 29;
	}
}

void create_calendar(int year,int day_number)
{
	int count;
	int fl;
	int limit;

	leap_check(year);
	limit = day_number-1;
	count = 1;
	fl = 0;

	for(i = 0;i < 12;i++)				// TRAVERSING THROUGH MONTHS
	{
		for(j = 0;j < 6;j++)			// TRAVERSING THROUGH WEEKS
		{
			if(j != 0)
				limit = 0;

			for(k = limit;k < 7;k++)	// TRAVERSING THROUGH DAYS
			{
				if(count <= month_value[i])
				{
					obj[i].month[j][k] = count;
					count++;
				}
				else					// END OF A MONTH
				{
					limit = k;
					count = 1;
					fl = 1;
				}
				if(fl == 1)
					break;
			}
			if(fl == 1)
				break;
		}
		fl = 0;
	}
}

void print_calendar(int lower,int upper,int year)
{
	clrscr();
	printf("\n\t\t. : YEAR %d : .\n\n", year);

	for(i = lower;i < upper;i++)
	{
		printf("\t%s\n\n",month_name[i]);
		printf("\tSUN\tMON\tTUE\tWED\tTHU\tFRI\tSAT\n");

		for(j = 0;j < 6;j++)
		{
			for(k = 0;k < 7;k++)
			{
				if(obj[i].month[j][k] != 0)
					printf("\t%d",obj[i].month[j][k]);
				else
					printf("\t");
			}
			printf("\n");
		}
		printf("\n");
	}

	printf("\n\n\tPress Enter To Continue ... ");
	fflush(stdin);
	getchar();
}
jephthah 1,888 Posting Maven

here's a bug: #include <conio.h> your program is worthless with that in it. why do you people insist on clinging onto that Turbo C crap?

xavier666 56 Junior Poster

here's a bug: #include <conio.h> your program is worthless with that in it. why do you people insist on clinging onto that Turbo C crap?

I'm actually new. Can you please explain the problems faced with conio.h I've been getting insults just for using this.
PS - Why did they (don't know who) put conio.h in C in the first place if it was such a problem?

why do you people insist on clinging onto that Turbo C crap?

What should i be clinging to?

Dave Sinkula 2,398 long time no c Team Colleague

PS - Why did they (don't know who) put conio.h in C in the first place if it was such a problem?

"They" didn't. It's for a third party library, one that was meant to work with DOS. Not a windows console, mind you, but DOS. Code that uses it is really meant for DOS. It doesn't really belong in Windows, and the "let's abandon this" period was begun about 1995. The "problem" is people still using this after, oh, about 1999.

Generally these folks are nudged away from using something like this that is quite out of date. Many insist on using it anyway. I don't know to what aim -- it's not going to become any more widely used, and almost certainly less so.

xavier666 56 Junior Poster

Thanks Dave :$

binkiwinki 0 Newbie Poster

Just use system("cls"); to clear the screen.

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.