Thread: c program for date to day of week

  1. #1
    Registered User
    Join Date
    Sep 2016
    Posts
    3

    c program for date to day of week

    I want to write a program that will make a date into the day of the week... I am having a problem figuring out how to get the leap year into this...and my program returns a invalid date for any day in the months of August (08) and Sept (09)
    heres my code...I am very new to this, just a fyi


    Code:
    #include <stdio.h>
    
    
    int main (void)
     {
      long int n;
      float n_day;
      int f, g;
     int month;
      int day;
      int year;
       char c;
       int months[12] = {31,28,31,30,31,30,31,31,30,31,30,31};
       char *days[7] = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};
      int i; 
      
      printf ("Welcome to the Date to Day-of-Week program.\n");
     printf ("\nThe program will give the day of the week for any date from 1/1/1900\n");
     
     printf ("\nEnter date (mm/dd/yyyy):");
     scanf ("%i/%i/%i", &month, &day, &year);
     
     if ( month > 12)
     {
      printf ("Invalid month, please re-enter date");
      
      printf ("\nEnter date (mm/dd/yyyy):");
      scanf ("%i/%i/%i", &month, &day, &year);
     }
     if ( day > 31)
     {
      printf ("Invalid day, please re-enter date");
      
      printf ("\nEnter date (mm/dd/yyyy):");
      scanf ("%i/%i/%i", &month, &day, &year);
     }
     if ( year <= 1900)
     {
      printf ("Invalid year, please re-enter date. Year must be greater than 1900");
      
      printf ("\nEnter date (mm/dd/yyyy):");
      scanf ("%i/%i/%i", &month, &day, &year);
     }
     while ( (c = getchar() != '\n') && c != EOF);   /* clear input buffer  */
       
       
      
     if ( month <= 2)
      f = year - 1;
     else 
      f = year;
     
     if ( month <= 2)
      g = month +13;
     else 
      g = month +1;
      
     
     
     n = 1461 * f / 4 + 153 * g / 5 + day;
     n = n - 621049 % 7;
     
     
     
     printf ("%i/%i/%i falls on a %s\n", month, day, year, days[i]);
     }

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > n = 1461 * f / 4 + 153 * g / 5 + day;
    If you're that new, where did you get this formula from?

    Is it meant to be this one?
    Zeller's congruence - Wikipedia, the free encyclopedia
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Sep 2016
    Posts
    3
    I bought a book at a used book store, and the formula was in there....

  4. #4
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    > I am having a problem figuring out how to get the leap year into this...

    Someone once said that if you think date-time programming is easy, you haven't done much of it. The good news is that the C library already does the heavy lifting for you[1], and it's reasonably straightforward to use:
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    
    int main(void)
    {
    	const char *weekdays[] = {
    		"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday"
    	};
    
    	printf("Date (mm/dd/yyyy): ");
    	fflush(stdout);
    
    	int m, y, d;
    
    	if (scanf("%d/%d/%d", &m, &d, &y) != 3)
    	{
    		fprintf(stderr, "Invalid input, brah\n");
    		return EXIT_FAILURE;
    	}
    
    	/* Initialize to a sane default */
    	time_t datime = time(NULL);
    	struct tm *dadate = localtime(&datime);
    
    	/* Apply adjustments based on input */
    	dadate->tm_mon = m - 1;
    	dadate->tm_mday = d;
    	dadate->tm_year = y - 1900;
    
    	/* Normalize the adjustments */
    	datime = mktime(dadate);
    	dadate = localtime(&datime);
    
    	/* Rock on! */
    	printf("%s, baby!\n", weekdays[dadate->tm_wday]);
    
    	return EXIT_SUCCESS;
    }
    [1] Having written said library, I don't recommend it, unless you're especially interested in those things.
    My best code is written with the delete key.

  5. #5
    Registered User
    Join Date
    Sep 2016
    Posts
    3
    Why is the scanf %d and not %i??

    I noticed that when I ran my program, I can use 02/30/16...I read that I can use an array that has the days of the month in it....



    but how do I connect that to the usr input to make sure that the month that is entered has the correct amount of days in it?


    Quote Originally Posted by Prelude View Post
    > I am having a problem figuring out how to get the leap year into this...

    Someone once said that if you think date-time programming is easy, you haven't done much of it. The good news is that the C library already does the heavy lifting for you[1], and it's reasonably straightforward to use:
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    
    int main(void)
    {
        const char *weekdays[] = {
            "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday"
        };
    
        printf("Date (mm/dd/yyyy): ");
        fflush(stdout);
    
        int m, y, d;
    
        if (scanf("%d/%d/%d", &m, &d, &y) != 3)
        {
            fprintf(stderr, "Invalid input, brah\n");
            return EXIT_FAILURE;
        }
    
        /* Initialize to a sane default */
        time_t datime = time(NULL);
        struct tm *dadate = localtime(&datime);
    
        /* Apply adjustments based on input */
        dadate->tm_mon = m - 1;
        dadate->tm_mday = d;
        dadate->tm_year = y - 1900;
    
        /* Normalize the adjustments */
        datime = mktime(dadate);
        dadate = localtime(&datime);
    
        /* Rock on! */
        printf("%s, baby!\n", weekdays[dadate->tm_wday]);
    
        return EXIT_SUCCESS;
    }
    [1] Having written said library, I don't recommend it, unless you're especially interested in those things.

  6. #6
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    > Why is the scanf %d and not %i??

    Ooh, good question! I used %d because it's more specific to the needs. %d accepts a decimal value only, which when representing dates is universal. %i allows for other representations such as hexadecimal or octal. On top of doing more than needed, it introduces subtle bugs from the formatting. Two digit date components either start with 0 or not, and a leading zero in C says that the value is represented as octal. So "08" would read the '0', but not the '8' because '8' is an invalid octal character.

    In general I recommend not using %i at all for the above reasons. %d, %x, %o, and %u are typically sufficient.

    > but how do I connect that to the usr input to make sure that the month that is entered has the correct amount of days in it?

    The nice thing about mktime is that is normalizes even invalid values. If you enter "02/30/2016", the date will be normalized to March 1st. Whether that's something acceptable for your program is your choice. If it's not, you'll need to do some validation. But even then I'd check the tm struct to determine if the month changed rather than try to work in leap year calculations. Why reproduce effort from a library that's likely done better and faster in the library?
    My best code is written with the delete key.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C Program that output the very next date after Input a date
    By rumel.ehsan in forum C Programming
    Replies: 7
    Last Post: 03-23-2013, 11:53 AM
  2. day of week when entering in date
    By BuckyBarnes in forum C Programming
    Replies: 6
    Last Post: 10-05-2012, 08:02 PM
  3. Replies: 2
    Last Post: 05-07-2012, 07:37 AM
  4. Replies: 11
    Last Post: 03-27-2012, 11:37 PM
  5. Week Selection from Date Time Picker
    By gaurav_13191 in forum Windows Programming
    Replies: 2
    Last Post: 10-31-2011, 12:57 PM

Tags for this Thread