problem again :(

This is a discussion on problem again :( within the C Programming forums, part of the General Programming Boards category; im having problems calculating the number of days until the 25/12/2002, here is my program (attached) so far, i know ...

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

    problem again :(

    im having problems calculating the number of days until the 25/12/2002,

    here is my program (attached) so far, i know its messy but have a look and see wot i can do, thanks,


    Brad.
    Attached Files Attached Files

  2. #2
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    What sort of problem? Do you have a specific problem with this, if not, I hate to say it, but there's plenty of example code here
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  3. #3
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >Do you have a specific problem with this
    Yes, it doesn't work. Sadly, google doesn't help a great deal with the art of debugging code.

    >im having problems calculating the number of days until the 25/12/2002
    You're making this very hard on yourself, remember to keep programs and solutions as simple as possible or you'll get stuck on annoying details that cause trouble. This works.
    Code:
    #include <stdio.h>
    
    struct dat{ 
       int day;   
       int month;
       int year;
    };
    
    int main ( void )
    {
      char loop ='y';
      while (loop == 'y'||loop == 'Y') {
        int leap, i, day = 0, total;
        struct dat t; 
         
        const int daytab[2] [13] = {
          {0,31,28,31,30,31,30,31,31,30,31,30,31},
          {0,31,29,31,30,31,30,31,31,30,31,30,31}
        };
        printf("***************************\n");
        printf("*A DATE CALCULATOR PROGRAM*\n");
        printf("***************************\n\n");
        printf("Please enter a date from which you wish to calculate\n");
        printf("how many days until the 25/12/2002.\n\n"); 
            
        scanf("%d/%d/%d", &t.day,&t.month,&t.year);
        leap = ( t.year % 4 == 0 && t.year % 100 != 0 || t.year % 400 == 0 );
        /* Rethink your error handling, it could be
        ** implemented better and more accurately.
        */
        for (i = 1; i < t.month; i++)
          day += daytab[leap][i];
        day += t.day;
        if ( leap == 0 )
          total = 359 - day;
        else
          total = 360 - day;
    
        printf("%d %s until 25/12/2002\n\n", total, (total == 1)?"day":"days");
        printf("Would you like to enter another date? (Y/N)\n");       
        scanf("\n%c",&loop);           
      }
      return 0;
    }
    However, it only works for the year 2002. You apparently haven't gotten to the part about handling multiple year spans, so I didn't do anything with it. Your input validation could also be improved by using a loop with simpler tests. I left that blank for you to fill in.

    -Prelude
    My best code is written with the delete key.

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, 04:21 PM
  2. Memory problem with Borland C 3.1
    By AZ1699 in forum C Programming
    Replies: 16
    Last Post: 11-16-2007, 10:22 AM
  3. Someone having same problem with Code Block?
    By ofayto in forum C++ Programming
    Replies: 1
    Last Post: 07-12-2007, 08:38 AM
  4. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 02:51 PM
  5. WS_POPUP, continuation of old problem
    By blurrymadness in forum Windows Programming
    Replies: 1
    Last Post: 04-20-2007, 06:54 PM

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