Simon said:
Im trying to make a C program that takes in a date (birthday) and tells the
user how many days it has been since that date. So far I have got this... It
compiles ok but then crashes, with no idea why I was wondering if anyone
else had any idea??
Yes.
You should check the return values of function time and function
mktime. These functions will return (time_1)-1 should the function is
unable to determine a time_t value. So, use something like:
if((now = time(NULL)) == (time_t)-1)
/* TODO: code to exit function or program */
if((birth = mktime(birthday)) == (time_t)-1);
/* TODO: code to exit function or program */
Anther problem is that struct tm member tm_year
is an int representing the number of years since
1900. The code prompts for the user birthday. Say the
user input "02/14/1976". You assign int yr the value 1976.
You then are assigning this value to the struct member tm_year with:
store.tm_year = yr;
when it should be:
store.tm_year = yr - 1900;
Because of the faulty assignment to tm_year, the function mktime
is returning (time_t)-1. Function mktime requires calendar time
agruments to compute a difference. (time_t)-1 is an indicator that
a calendar time cannot be represented.
The cause of the crash is your use of the struct tm * variable
birthday. You declare it pointing to nothing. Then later in the
code you do the assignment:
*birthday = store;
The problem crashes because the pointer in meaningless and
assignment to it is undefined behavior.
See a possible solution below.
I guess you are aware that function difftime returns a difference
in seconds, not days.
In general time_t values are not the best solution for birthdays.
It's range of values is restrictive. For example, I am old; my
birthday cannot be represented in a time_t value on my implementation.
You might consider using a scalar date solution like shown in the link:
http://c.snippets.org/browser.php#27
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main() {
int day, mnth, yr; //The users birthday.
time_t now, birth;
struct tm *birthday, store;
double i;
now = time(0);
//Prompt the user for birthdate.
printf("Please enter your birthday [DD/MM/YYYY]: ");
//Read in the birthday.
scanf("%2d/%2d/%4d", &day, &mnth, &yr);
printf("\n\n");
store.tm_mday = day;
store.tm_mon = (mnth - 1);
store.tm_year = yr;
*birthday = store;
birth = mktime(birthday);
i = difftime(now, birth);
printf("%f", 1);
getchar();
getchar();
return 0;
}
#include <stdio.h>
#include <time.h>
int main(void)
{
int day, mnth, yr; //The users birthday.
time_t now, birth;
struct tm birthday = {0};
double i;
if((now = time(NULL)) == (time_t)-1)
{
puts("Unable to represent current date");
getchar();
return 1;
}
//Prompt the user for birthdate.
printf("Please enter your birthday [DD/MM/YYYY]: ");
//Read in the birthday.
scanf("%2d/%2d/%4d", &day, &mnth, &yr);
printf("\n\n");
birthday.tm_mday = day;
birthday.tm_mon = (mnth - 1);
birthday.tm_year = yr - 1900;
if((birth = mktime(&birthday)) == (time_t)-1)
{
puts("Unable to represent the birthday");
getchar();
return 1;
}
i = difftime(now, birth);
printf("%f secs\n", i);
printf("In Days: %.1f days\n",i/(60.0*60.0*24));
getchar();
return 0;
}