Age in Days... But now freehand!?

  • Thread starter Simon Mansfield
  • Start date
S

Simon Mansfield

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];
}
} else {
for(i = (tmnth - 1); i > 0; i--) {
dayCount += YearDb[2];
}
}
// 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];
}
} else {
for(i = (mnth + 1); i >= 12; i++) {
dayCount += YearDb[2];
}
}

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

getchar();
getchar();

return 0;
}
 
I

ivan.coughlan

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.
 
A

Al Bowers

Simon said:
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;
}
 
S

Simon Mansfield

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

Simon Mansfield

Ignore my last post.. I now understand.. I was referencing [1] and [2]
when it should have been [0] and [1].. Thanks..

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

Simon
 
I

ivan.coughlan

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'
 
I

ivan.coughlan

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'
 
S

Simon Mansfield

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
 

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

No members online now.

Forum statistics

Threads
473,763
Messages
2,569,562
Members
45,038
Latest member
OrderProperKetocapsules

Latest Threads

Top