Thread: why doesnt this work?

  1. #1
    Registered User
    Join Date
    Apr 2002
    Posts
    32

    why doesnt this work?

    can anyone tell me y this doesnt work please, and how to make it work?


    /* ---------------------------------------------------------------------- */
    /* Program to calculate # of days between two dates */
    /* This program was tested & compiled on a DeSmet 'C' compiler */
    /* ver 2.2 -- but should run on any good 'C' compiler */
    /* ---------------------------------------------------------------------- */

    /* Please note that this program only works from 01-12-1600 A.D. onward */



    struct date /* structure to hold date */
    {
    int month;
    int day;
    int year;
    } date_1;


    long int funct1 (y,m) /* part of # of days calc. */
    int y, m;
    {
    long int result;
    if ( m <= 2 )
    y -= 1;
    result = y;
    return (result);
    }

    long int funct2 (m)
    int m;
    {
    long int result;
    if ( m <= 2 )
    result = m + 13;
    else
    result = m + 1;
    return(result);
    }

    /* Function to calculate the number of days in dates */

    long int day_count (m, d, y)
    int m, d, y;
    {
    long int number;
    number = 1461 * funct1(y,m) / 4 + 153 * funct2(m) / 5 + d;

    return (number);
    }

    main ()
    {
    long int number_of_days1;
    int day_of_week, screw_up = 0;

    printf("\n\n************************************** ***************************\n");
    printf("THIS PROGRAM WILL COMPUTE THE DAY OF THE WEEK (SUNDAY - SATURDAY)\n");
    printf("\t\tTHAT A GIVEN DATE WILL FALL ON\n");
    printf("****************************************** ***********************\n\n");

    printf ("Enter a date (mm dd yyyy) i.e. 03 12 1985 \n");
    scanf ("%d %d %d", &date_1.month, &date_1.day, &date_1.year);

    number_of_days1 = day_count (date_1.month, date_1.day, date_1.year);

    printf ("\nThe date is: " );

    day_of_week = (number_of_days1 - 621049) % 7;

    switch (day_of_week)
    {
    case 0 :
    printf ("Sunday,");
    break;
    case 1 :
    printf ("Monday,");
    break;
    case 2 :
    printf ("Tuesday,");
    break;
    case 3 :
    printf ("Wednesay,");
    break;
    case 4 :
    printf ("Thursday,");
    break;
    case 5 :
    printf ("Friday,");
    break;
    case 6 :
    printf ("Saturday,");
    break;
    default:
    printf ("Something is screwed up -- Maybee you entered\n");
    printf ("a date earlier than 01 12 1600\n\n");
    screw_up = 1;
    }
    if ( !screw_up )
    printf (" %02d/%02d/%02d\n", date_1.month, date_1.day, date_1.year);
    }

    brad.

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    long int funct1 (y,m) /* part of # of days calc. */
    int y, m;
    {
    Wow! Some one needs a new book. This is OLD.

    long int funct1( int y, int m )
    {

    You don't have to do the old style declarations. The above is fine. Other than that, use code tags.

    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Apr 2002
    Posts
    32
    if the code is fine then maybe something else is wrong, it compiles the file with no problems but when trying to build it and run, it says the failure is a result of a failed compilation?? i dont understand. have any of u been able to run the file on your machines?

    Brad.

  4. #4
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    This works for me, perhaps your compiler doesn't like K&R C.
    Code:
    #include <stdio.h>
    
    struct date /* structure to hold date */ 
    { 
      int month; 
      int day; 
      int year; 
    } date_1; 
    
    long int funct1 (int y,int m) /* part of # of days calc. */ 
    { 
      long int result; 
      if ( m <= 2 ) 
        y -= 1; 
      result = y; 
      return (result); 
    } 
    
    long int funct2 (int m) 
    { 
      long int result; 
      if ( m <= 2 ) 
        result = m + 13; 
      else 
        result = m + 1; 
      return(result); 
    } 
    
    /* Function to calculate the number of days in dates */ 
    
    long int day_count (int m, int d, int y)  
    { 
      long int number; 
      number = 1461 * funct1(y,m) / 4 + 153 * funct2(m) / 5 + d; 
      return (number); 
    } 
    
    int main ( void ) 
    { 
      long int number_of_days1; 
      int day_of_week, screw_up = 0; 
    
      printf(" \n\n******************************************\n"); 
      printf("THIS PROGRAM WILL COMPUTE THE DAY OF THE WEEK (SUNDAY - SATURDAY)\n"); 
      printf("\t\tTHAT A GIVEN DATE WILL FALL ON\n"); 
      printf(" ********************************************\n\n"); 
    
      printf ("Enter a date (mm dd yyyy) i.e. 03 12 1985 \n"); 
      scanf ("%d %d %d", &date_1.month, &date_1.day, &date_1.year); 
    
      number_of_days1 = day_count (date_1.month, date_1.day, date_1.year); 
    
      printf ("\nThe date is: " ); 
    
      day_of_week = (number_of_days1 - 621049) % 7; 
    
      switch (day_of_week) 
      { 
      case 0 : 
        printf ("Sunday,"); 
        break; 
      case 1 : 
        printf ("Monday,"); 
        break; 
      case 2 : 
        printf ("Tuesday,"); 
        break; 
      case 3 : 
        printf ("Wednesay,"); 
        break; 
      case 4 : 
        printf ("Thursday,"); 
        break; 
      case 5 : 
        printf ("Friday,"); 
        break; 
      case 6 : 
        printf ("Saturday,"); 
        break; 
      default: 
        printf ("Something is screwed up -- Maybee you entered\n"); 
        printf ("a date earlier than 01 12 1600\n\n"); 
        screw_up = 1; 
      } 
      if ( !screw_up ) 
        printf (" %02d/%02d/%02d\n", date_1.month, date_1.day, date_1.year);
      return 0;
    }
    -Prelude
    My best code is written with the delete key.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. strcmp returning 1...
    By Axel in forum C Programming
    Replies: 12
    Last Post: 09-08-2006, 07:48 PM
  2. getline() don't want to work anymore...
    By mikahell in forum C++ Programming
    Replies: 7
    Last Post: 07-31-2006, 10:50 AM
  3. Why don't the tutorials on this site work on my computer?
    By jsrig88 in forum C++ Programming
    Replies: 3
    Last Post: 05-15-2006, 10:39 PM
  4. fopen();
    By GanglyLamb in forum C Programming
    Replies: 8
    Last Post: 11-03-2002, 12:39 PM
  5. DLL __cdecl doesnt seem to work?
    By Xei in forum C++ Programming
    Replies: 6
    Last Post: 08-21-2002, 04:36 PM