473,395 Members | 1,637 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

What are my errors in this code?

Expand|Select|Wrap|Line Numbers
  1. /* Add days to a date structure and obtain new date */
  2.  
  3. #include<cstdio>
  4. #include<cstdlib>
  5.  
  6. struct date
  7. {
  8.     int day;
  9.     int month;
  10.     int year;
  11. };
  12.  
  13. const int mdays[13] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
  14.  
  15. void addDays(struct date *, const int);
  16. int isleapyear(const int);
  17.  
  18. int main(void)
  19. {
  20.     struct date dt = {31, 1, 2000};
  21.  
  22.     for(int i = 1000; i < 5000; i += 1000)
  23.     {
  24.         addDays(&dt, i);
  25.         printf("%d-%d-%d\n", dt.day, dt.month, dt.year);
  26.     }
  27.  
  28.     return 0;
  29. }
  30.  
  31. void addDays(struct date *dt, const int days)
  32. {
  33.     (*dt).day += days;
  34.  
  35.     while((*dt).day > mdays[(*dt).month])
  36.     {
  37.         if(isleapyear((*dt).year) && (*dt).month == 2)
  38.         {
  39.             (*dt).day = (*dt).day - mdays[(*dt).month] - 1;
  40.  
  41.             /* only for displaying 29 instead of 0 */
  42.             if(!(*dt).day)
  43.                 (*dt).day = 29;
  44.         }
  45.         else
  46.             (*dt).day = (*dt).day - mdays[(*dt).month];
  47.  
  48.         (*dt).month += 1;
  49.  
  50.         if((*dt).month > 12)
  51.         {
  52.             (*dt).year += 1;
  53.             (*dt).month %= 12;
  54.         }
  55.     }
  56. }
  57.  
  58. int isleapyear(const int year)
  59. {
  60.     if((!(year % 4) && !(year % 10)) || !(year % 400))
  61.         return 1;
  62.     else
  63.         return 0;
  64. }
  65.  
  66. in the following code i am trying to add days to the structure date and trying to change the values of the structure to a new date. Problem is for every 1000 days added to the actual date, the error in the value of day is as follows
  67.  
  68. //ouput
  69.  
  70. 1000 days added - 0 days error in the modified structure
  71. 2000 days added - 2 days extra in the modified structure
  72. 3000 days added - 4 days extra in the modified structure
  73. 4000 days added - 5 days extra in the modified structure
  74.  
  75. //end of output
  76. i checked by coding in C# and the above extra days are relative to the values obtained from this C# program(ofcourse i used built in library functions). I know i am doing something wrong somewhere, i wonder if its in the concept of days calculated for a year 365.25xxx or is it logic problem.
Mar 3 '09 #1
4 1210
JosAH
11,448 Expert 8TB
There's a typo in your isleapyear function; make that % 100 instead of % 10.

kind regards,

Jos
Mar 3 '09 #2
thank you. what a silly mistake

but still i am having the same output
Mar 3 '09 #3
JosAH
11,448 Expert 8TB
@tsatyadev
Your isleapyear function is still incorrect: centuries are never leapyears unless the year can be divided by 400.

kind regards,

Jos
Mar 3 '09 #4
thank you for your time

i understand what went wrong
Mar 3 '09 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

20
by: Market Mutant | last post by:
www.51.ca is a nice site, and I want to build something similar and wonder if anyone knows if they are using PHPNuke or other nukes or they wrote everything themselves. What is the signature thing...
121
by: typingcat | last post by:
First of all, I'm an Asian and I need to input Japanese, Korean and so on. I've tried many PHP IDEs today, but almost non of them supported Unicode (UTF-8) file. I've found that the only Unicode...
4
by: atv | last post by:
Whatis the proper way to handle errors from function calls? For example, i normally have a main function, with calls to mine or c functions. Should i check for errors in the functions called...
13
by: Jason Huang | last post by:
Hi, Would someone explain the following coding more detail for me? What's the ( ) for? CurrentText = (TextBox)e.Item.Cells.Controls; Thanks. Jason
12
by: William | last post by:
VB6 had the ability to pause your execution while debugging, messing with the code, and continue executing right from where you left off. Is DotNet ever going to have this capability? I really...
669
by: Xah Lee | last post by:
in March, i posted a essay “What is Expressiveness in a Computer Language”, archived at: http://xahlee.org/perl-python/what_is_expresiveness.html I was informed then that there is a academic...
11
by: Howard Kaikow | last post by:
I'm using the code below, but an error is not getting trapped. Where can such errors occur? In another process? Try SomeCode Catch ex As Exception Dim strMsg() As String = Split(ex.ToString,...
0
by: clemrock | last post by:
Help w/ errors.add_to_base between controller and model Hello, I'm having trouble in routing some errors between model and controller. The errors produced in the controller...
2
by: Tarik Monem | last post by:
OK! I've gone through a few tutorials and I cannot understand what I'm doing wrong casting_registration.php <table> <tr> <td> <form enctype="multipart/form-data" action="thankyou.php"...
5
by: adam.timberlake | last post by:
I've just finished reading the article below which goes into some depth about exceptions. The article was rather lucid and so I understand how to implement it all, the thing I'm having trouble with...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
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,...
0
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
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...

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.