Hi,
Input: Names and born dates of persons
Output: Oldest person and his/here born date
How to do this? I'm new at this so be gentle
thx
Hi,
Input: Names and born dates of persons
Output: Oldest person and his/here born date
How to do this? I'm new at this so be gentle
thx
a) If A is born in 1940 and B in 1941 then A is older.
else if years are the same
b) If A is born in September and B in October then A is older.
else if months are the same
c) If A is born on the 13th and B on the 14th then A is older.
else they are both the same age
Hope this gives you an idea.
But you really should try to formulate some ideas, e.g how you are going to represent data, how you are going to search the persons etc, post some code and ask more specific questions.
I might be wrong.
Quoted more than 1000 times (I hope).Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
thx for fast replay
I need to use array if I have more than two names.
I know how to make this in delphi, so I need someone
who can explain me how arrays, dates, etc works in C.
If you only want to display data for one person you don't need arrays. Just store the data for the currently oldest person and discard younger ones.
For dates may-be you can use something from the <time.h> header, but the data for a person might be as simple as a struct containing a string (name) and three integers (year, month, day).
I might be wrong.
Quoted more than 1000 times (I hope).Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
time_t holds dates from 0 to max. 0 I think starts at January 1, 1970. So if you convert the dates into time_t then comparing dates is a simple unsigned long (time_t) comparison.
I'm trying but something isn't right...
what do I need here?Code:#include <stdio.h> #include <time.h> int main() { double dif; time_t t1, t2; dif=difftime(t1,t2)); clrscr(); printf("First date:"); scanf("%d",t1); printf("Second date:"); scanf("%d",t2); printf("\nThe difference between dates is: ", diff); }
Well, first of all, your call to difftime is done BEFORE you read the time variables from the user, so that won't work.
Also, it won't compile, because you have one variable "dif" that is declared, then printing a "diff" which is undeclared, so I guess you have to fix that.
Just to clarify, if you use "Microsoft Excel", you can declare a calculation, and then change the variables that the calculation relates to afterwards, and get the new value out of it. C, C++ and most other programming languages, the calculation is done with the values the variable have at the time, and no matter what other changes you do later, the value stays the same. So you need to make sure the variables are "filled in" before you use them for a calculation.
--
Mats
Compilers can produce warnings - make the compiler programmers happy: Use them!
Please don't PM me for help - and no, I don't do help over instant messengers.
Nor can you use %d to scan directly into a time_t.
First you need to use fgets() to read a line.
Then use strptime to parse the line and fill in a tm struct with the parsed information. If you don't have strptime, then you need to roll your own to do the same thing.
Then you use mktime() to turn your tm struct into a time_t.
Finally, having done that twice, you have two time_t variables which you can difftime()
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
If at first you don't succeed, try writing your phone number on the exam paper.
My homepage
Advice: Take only as directed - If symptoms persist, please see your debugger
Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"
Compilers can produce warnings - make the compiler programmers happy: Use them!
Please don't PM me for help - and no, I don't do help over instant messengers.
Ok. Maybe I posted wrong code... I have tried diff and dif (typing mistake).
Also, I declare diff=difftime(... after input, also didn't work. I will try SALEM's
post. I think it would we correct... thx...