Struct Challange

[code]#include <stdio.h>
/* Also, include the <time.h> header at the start of your program.*/
#include <time.h>

int main(int argc, const char * argv[])

/*Your challenge is to write a program that will tell you what the date (4-30-2015 format is fine) will be in 4 million seconds*/

/*Calculate and print the number of seconds that have passed from 1970 in Greenwich England to now.*/
long secondsSince1970 = time(NULL);
printf("The time now from 1970 in Greenwich England is %ld seconds.\n", secondsSince1970);

/*Calculate and print the number of seconds four million seconds from now.*/
long fourMillionSecondsFromNow = secondsSince1970 + 4E6;
printf("Four million seconds from now is %ld seconds.\n", fourMillionSecondsFromNow);

/Use the struct tm, which is the standard C library uses to hold time broken down into its components. Reference: page 406 0f 701./
struct tm laterDate;
localtime_r(&fourMillionSecondsFromNow, &laterDate);

/*Hints: tm_mon = 0 means January, so be sure to add 1 and tm_year = 1900, so be sure to add 1900 to your year.*/
printf("The date is %d-%d-%d\n", laterDate.tm_mon+1, laterDate.tm_mday, laterDate.tm_year+1900);

return 0;


Thanks for this. I actually missed the part where it said I needed time.h in the book. Now my program works. LOL!

I don’t understand what the challenge is asking of me. Structs are not confusing in theory but when on “syntax paper” it doesn’t make sense.

It was very tricky of whoever wrote this chapter to put the header information at the end of the challenge. :wink:

After I read this thread, it worked — here’s what I got:

[code]#include <stdio.h>
#include <time.h>

int main(int argc, const char * argv[])
//Today’s Seconds

long secondsSince1970 = time(NULL);
printf("It has been %ld seconds since 1970.\n", secondsSince1970);

//Today's Date

struct tm now;
localtime_r(&secondsSince1970, &now);
printf("Today's date is %d-%d-%d.\n", now.tm_mon + 1, now.tm_mday, now.tm_year + 1900);

//Future Seconds

long secondsInFourMillion = secondsSince1970 + 4000000;
printf("In four million seconds, it will have been %ld seconds since 1970.\n", secondsInFourMillion);

//Future Date

struct tm future;
localtime_r(&secondsInFourMillion, &future);
printf("The date four million seconds from now will be: %d-%d-%d.\n", future.tm_mon+1, future.tm_mday, future.tm_year + 1900);

return 0;