• 0

[C#]Need help in improvement in coding and self define function


Question

This is the coding to locate the day of the week one's birthday fall on,

can someone please check for me if there is anything that can be improved?

#include <stdio.h>

void title(void);
int cal_day(int year);
int finalo(int sun,int mon,int tue,int wed,int thu,int fri,int sat);
int main(void)
{
	int sentinel,i,year,month,date,day;
	int s;
	int sun=0,mon=0,tue=0,wed=0,thu=0,fri=0,sat=0;
	char b='\0';
	char dummyvar[100];

	title();
	printf("Please enter how many birthdays you want to check\n>>");
	scanf("%d",&sentinel);

	for(i=1;i<=sentinel;i++)
	{
		printf("\nPlease enter the year,month and date of the birthday which you wish to check\n\n");

		printf("Please enter the year<1900-2008, e.g.2008>\nYear>");
		while((s=scanf("%d",&year))==0 || year<1900 || year>2008 || (b=getchar())=='.')
		{
			printf("\nPlease enter correct year<1900-2008, e.g.2008>\nYear>");
			fgets(dummyvar,100,stdin);
		}

		printf("\nPlease enter the month<1-12>\nMonth>");
		while((s=scanf("%d",&month))==0 || month>12 || month<1 || (b=getchar())=='.')
		{
			printf("\nPlease enter the correct month<1-12>\nMonth>");
			fgets(dummyvar,100,stdin);
		}

		if(month==2)
		{
			if(year%4!=0)
			{
				printf("\nPlease enter the date<1-28>\nDate>");
				while((s=scanf("%d",&date))==0 || date>28 || date<1 || (b=getchar())=='.')
				{
					printf("\nPlease enter the correct date<1-28>\nDate>");
					fgets(dummyvar,100,stdin);
				}
			}
			else 
			{
				printf("\nPlease enter the date<1-29>\nDate>");
				while((s=scanf("%d",&date))==0 || date>29 || date<1 || (b=getchar())=='.')
				{
					printf("\nPlease enter the correct date<1-29>\nDate>");
					fgets(dummyvar,100,stdin);
				}
			}
		}

		else if(month==4 || month==6 || month==9 || month==11)
		{
			printf("\nPlease enter the date<1-30>\nDate>");
			while((s=scanf("%d",&date))==0 || date>30 || date<1 || (b=getchar())=='.')
			{
				printf("\nPlease enter the correct date<1-30>\nDate>");
				fgets(dummyvar,100,stdin);
			}
		}

		else 
		{
			printf("\nPlease enter the date<1-31>\nDate>");
			while((s=scanf("%d",&date))==0 || date>31 || date<1 || (b=getchar())=='.')
			{
				printf("\nPlease enter the correct date<1-31>\nDate>");
				fgets(dummyvar,100,stdin);
			}
		}

		day=cal_day(year);

		switch(month)
		{
		case 12:day=day+30;
		case 11:day=day+31;
		case 10:day=day+30;
		case 9:day=day+31;
		case 8:day=day+31;
		case 7:day=day+30;
		case 6:day=day+31;
		case 5:day=day+30;
		case 4:day=day+31;
		case 3:if(year%4==0)
			   {
				   day=day+29;
			   }
			   else day=day+28;
		case 2:day=day+31;
		case 1:day=(day+date)%7;
		}

		printf("\nYou are born in %d/%d/%d <YYYY/MM/DD>\n",year,month,date);

		switch (day)
		{
		case 0: printf("\nIt's a Sunday!!!\n\n");
			sun++;
			break;
		case 1: printf("\nIt's a Monday!!!\n\n");
			mon++;
			break;
		case 2: printf("\nIt's a Tuesday!!!\n\n");
			tue++;
			break;
		case 3: printf("\nIt's a Wednesday!!!\n\n");
			wed++;
			break;
		case 4: printf("\nIt's a Thursday!!!\n\n");
			thu++;
			break;
		case 5: printf("\nIt's a Friday!!!\n\n");
			fri++;
			break;
		case 6: printf("\nIt's a Saturday!!!\n\n");
			sat++;
		}
	}

	finalo(sun,mon,tue,wed,thu,fri,sat);

	return 0;
}

void title(void)
{
	printf("                      *********************************\n");
	printf("                      *     Birthday Finder v2008     *\n");
	printf("                      *********************************\n\n");
	printf("              This program will help you to locate which day of\n");
	printf("              the week your birthday fall on ^^V\n\n\n");
}

int cal_day(int year)
{
	int a;
	a =(((year-1)*365)+((year-1)/4)-((year-1)/100)+((year-1)/400))%7;
	return a;
}

int finalo(int sun,int mon,int tue,int wed,int thu,int fri,int sat)
{
	printf("---------------------------------------------------------\n");
	printf("There are %d persons' birthday fall on Sunday\n", sun);
	printf("There are %d persons' birthday fall on Monday\n", mon);
	printf("There are %d persons' birthday fall on Tuesday\n", tue);
	printf("There are %d persons' birthday fall on Wednesday\n", wed);
	printf("There are %d persons' birthday fall on Thursday\n", thu);
	printf("There are %d persons' birthday fall on Friday\n", fri);
	printf("There are %d persons' birthday fall on Saturday\n", sat);
	printf("---------------------------------------------------------\n\n\n\n");
	return 0;
}

by the way, i would like to make a self-defined function for the input part(year,month and date), but i don't know how, someone please teach me?

**my syllabus is only within if else, switchcase and looping

Link to comment
Share on other sites

0 answers to this question

Recommended Posts

There have been no answers to this question yet

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.