Family Age Statistics Program

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

Join Date: Sep 2009
Posts: 1
Reputation: grijalva10 is an unknown quantity at this point 
Solved Threads: 0
grijalva10 grijalva10 is offline Offline
Newbie Poster

Family Age Statistics Program

 
1
  #1
Oct 4th, 2009
Hi I am new to C++ and new to programming in general. I would greatly appreciate if you could help me. I need to create a program using loops, that will do the following 3 task:

1. For each of the three persons, determine and report whether that person was born in a leap year or not. Check whether the birthday information is valid. For example, no one could be born on February 29, 2005. It is an invalid date. If it is not valid, report an “Invlaid Date” message to the user and then have a return statement like: return 0; to stop the execution of the main function.
2. Then for each of the three persons, determine and report how many of the other two persons are (is) older than that person.
3. Finally, use the information in step 3 to determine and report who is (are) among the oldest in the family


I am having a problem comparing birthdays properly. Also any advice on cleaning up my code or making it more efficient would be great.

  1. //*------------- Family Statics Program ---------- */
  2.  
  3.  
  4. #include <string>
  5. #include <iostream>
  6.  
  7. using namespace std;
  8.  
  9.  
  10. /*------------ Beginning of main function ----------*/
  11. int main()
  12.  
  13. {
  14.  
  15. /*---------- Declaring Variables ----------*/
  16. int m, d , y;
  17. int fatherM, fatherD, fatherY;
  18. int motherM, motherD, motherY;
  19. int age, fatherAge, motherAge;
  20.  
  21.  
  22.  
  23.  
  24.  
  25. /*---------- Getting User DOB----------*/
  26. cout << "Please enter your date of birth (mm dd yyyy): " << endl;
  27. cout << "Month (1 - 12): ";
  28. cin >> m;
  29. cout << "Day (1 - 31): ";
  30. cin >> d;
  31. cout << "Year (e.g. 1996): ";
  32. cin >> y;
  33.  
  34. /*---------- Validating Bday for user --------------------------------------------------------------------------------------------------*/
  35. /*--------------------------------------------------------------------------------------------------------------------------------------*/
  36. /*--------------------------------------------------------------------------------------------------------------------------------------*/
  37.  
  38. while ((m < 1|| m > 12) || (d < 1 || d > 31) || (d==31 && m==2 || m==4 || m==6 || m==9 || m==11))
  39. {
  40. cout << " Invalid date, start over"<<endl;
  41. return 0;
  42. }
  43.  
  44. while ( (d==30) && (m==2) )
  45. {
  46. cout << " Invalid date, start over"<<endl;
  47. return 0;
  48. }
  49.  
  50. while ( (m==2) && (d==29) && (y%4!=0) )
  51. {
  52. cout << " Invalid date, start over"<<endl;
  53. return 0;
  54. }
  55. while ( (m==2) && (d==29) && (y%100==0) )
  56. {
  57. cout << " Invalid date, start over"<<endl;
  58. return 0;
  59. }
  60. /*---------- Determine leap year----------*/
  61.  
  62. if(y%400 ==0 || (y%100 != 0 && y%4 == 0))
  63. {
  64. cout<< " You were born on a leap year!"<<endl;
  65. }
  66. else
  67. {
  68. cout<< " You were not born on a leap year."<<endl;
  69. }
  70.  
  71.  
  72.  
  73. /*---------- Getting Father DOB---------------------------------------------------------------------------------------------------------*/
  74. /*--------------------------------------------------------------------------------------------------------------------------------------*/
  75. /*--------------------------------------------------------------------------------------------------------------------------------------*/
  76.  
  77. cout << "Please enter your father's date of birth. (mm dd yyyy): " << endl;
  78. cout << "Month (1 - 12): ";
  79. cin >> fatherM;
  80. cout << "Day (1 - 31): ";
  81. cin >> fatherD;
  82. cout << "Year (e.g. 1996): ";
  83. cin >> fatherY;
  84.  
  85. /*---------- Validating Bday for father ----------*/
  86. while ((fatherM < 1|| fatherM > 12) || (fatherD < 1 || fatherD > 31) || (fatherD==31 && fatherM==2 || fatherM==4 || fatherM==6 || fatherM==9 || fatherM==11))
  87. {
  88. cout << " Invalid date, start over"<<endl;
  89. return 0;
  90. }
  91.  
  92. while ( (fatherD==30) && (fatherM==2) )
  93. {
  94. cout << " Invalid date, start over"<<endl;
  95. return 0;
  96. }
  97.  
  98. while ( (fatherM==2) && (fatherD==29) && (fatherY%4!=0) )
  99. {
  100. cout << " Invalid date, start over"<<endl;
  101. return 0;
  102. }
  103. while ( (fatherM==2) && (fatherD==29) && (fatherY%100==0) )
  104. {
  105. cout << " Invalid date, start over"<<endl;
  106. return 0;
  107. }
  108. /*---------- Determine leap year----------*/
  109.  
  110. if(fatherY%400 ==0 || (fatherY%100 != 0 && fatherY%4 == 0))
  111. {
  112. cout<< " Your father born on a leap year!"<<endl;
  113. }
  114. else
  115. {
  116. cout<< " Your father was not born on a leap year."<<endl;
  117. }
  118.  
  119. /*---------- Getting Mother DOB---------------------------------------------------------------------------------------------------------*/
  120. /*--------------------------------------------------------------------------------------------------------------------------------------*/
  121. /*--------------------------------------------------------------------------------------------------------------------------------------*/
  122.  
  123. cout << "Please enter your father's date of birth. (mm dd yyyy): " << endl;
  124. cout << "Month (1 - 12): ";
  125. cin >> motherM;
  126. cout << "Day (1 - 31): ";
  127. cin >> motherD;
  128. cout << "Year (e.g. 1996): ";
  129. cin >> motherY;
  130.  
  131. /*---------- Validating Bday for mother ----------*/
  132. while ((motherM < 1|| motherM > 12) || (motherD < 1 || fatherD > 31) || (motherD==31 && motherM==2 || motherM==4 || motherM==6 || motherM==9 || motherM==11))
  133. {
  134. cout << " Invalid date, start over"<<endl;
  135. return 0;
  136. }
  137.  
  138. while ( (motherD==30) && (motherM==2) )
  139. {
  140. cout << " Invalid date, start over"<<endl;
  141. return 0;
  142. }
  143.  
  144. while ( (motherM==2) && (motherD==29) && (motherY%4!=0) )
  145. {
  146. cout << " Invalid date, start over"<<endl;
  147. return 0;
  148. }
  149. while ( (motherM==2) && (motherD==29) && (motherY%100==0) )
  150. {
  151. cout << " Invalid date, start over"<<endl;
  152. return 0;
  153. }
  154. /*---------- Determine leap year----------*/
  155.  
  156. if(motherY%400 ==0 || (motherY%100 != 0 && motherY%4 == 0))
  157. {
  158. cout<< " Your mother was born on a leap year!"<<endl;
  159. }
  160. else
  161. {
  162. cout<< " Your mother was not born on a leap year."<<endl;
  163. }
  164.  
  165. /*------Comparing Ages------------------------------------------------------------------------------------------------------------------*/
  166. /*--------------------------------------------------------------------------------------------------------------------------------------*/
  167. /*--------------------------------------------------------------------------------------------------------------------------------------*/
  168.  
  169. age = (m * 100) + (y * 10000) + (d);
  170. fatherAge = (fatherM * 100) + (fatherY * 10000) + fatherD;
  171. motherAge = (motherM * 100) + (motherY * 10000) + motherD;
  172.  
  173. if ( (age < fatherAge) && (age < motherAge) )
  174. {
  175. cout << " Your father and mother are older than you."<<endl;
  176. }
  177.  
  178. if ( (age > fatherAge) && (fatherAge < motherAge) )
  179. {
  180. cout << " Your father is the oldest amongst you and your mother.";
  181. }
  182.  
  183. if ( (motherAge < age) && (motherAge < fatherAge) )
  184. {
  185. cout << " Your mother is the oldest amongst you and your father.";
  186. }
  187.  
  188. if ( (age > fatherAge) && (age > motherAge) )
  189. {
  190. cout << " You are older than your father and mother."<<endl;
  191. }
  192. if ( (fatherAge > age) && (fatherAge > motherAge) )
  193. {
  194. cout << " Your father is younger than you and your mother.";
  195. }
  196. if ( (motherAge > age) && (motherAge > fatherAge) )
  197. {
  198. cout << " Your mother is the oldest amongst you and your father.";
  199. }
  200. if ((age > fatherAge) && (age < motherAge))
  201. {
  202. cout<< "You are older than your father but younger than your mother.";
  203. }
  204. if ((age > fatherAge) && (age > motherAge))
  205. {
  206. cout << "You are older than your mother but younger than your father.";
  207. }
  208.  
  209.  
  210.  
  211.  
  212.  
  213. if ((fatherAge > age) && (fatherAge > motherAge))
  214. {
  215. cout<< " Your father is the oldest. " <<endl;
  216. }
  217. if ((age < fatherAge) && (age > motherAge))
  218. {
  219. cout<< " You are the oldest. " <<endl;
  220. }
  221. if ((motherAge > age) && (motherAge > fatherAge))
  222. {
  223. cout<< " Your mother is the oldest. " <<endl;
  224. }
  225.  
  226.  
  227.  
  228.  
  229.  
  230.  
  231.  
  232.  
  233.  
  234. return 0;
  235. }
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 61
Reputation: chunalt787 is an unknown quantity at this point 
Solved Threads: 1
chunalt787 chunalt787 is offline Offline
Junior Poster in Training
 
0
  #2
Oct 6th, 2009
Your problem is in this line right here
  1. age = (m * 100) + (y * 10000) + (d);
I somewhat see what you are trying to do but that's a strange way of doing it. Try three nested if statements. For example to check if father is older than mother do this:

-check is fathers year < mothers year
-if yes is fathers month < mothers month
-if yes is fathers day < mothers day?

if any of these are no then the mother is older than the father.
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 61
Reputation: chunalt787 is an unknown quantity at this point 
Solved Threads: 1
chunalt787 chunalt787 is offline Offline
Junior Poster in Training
 
0
  #3
Oct 6th, 2009
Sorry for the double, post deleted look at post above
Last edited by chunalt787; Oct 6th, 2009 at 3:53 pm. Reason: Accidental double posting
Reply With Quote Quick reply to this message  
Reply

Message:


Thread Tools Search this Thread



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

©2003 - 2009 DaniWeb® LLC