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;
} 6 2019
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;
}
Simon Mansfield wrote: 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;
}
--
Al Bowers
Tampa, Fl USA
mailto: xa******@myrapidsys.com (remove the x to send email) http://www.geocities.com/abowers822/
On Sun, 05 Dec 2004 16:23:15 GMT
"Simon Mansfield" <gl************@ntlworld.com> wrote: 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.
return 0; }
--
Flash Gordon
Living in interesting times.
Although my email address says spam, it is real and I read it.
"Simon Mansfield" <gl************@ntlworld.com> wrote in message
news:TB***************@newsfe4-gui.ntli.net... 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.
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;
}
On Sun, 05 Dec 2004 17:24:37 +0000, Simon Mansfield wrote: 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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Karam Chand |
last post by:
Greetings
I have a table and I want to make a copy of it in the
same database. I only keep the structure and indexes
and not the data.
I issue a command -
create table...
|
by: bart |
last post by:
Hi all
command "select * from table" whith 3000 records give me only 1000
rows
I can't figure out what is wrong. I think that something wrong is with
my my.cnf but can't find which varaible I...
|
by: Chris Geerdink |
last post by:
combo with PHP. what is wrong with the Javascript?
else
{
include("mysql.php");
$query1 = mysql_query("INSERT INTO gbook (naam, email, text) VALUES
('".$_POST."', '".$_POST."',...
|
by: erdem kemer |
last post by:
i cant send mail to yahoo mail or hotmail while i can send my other mail
accounts (pop3)
is it becouse yahoo and hotmail is web-based mail
here is the code
MailMessage mailMsg = new...
|
by: Mike Barnard |
last post by:
It's a simple test... VERY SIMPLE. But...
In an external stlyesheet some attributes don't show. With the same
styles cut and pasted to the test internally it works as expected.
Anyone tell...
| |
by: g35rider |
last post by:
Hi, I have the following code that is giving this error, I cant
simplify the code, I was just testing some theory for something we are
doing and was getting an issue here. Please someone point out...
|
by: islayer |
last post by:
can someone tell me what is wrong with the bold code? i am just learning perl. the program should create a perl file with a random name (5 letters, followed by a number), but the name is always just...
|
by: WolfgangS |
last post by:
Ok first off, i am a total beginner at this groups stuff and i have no
clue how this works. This is probabaly the wrong group for my problem
but i will post it anyways. Learning by doing right?
...
|
by: sanjay.vasudevan |
last post by:
Why are the following declarations invalid in C?
int f();
int f();
It would be great if anyone could also explain the design decision
for such a language restricton.
Regards,
Sanjay
|
by: Silentshot232 |
last post by:
Ok It seems to be effecting both my laptop and my desk top I cant sign onto aim I cant get on google page and It only lets me watch 1/4 of the youtube videos I want to. I have all the recent Java and...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
| |
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
|
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...
| |