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;
} 8 1865
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.
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(2 004,12,6); /* 06DEC2004 */
long yesterday = ymd_to_scalar(2 004,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(y r);
return scalar;
}
--
Al Bowers
Tampa, Fl USA
mailto: xa******@myrapi dsys.com (remove the x to send email) http://www.geocities.com/abowers822/
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?
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
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'
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'
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.goo glegroups.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' This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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 type of data:
|
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 geographical region.
I have written a query which prompts the user for the start and end
dates. It also filters for entries which pertain to the particular
geographical region.
I'm not sure where to go from here.
|
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 example is: 36,525 represents 01/01/1900. The starting point date is considered to be :
00/00/0000. I have looked thru Help and used Google and have not really found an answer.
I know that Leap Years need to be accounted for too.
Any...
|
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:
--------
|
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
| |
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 calculate in the query the amount of weeks and days within the 17 days to show 2 weeks 3 days.
I can create the function within excel, (A1 as cell with the days within it) as:
=INT(A1/7) & " Weeks, " & MOD(A1,7) & " days"
but cant seem to...
|
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
|
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 cannot
be done from starting from bottom-right to up-left. The only selection
i'm allowed to do is starting from top-left towards bottom-right
orientation.
The code is:
|
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 should see 4 business days and 2
weekend days.
How can I get that result? I'm getting 4 business days but its not counting
the weekend days?
|
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
|
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
| |
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
|
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
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 then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
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 we have to send another system
| |