Your exact age

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Jul 2005
Posts: 15
Reputation: cpp noob is an unknown quantity at this point 
Solved Threads: 0
cpp noob cpp noob is offline Offline
Newbie Poster

Your exact age

 
0
  #1
Jul 1st, 2005
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 )

  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 >>
Reply With Quote Quick reply to this message  
Join Date: Jun 2005
Posts: 2,044
Reputation: Rashakil Fol is just really nice Rashakil Fol is just really nice Rashakil Fol is just really nice Rashakil Fol is just really nice 
Solved Threads: 139
Team Colleague
Rashakil Fol's Avatar
Rashakil Fol Rashakil Fol is offline Offline
Super Senior Demiposter

Re: Your exact age

 
0
  #2
Jul 1st, 2005
Originally Posted by cpp noob
  1. if(years1>1999) leapdays1--; //2000 is no leapyear
2000 was a leap year.
Reply With Quote Quick reply to this message  
Join Date: Jul 2005
Posts: 15
Reputation: cpp noob is an unknown quantity at this point 
Solved Threads: 0
cpp noob cpp noob is offline Offline
Newbie Poster

Re: Your exact age

 
0
  #3
Jul 2nd, 2005
hmmmm...
thanks
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC