Calendar Problem

This is a discussion on Calendar Problem within the C Programming forums, part of the General Programming Boards category; I have to write a function which takes in a year between 1900 and 1999 and returns as a number ...

  1. #1
    Registered User
    Join Date
    Oct 2002
    Posts
    10

    Question Calendar Problem

    I have to write a function which takes in a year between 1900 and 1999 and returns as a number the first day of that year. Days of the week go from 0 - 6 ( Sunday = 0, Saturday = 6).

    I am supposed to use a leap year test to make this function work. I am also meant to make use of the fact that 1/1/1900 was a Monday. At the moment the program returns the correct value for the year 1900, but for every other year i enter i get "9" which is an invalid value. Any help??

    Here is my code so far:

    Code:
    # include <stdio.h>
    
    int new_years_day(int day, int year);
    int no_of_days(int months, int year);
    int first_day(int day, int year, int months);
    
    int main(void)
    {
    	int choice, day = 0, months = 0, year = 0;
    	printf("Choose a function\n (1) new_years_day (2) no_of_days (3) first_day >> ");
    	scanf("%d", &choice);
    	if (choice == 1)
    		new_years_day(day, year);
    		else if (choice == 2)
    			no_of_days(months, year);
    			else if (choice == 3)
    				first_day(day, year, months);
    				else 
    				{
    					printf("\nPlease enter a number between 1 and 3 >> ");
    					scanf("%d", &choice);
    				}
    					return 0;
    }
    
    
    int new_years_day(int day, int year)
    {
    	int i = 0, leap = 0;
    
    	// ask user for year between 1900 and 1999
    	printf("Enter a year between 1900 and 1999 >> ");
    	scanf("%d", &year);
    
    	// check that user enters a valid year
    	while (year < 1900 || year > 1999)
    	{
    		// print error message
    		printf("You can only choose a year between 1900 and 1999!! >> ");
    		scanf("%d", &year);
    	}
    	
    	for ( i = day; day <= 6; day++)
    	
    	/* Problem: need to count no. of years after 1900 and loop between 0 and 6
    	   the right number of times to get the right day */
    	
    	
    	// leap year check
    
    	while ((year % 400 == 0) && (year % 4 == 0) && (year % 100 > 0))	
    			leap = 1;		// is a leap year
    		
    	// find correct day
    
    		if (year == 1900)
    		{
    			day = 1;
    			printf("\nThe first day of the year in %d was a %d (0 - Sunday, 6 - Saturday)\n", year, day);
    		}
    		else if (leap = 1)
    		{
    			day = day + 2;
    			printf("\nThe first day of the year in %d was a %d (0 - Sunday, 6 - Saturday)\n", year, day);
    		}
    		else if (leap = 0)
    		{
    			day++;
    			printf("\nThe first day of the year in %d was a %d (0 - Sunday, 6 - Saturday)\n", year, day);
    		}
    	
    	return year, day;
    }
    
    
    int no_of_days(int months, int year)
    {
    	int i, current, leap = 0;
    
    	// ask user for year between 1900 and 1999
    	printf("Enter a year between 1900 and 1999 >> ");
    	scanf("%d", &year);
    	
    	// check that user enters a valid year
    	while (year < 1900 || year > 1999)
    	{
    		// print error message
    		printf("You can only choose a year between 1900 and 1999!! >> ");
    		scanf("%d", &year);
    	}
    
    	// ask user for a month between 1 and 12
    	printf("Enter a month between 1 and 12 >> ");
    	scanf("%d", &months);
    
    	// check that user enters a valid month
    	while (months < 1 || months > 12)
    	{
    		// print error message
    		printf("You can only choose a month between 1 and 12!! >> ");
    		scanf("%d", &months);
    	}
    
    	return months, year;
    }
    
    
    int first_day(int day, int year, int months)
    {
    	int i, current, leap = 0;
    
    	// ask user for year between 1900 and 1999
    	printf("Enter a year between 1900 and 1999 >> ");
    	scanf("%d", &year);
    	
    	// check that user enters a valid year
    	while (year < 1900 || year > 1999)
    	{
    		// print error message
    		printf("You can only choose a year between 1900 and 1999!! >> ");
    		scanf("%d", &year);
    	}
    
    	// ask user for a month between 1 and 12
    	printf("Enter a month between 1 and 12 >> ");
    	scanf("%d", &months);
    
    	// check that user enters a valid month
    	while (months <1 || months >12)
    	{
    		// print error message
    		printf("You can only choose a month between 1 and 12!! >> ");
    		scanf("%d", &months);
    	}
    	
    	return day, year, months;
    }

  2. #2
    Registered User The Dog's Avatar
    Join Date
    May 2002
    Location
    Cape Town
    Posts
    788
    Firstly, you cannot return multiple values from functions. You only have one function that does something, namely new_years_day. The other two functions just accept user-input but they don't do anything.

  3. #3
    Registered User
    Join Date
    Oct 2002
    Posts
    10
    Sorry bout the confusion but the other 2 functions are meant to be there cos i still have to build them yet. It's just that i'm stuck on the 1st function (new_years_day). Should I just have (return 0) at the bottom then instead of (return year, day), for example ??

  4. #4
    Registered User The Dog's Avatar
    Join Date
    May 2002
    Location
    Cape Town
    Posts
    788
    Declare the function like this :
    void new_years_day(int day, int year);

    then you don't have to return anything.

  5. #5
    Registered User
    Join Date
    Oct 2002
    Posts
    10
    Ok, thanks. So anybody got any ideas as to how i can make this function work then?

    Code:
    void new_years_day(int day, int year)
    {
    	int i = 0, leap = 0;
    
    	// ask user for year between 1900 and 1999
    	printf("Enter a year between 1900 and 1999 >> ");
    	scanf("%d", &year);
    
    	// check that user enters a valid year
    	while (year < 1900 || year > 1999)
    	{
    		// print error message
    		printf("You can only choose a year between 1900 and 1999!! >> ");
    		scanf("%d", &year);
    	}
    	
    	for ( i = day; day <= 6; day++)
    	
    	/* Problem: need to count no. of years after 1900 and loop between 0 and 6
    	   the right number of times to get the right day */
    	
    	
    	// leap year check
    
    	while ((year % 400 == 0) && (year % 4 == 0) && (year % 100 > 0))	
    			leap = 1;		// is a leap year
    		
    	// find correct day
    
    		if (year == 1900)
    		{
    			day = 1;
    			printf("\nThe first day of the year in %d was a %d (0 - Sunday, 6 - Saturday)\n", year, day);
    		}
    		else if (leap = 1)
    		{
    			day = day + 2;
    			printf("\nThe first day of the year in %d was a %d (0 - Sunday, 6 - Saturday)\n", year, day);
    		}
    		else if (leap = 0)
    		{
    			day++;
    			printf("\nThe first day of the year in %d was a %d (0 - Sunday, 6 - Saturday)\n", year, day);
    		}
    	
    }

  6. #6
    Registered User
    Join Date
    Oct 2002
    Posts
    10
    Please ??

  7. #7
    Registered User
    Join Date
    Oct 2002
    Posts
    10
    Yeah, I've tried that but it makes very little difference. What I really need is a little help with the logic of how to actually code the solution. I know what I want to do, I just can't figure it out in terms of code or logical steps.

  8. #8
    Registered User
    Join Date
    Oct 2002
    Posts
    10

    Thumbs up

    Cheers appreciate the help

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help understanding a problem
    By dnguyen1022 in forum C++ Programming
    Replies: 2
    Last Post: 04-29-2009, 05:21 PM
  2. Memory problem with Borland C 3.1
    By AZ1699 in forum C Programming
    Replies: 16
    Last Post: 11-16-2007, 11:22 AM
  3. Someone having same problem with Code Block?
    By ofayto in forum C++ Programming
    Replies: 1
    Last Post: 07-12-2007, 09:38 AM
  4. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 03:51 PM
  5. WS_POPUP, continuation of old problem
    By blurrymadness in forum Windows Programming
    Replies: 1
    Last Post: 04-20-2007, 07:54 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21