473,503 Members | 11,783 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Age in Days... But now freehand!?

The code has the same end result as the last one.. It must tell the user how
many days it has been since their birthday. Now it does give a result but
its normally off by a bit.. I have figured out which bit of the code is
causing this error, but, can't figure out exactly what is wrong with it! :(

The bit of code that is incorrect I have highlighted below.

#include <stdio.h>

static int YearDb [2][13] = { //A database of the number of days in each
month in a normal and a leap year.
{0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31},
{0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}
};

int leapyr(int year) { //Works out whether a given year is a leap year or
not.
if(year % 4 == 0 && year % 100 != 0 || year % 400 == 0) {
return 1;
} else {
return 0;
}
}

int main(void) {
int day, mnth, yr;
int tday, tmnth, tyr;
int i = 0;
int dayCount = 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");

//Prompt the user for today's date.
printf("Please enter today's date [DD/MM/YYYY]: ");

//Read in the date.
scanf("%2d/%2d/%4d", &tday, &tmnth, &tyr);
printf("\n\n");

dayCount = tday; //Add this months days

// THIS BIT IS THE CODE THAT DOES NOT FUNCTION AS EXPECTED >>
// This adds all this years days up to now.
if(leapyr(tyr) != 1) {
for(i = (tmnth - 1); i > 0; i--) {
dayCount += YearDb[1][i];
}
} else {
for(i = (tmnth - 1); i > 0; i--) {
dayCount += YearDb[2][i];
}
}
// THIS BIT IS THE CODE THAT DOES NOT FUNCTION AS EXPECTED <<

// Adds all the years days from last year till the year after their
birthdate.
for(i = (tyr - 1); i > yr; i--) {
if(leapyr(i) != 1) {
dayCount += 365;
} else {
dayCount += 366;
}
}

// Adds days till the end of the month from birthday.
if(leapyr(yr) != 1) {
dayCount += (YearDb[1][mnth] - day);
} else {
dayCount += (YearDb[2][mnth] - day);
}

// Adds the remaining months of the birthyear.
if(leapyr(yr) != 1) {
for(i = (mnth + 1); i >= 12; i++) {
dayCount += YearDb[1][i];
}
} else {
for(i = (mnth + 1); i >= 12; i++) {
dayCount += YearDb[2][i];
}
}

printf("You have been alive for %d days!", dayCount);

getchar();
getchar();

return 0;
}
Nov 14 '05 #1
8 1852
thrhrthrt

Nov 14 '05 #2
YearDb indexes are wrong - indexes start at 0 not 1. (Use the return
from the leap year function)
You should put brackets around the '||' (OR) part of 'if' in the leap
year function.
The last part ( // Adds the remaining months of the birthyear) in the
'for' statement, the expression is wrong.

Nov 14 '05 #3


Simon Mansfield wrote:
The code has the same end result as the last one.. It must tell the user how
many days it has been since their birthday. Now it does give a result but
its normally off by a bit.. I have figured out which bit of the code is
causing this error, but, can't figure out exactly what is wrong with it! :(


..........code snipped................

Instead of figuring out the errors, why don't you look at
Ray Gardner's published scalar date and time functions located at:
http://c.snippets.org/browser.php#27

Example of some of the functions provided:

#include <stdio.h>

int isleap (unsigned yr);
unsigned months_to_days (unsigned month);
long years_to_days (unsigned yr);
long ymd_to_scalar (unsigned yr, unsigned mo, unsigned day);

int main(void)
{
long today = ymd_to_scalar(2004,12,6); /* 06DEC2004 */
long yesterday = ymd_to_scalar(2004,12,5); /* 0DEC2004 */

printf("today - yesterday = %ld day%s\n",
today-yesterday,today-yesterday==1?"":"s");
return 0;
}

int isleap (unsigned yr)
{
return yr % 400 == 0 || (yr % 4 == 0 && yr % 100 != 0);
}

unsigned months_to_days (unsigned month)
{
return (month * 3057 - 3007) / 100;
}

long years_to_days (unsigned yr)
{
return yr * 365L + yr / 4 - yr / 100 + yr / 400;
}

long ymd_to_scalar (unsigned yr, unsigned mo, unsigned day)
{
long scalar;
scalar = day + months_to_days(mo);
if ( mo > 2 ) /* adjust if past February */
scalar -= isleap(yr) ? 1 : 2;
yr--;
scalar += years_to_days(yr);
return scalar;
}
--
Al Bowers
Tampa, Fl USA
mailto: xa******@myrapidsys.com (remove the x to send email)
http://www.geocities.com/abowers822/

Nov 14 '05 #4
Since i've nearly got it working this way, i'd quite like to stick to this
method.. However Ivan I dont quite understand what you are saying about the
YearDb indexing.. Where have I stated that it starts at 1 in the code?
Nov 14 '05 #5
Ignore my last post.. I now understand.. I was referencing [1][i] and [2][i]
when it should have been [0][i] and [1][i].. Thanks..

However I dont get what is wrong with the for statement.... Could you
clarify please?

Simon
Nov 14 '05 #6
for(i = (mnth + 1); i >= 12; i++) {

1>=12 will go on forever if i starts out being greater than 12 and not
run at all if i<12.
I think you want to say '1<=12'

Nov 14 '05 #7
for(i = (mnth + 1); i >= 12; i++) {

1>=12 will go on forever if i starts out being greater than 12 and not
run at all if i<12.
I think you want to say '1<=12'

Nov 14 '05 #8
Ah yes, sorry being a bit blonde there for a moment! All works nicely, with
the expected result given now, so I am one happy man! ;)

Simon
<iv***********@gmail.com> wrote in message
news:11**********************@f14g2000cwb.googlegr oups.com...
for(i = (mnth + 1); i >= 12; i++) {

1>=12 will go on forever if i starts out being greater than 12 and not
run at all if i<12.
I think you want to say '1<=12'

Nov 14 '05 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
3928
by: Johnson, Shaunn | last post by:
Howdy: Running PostgreSQL 7.2.1 on RedHat Linux 7.2. How can I convert data in a table that has been created with the INTERVAL data type into a numeric format? Say, I have a table with this...
5
14870
by: BlackFireNova | last post by:
I need to write a report in which one part shows a count of how many total records fall within the working days (Monday - Friday) inside of a (prompted) given date range, in a particular...
29
9028
by: james | last post by:
I have a problem that at first glance seems not that hard to figure out. But, so far, the answer has escaped me. I have an old database file that has the date(s) stored in it as number of days. An...
19
4496
by: Ricardo Perez Lopez | last post by:
Hello everyone: I'm a PostgreSQL newbie, working now with dates, times, timestamps and intervals. I have three questions about the above: FIRST: --------
11
11235
by: dongarbage | last post by:
Hi there, I'm very much a C# novice. How do you do freehand drawing on a panel with a mouse in c#? Thanks, Don
3
5942
by: Libber39 | last post by:
Hi everyone, Have a query on how to calculate the amount of weeks and days contained in a number in an access query. ie: the difference in days between 2 dates amounts to 17 days. I want to now...
3
6423
by: hharry | last post by:
Hello All, I'm writing an app to track file transfer activity. I have this enum to represent days of the week: Monday = 1 Tuesday = 2 Wednesday = 3 Thursday = 4
10
5449
by: kimiraikkonen | last post by:
Hi, If previous post was missing, here's the complete one: I'm trying to draw a rectange on a picturebox image using mouse move event but the problem is that the rectangle selection / drawing...
7
3211
by: Mike | last post by:
I have a routine that's calculating business days but its not counting the weekend days that are between the start date and end date. If my start date is 9/26/08 and my end date is 10/01/08, I...
0
7264
Oralloy
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,...
0
7316
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
6975
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7449
tracyyun
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...
0
5562
agi2029
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,...
0
4666
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3160
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3148
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1495
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 using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.