Cant quite figure out whats wrong!? Help??

  • Thread starter Simon Mansfield
  • Start date
S

Simon Mansfield

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??

#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;
}
 
S

Simon Mansfield

Ok, now I have this: (It doesn't crash and it does give an answer... However
it is very different than that given by http://www.peterussell.com/Age.html
and the like)

#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);
i = i / 86400; //Converts the result into days as opposed to seconds.

printf("%f", i);

getchar();
getchar();

return 0;
}
 
F

Flash Gordon

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??

#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]: ");

You need to flush the output stream or this might not be displayed
before the program waits for its input.
//Read in the birthday.
scanf("%2d/%2d/%4d", &day, &mnth, &yr);

You have serious need of input validation here.
printf("\n\n");

store.tm_mday = day;
store.tm_mon = (mnth - 1);
store.tm_year = yr;

*birthday = store;

BANG! birthday is an uninitialised pointer, and this copies the contents
of store to the random location. I suggest you read the section of your
C text book that deals with pointers.
birth = mktime(birthday);

Here you could use
birth = mktime(&time);
i = difftime(now, birth);

printf("%f", 1);

Please cut and paste your code rather than retyping it. I'm sure in your
actual code you had an "i" rather than a 1. You also need to look up
what difftime returns, you still have not solved the problem.

Without a terminating newline there is no guarantee that this is
displayed.
getchar();
getchar();

Why two getchar calls? One is sufficient if you handle your input above
properly.
 
A

Al Bowers

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;
}
 
M

mef526

Simon Mansfield said:
store.tm_mday = day;
store.tm_mon = (mnth - 1);
store.tm_year = yr;

*birthday = store;
birth = mktime(birthday);

birthday = &store;

Other problems also, like the epoch issue for the year, etc.
 
S

Simon Mansfield

Thanks for all the help guys.. As yet I have been concentrating on just
getting the results that I need.. Later I will try to add some validation
checks and the like into the code. What I have so far seems to work fine,
however I really want the output to be an int.. Which I may code in later.

#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 - 1900;

birthday = &store;
birth = mktime(birthday);
i = difftime(now, birth);
i = i / 86400; //Converts the result into days as opposed to seconds.

printf("The number of days that have past since your birthday are: %f",
i);

getchar();
getchar();

return 0;
}
 
L

Lawrence Kirby

Ok, now I have this: (It doesn't crash and it does give an answer... However
it is very different than that given by http://www.peterussell.com/Age.html
and the like)

#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;

You don't need a separate pointer here, you could write

struct tm birthday;
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;

then

birthday.tm_mday = day;

etc.
store.tm_mon = (mnth - 1);
store.tm_year = yr;

The tm_year member is defined as number of years since 1900. So to
represent for example the year 2004 you would put the value 104 in tm_year.
birthday = &store;
birth = mktime(birthday);

and here

birth = mktime(&birthday);


Lawrence
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top