Typedef

This is a discussion on Typedef within the C Programming forums, part of the General Programming Boards category; I am writing a program that gives you tomorrow's date. However How am I able to get next weeks date? ...

  1. #1
    Sarah_SE
    Join Date
    Nov 2005
    Location
    Canada
    Posts
    6

    Typedef

    I am writing a program that gives you tomorrow's date.
    However How am I able to get next weeks date? Please help!
    I know I will have to have a loop.....

    Thanks in advance!

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    typedef struct date
    
    { 
    int day; 
    int month; 
    int year;
    }date;
    
    date tomorrow (date today);
    
    void nextweek (date today, date *nweek);
    
    printf("Enter today's date in the format: (month/day/year) ie: 12 31 2005\n"); 
    scanf("%d %d %d", &today.month, &today.day, &today.year); 
    
    tomorrow.month = today.month;
    tomorrow.day = today.day + 1; 
    tomorrow.year = today.year;
    
    if (today.month == 2) 
    { 
    
    if (tomorrow.day > 28) 
    	{ 
    tomorrow.day = 1; 
    tomorrow.month ++;
    	}
    } 
    else if (today.month==4||today.month == 6|| today.month==9 ||today.month == 11) 
    { 
    if (tomorrow.day > 30) 
    	{ 
    tomorrow.day = 1; 
    tomorrow.month ++;
    	}
    } 
    else 
    { 
    if (tomorrow.day > 31) 
    	{ 
    tomorrow.day = 1; 
    tomorrow.month ++;
    	}
    } 
    if (tomorrow.month > 12) 
    	{ 
    
    tomorrow.month = 1; 
    tomorrow.year ++;
    	}
    
    printf("Tomorrow's date will be: \n\n%d/%d/%d\n", tomorrow.month, tomorrow.day, tomorrow.year);
    
     
    }
    Last edited by Sarah_SE; 11-24-2005 at 12:43 PM.

  2. #2
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,048
    You should indent your code. It makes it easier to read.

    Um, maybe you meant to include most of that code in main()?
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  3. #3
    ZuK
    ZuK is offline
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    If you call tomorrow 7 times you will get next weeks date.
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    typedef struct date { 
        int day; 
        int month; 
        int year;
    } date;
    
    date date_tomorrow (date today);
    date date_nextweek (date today );
    
    int main() {
        date today, tomorrow, next_week ;
        int i;
        printf("Enter today's date in the format: (month/day/year) ie: 12 31 2005\n"); 
        scanf("%d %d %d", &today.month, &today.day, &today.year); 
        tomorrow = date_tomorrow( today );
        printf("Tomorrow's date will be: \n\n%d/%d/%d\n", tomorrow.month, tomorrow.day, tomorrow.year);
        next_week = date_nextweek( today );
        printf("Next weeks's date will be: \n\n%d/%d/%d\n", next_week.month, next_week.day, next_week.year);
    }
    
    int is_leap_year( int year ) {
        if ((year % 400) == 0)
            return 1;
        else if ((year % 100) == 0)
            return 0;
        else if ((year % 4) == 0)
            return 1;
        return 0;
    }
    
    date date_nextweek (date today ) {
        int i;
        for ( i = 0; i < 7 ; i++ )
        	today = date_tomorrow(today);
        return today;
    }
    
    date date_tomorrow ( date today) {
       date tomorrow = today;
       tomorrow.month = today.month;
       tomorrow.day++; 
       tomorrow.year = today.year;
    
       int leap_year = is_leap_year( tomorrow.year );
       
       if (today.month == 2) { 
          if ( ( tomorrow.day > 28 && ! leap_year ) || ( tomorrow.day > 29 && leap_year ) ) { 
              tomorrow.day = 1; 
              tomorrow.month ++;
          }
       } else if (today.month==4||today.month == 6|| today.month==9 ||today.month == 11) { 
           if (tomorrow.day > 30) { 
               tomorrow.day = 1; 
               tomorrow.month ++;
           }
       } 
       else { 
           if (tomorrow.day > 31) { 
               tomorrow.day = 1; 
               tomorrow.month ++;
           }
       } 
       if (tomorrow.month > 12) { 
            tomorrow.month = 1; 
            tomorrow.year ++;
       }
       return tomorrow;
    }
    Kurt

  4. #4
    Sarah_SE
    Join Date
    Nov 2005
    Location
    Canada
    Posts
    6
    I'm getting errors when I try to compile it

  5. #5
    Registered User
    Join Date
    May 2005
    Posts
    6
    Hi all,
    I know that is not relevant here but, "Zeller's formula" will give you day for any given date.

    Regards,
    Verge2k1

  6. #6
    ZuK
    ZuK is offline
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    It compiles fine with gcc. What are the errormessages ?
    K

  7. #7
    Sarah_SE
    Join Date
    Nov 2005
    Location
    Canada
    Posts
    6
    error message's are
    c:58: leap_year undeclared (first use in this function)
    c:54: parse error before 'int'

  8. #8
    Sarah_SE
    Join Date
    Nov 2005
    Location
    Canada
    Posts
    6
    Thanks again for your help!

  9. #9
    Sarah_SE
    Join Date
    Nov 2005
    Location
    Canada
    Posts
    6
    I fixed it however I'm getting one last error....
    c:54 parse error before 'int'

  10. #10
    ZuK
    ZuK is offline
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    This way it compiles even with "-Wall -pedantic" without any warnings.
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    typedef struct date { 
        int day; 
        int month; 
        int year;
    } date;
    
    date date_tomorrow (date today);
    date date_nextweek (date today );
    
    int main() {
        date today, tomorrow, next_week ;
        printf("Enter today's date in the format: (month/day/year) ie: 12 31 2005\n"); 
        scanf("%d %d %d", &today.month, &today.day, &today.year); 
        tomorrow = date_tomorrow( today );
        printf("Tomorrow's date will be: \n\n%d/%d/%d\n", tomorrow.month, tomorrow.day, tomorrow.year);
        next_week = date_nextweek( today );
        printf("Next weeks's date will be: \n\n%d/%d/%d\n", next_week.month, next_week.day, next_week.year);
        return 0;
    }
    
    int is_leap_year( int year ) {
        if ((year % 400) == 0)
            return 1;
        else if ((year % 100) == 0)
            return 0;
        else if ((year % 4) == 0)
            return 1;
        return 0;
    }
    
    date date_nextweek (date today ) {
        int i;
        for ( i = 0; i < 7 ; i++ )
        	today = date_tomorrow(today);
        return today;
    }
    
    date date_tomorrow ( date today) {
       date tomorrow = today;
       int leap_year = is_leap_year( tomorrow.year );
       tomorrow.month = today.month;
       tomorrow.day++; 
       tomorrow.year = today.year;
    
       
       if (today.month == 2) { 
          if ( ( tomorrow.day > 28 && ! leap_year ) || ( tomorrow.day > 29 && leap_year ) ) { 
              tomorrow.day = 1; 
              tomorrow.month ++;
          }
       } else if (today.month==4||today.month == 6|| today.month==9 ||today.month == 11) { 
           if (tomorrow.day > 30) { 
               tomorrow.day = 1; 
               tomorrow.month ++;
           }
       } 
       else { 
           if (tomorrow.day > 31) { 
               tomorrow.day = 1; 
               tomorrow.month ++;
           }
       } 
       if (tomorrow.month > 12) { 
            tomorrow.month = 1; 
            tomorrow.year ++;
       }
       return tomorrow;
    }
    Kurt

  11. #11
    Sarah_SE
    Join Date
    Nov 2005
    Location
    Canada
    Posts
    6
    Thank you so much!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 09:18 PM
  2. Need help understanding info in a header file
    By hicpics in forum C Programming
    Replies: 8
    Last Post: 12-02-2005, 12:36 PM
  3. Please STICKY this- vital to MSVC 6 dev - BASETSD.h
    By VirtualAce in forum Game Programming
    Replies: 11
    Last Post: 03-15-2005, 09:22 AM
  4. build errors migrated from dx9b to dx9c sdk
    By reanimated in forum Game Programming
    Replies: 4
    Last Post: 12-17-2004, 07:35 AM
  5. oh me oh my hash maps up the wazoo
    By DarkDays in forum C++ Programming
    Replies: 5
    Last Post: 11-30-2001, 12:54 PM

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