944,206 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 1953
  • C++ RSS
Jul 1st, 2005
0

Your exact age

Expand Post »
this program contains everything even leap year:
i am a beginner in cpp (we have this in school so i am learning this age 15 )

C++ Syntax (Toggle Plain Text)
  1. # include<iostream.h>
  2. # include<conio.h>
  3.  
  4. void main (void)
  5.  
  6. {
  7.  
  8.  
  9. clrscr();
  10.  
  11. int years1, months1, days1;
  12. int monthdays1[12] = {31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365};
  13. int startday;
  14. int years2, months2, days2;
  15. int endday;
  16. int leapdays1, leapdays2;
  17. int oldyears, agebalance, balancedays, oldmonths, olddays, i;
  18.  
  19. int const BASEYEAR=1900;
  20. int const BASEMONTH=12;
  21. int const BASEDAY=31;
  22.  
  23. //Input birth date
  24.  
  25. cout<<"Enter the date you were born(dd mm yyyy):\t";
  26. cin>>days1>>months1>>years1;
  27. cout<<endl;
  28.  
  29. //error
  30. if (years1<BASEYEAR)
  31. {
  32. cout<<"The value of year is below 1900 Press any key to continue";
  33. getch();
  34. return;
  35. }
  36. if (months1>BASEMONTH)
  37. {
  38. cout<<"The value of months is above 12 Press any key to continue";
  39. getch();
  40. return;
  41. }
  42.  
  43. if (days1>BASEDAY)
  44. {
  45. cout<<"The value of days is above 31 Press any key to continue";
  46. getch();
  47. return;
  48. }
  49.  
  50. //input todays date
  51.  
  52. cout<<"Enter the date today(dd mm yyyy):\t";
  53. cin>>days2>>months2>>years2;
  54. cout<<endl;
  55.  
  56. //error
  57. if (years2<BASEYEAR)
  58. {
  59. cout<<"The value of years is below 1900 Press any key to continue";
  60. getch();
  61. return;
  62. }
  63. if (months2>BASEMONTH)
  64. {
  65. cout<<"The value of months is above 12 Press any key to continue";
  66. getch();
  67. return;
  68. }
  69.  
  70. if (days2>BASEDAY)
  71. {
  72. cout<<"The value of days is above 31 Press any key to continue";
  73. getch();
  74. return;
  75. }
  76.  
  77. //calculation
  78. years1=years1-1;
  79. months1=months1-1;
  80. days1=days1;
  81.  
  82. leapdays1=years1/4; //leapyear is a multiple for 4
  83. if((months1>=2) && ((years1+1)%4==0)) leapdays1++;
  84. if(years1>1999) leapdays1--; //2000 is no leapyear
  85.  
  86.  
  87. startday=(years1*365)+monthdays1[months1-1]+days1+leapdays1;
  88.  
  89. years2=years2-1;
  90. months2=months2-1;
  91. days2=days2;
  92.  
  93. leapdays2=years2/4; //leapyear is a multiple for 4
  94.  
  95. if((months2>=2) && ((years2+1)%4==0)) leapdays2++;
  96. if(years2>1999) leapdays2--; //2000 is no leapyear
  97.  
  98.  
  99. endday=(years2*365)+monthdays1[months2-1]+days2+leapdays2;
  100.  
  101. agebalance=endday-startday;
  102. oldyears=agebalance/365;
  103. balancedays=agebalance%365;
  104. i=0;
  105. while (balancedays> monthdays1[i])
  106. {
  107. i++;
  108. }
  109. oldmonths=i;
  110.  
  111. olddays=balancedays-monthdays1[oldmonths-1];
  112.  
  113. //output
  114. cout<<"Your age is "<<oldyears<<" years, "<<oldmonths<<" months, ";
  115. cout<<olddays<<" days"<<endl;
  116. getch();
  117. }
<< moderator edit: added [code][/code] tags >>
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
cpp noob is offline Offline
15 posts
since Jul 2005
Jul 1st, 2005
0

Re: Your exact age

Quote originally posted by cpp noob ...
C++ Syntax (Toggle Plain Text)
  1. if(years1>1999) leapdays1--; //2000 is no leapyear
2000 was a leap year.
Team Colleague
Reputation Points: 1135
Solved Threads: 173
Super Senior Demiposter
Rashakil Fol is offline Offline
2,480 posts
since Jun 2005
Jul 2nd, 2005
0

Re: Your exact age

hmmmm...
thanks
Reputation Points: 10
Solved Threads: 0
Newbie Poster
cpp noob is offline Offline
15 posts
since Jul 2005

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: help with saving data to and reading from file
Next Thread in C++ Forum Timeline: Visual Studio 6.0 or Visual Studio .Net 2003





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC