Beginner and I would love help :) date validation

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

Join Date: Mar 2006
Posts: 11
Reputation: porterrj is an unknown quantity at this point 
Solved Threads: 0
porterrj porterrj is offline Offline
Newbie Poster

Beginner and I would love help :) date validation

 
0
  #1
Apr 12th, 2006
hello!

I need a program that validates the imput of dates. (Checks for leap years etc.)

The user enters: mm/dd/yyyy

The problem is, I have trouble with complex programs. I came up with this program, but i know it's too basic for what she wants. I need to have arrays in there, but i don't know how. I would love any help considering I'm bad at understanding this subject.




could someone help me ???!!!


  1. bool GoodDate(int month,int day,int year)
  2.  
  3.  
  4. {
  5. /*** Bad Month ***/
  6. if(month < 1 || month > 12)
  7. return false;
  8.  
  9. /*** January ***/
  10. else if(month == 1)
  11.  
  12.  
  13. {
  14. if(day >=1 && day<= 31)
  15. return true;
  16. else
  17. return false;
  18. }
  19.  
  20. /*** Febuary ***/
  21. else if(month == 2) // divisible by 4 and either divisible by 400 or not divisible by 100
  22.  
  23.  
  24. {
  25. /*** if the year is a leap year... ***/
  26. if(year % 4 == 0 && (year % 400 == 0 || year % 100 != 0))
  27.  
  28.  
  29. {
  30. if(day >= 1 && day <= 29)
  31. return true;
  32. else
  33. return false;
  34. }
  35. /*** if the year is not a leap year... ***/
  36. else
  37.  
  38. {
  39. if(day >=1 && day <= 28)
  40. return true;
  41. else
  42. return false;
  43. }
  44. }
  45.  
  46. /*** March ***/
  47. else if(month == 3)
  48.  
  49.  
  50. {
  51. if(day >=1 && day<= 31)
  52. return true;
  53. else
  54. return false;
  55. }
  56.  
  57. /*** April ***/
  58. else if(month == 4)
  59.  
  60.  
  61. {
  62. if(day >=1 && day<= 30)
  63. return true;
  64. else
  65. return false;
  66. }
  67.  
  68. /*** May ***/
  69. else if(month == 5)
  70.  
  71.  
  72. {
  73. if(day >=1 && day<= 31)
  74. return true;
  75. else
  76. return false;
  77. }
  78.  
  79. /*** June ***/
  80. else if(month == 6)
  81.  
  82.  
  83. {
  84. if(day >=1 && day<= 30)
  85. return true;
  86. else
  87. return false;
  88. }
  89.  
  90. /*** July ***/
  91. else if(month == 7)
  92.  
  93.  
  94. {
  95. if(day >=1 && day<= 31)
  96. return true;
  97. else
  98. return false;
  99. }
  100.  
  101. /*** August ***/
  102. else if(month == 8)
  103.  
  104.  
  105. {
  106. if(day >=1 && day<= 31)
  107. return true;
  108. else
  109. return false;
  110. }
  111.  
  112. /*** September ***/
  113. else if(month == 9)
  114.  
  115.  
  116. {
  117. if(day >=1 && day<= 30)
  118. return true;
  119. else
  120. return false;
  121. }
  122.  
  123. /*** October ***/
  124. else if(month == 10)
  125.  
  126.  
  127. {
  128. if(day >=1 && day<= 31)
  129. return true;
  130. else
  131. return false;
  132. }
  133.  
  134. /*** November ***/
  135. else if(month == 11)
  136.  
  137.  
  138. {
  139. if(day >=1 && day<= 30)
  140. return true;
  141. else
  142. return false;
  143. }
  144.  
  145. /*** December ***/
  146. else if(month == 12)
  147.  
  148.  
  149. {
  150. if(day >=1 && day<= 31)
  151. return true;
  152. else
  153. return false;
  154. }
  155. else
  156.  
  157. {
  158. cout<<"Error\n";
  159. exit(0);
  160. }
  161. return false;
  162. }
Reply With Quote Quick reply to this message  
Join Date: Feb 2005
Posts: 466
Reputation: winbatch is on a distinguished road 
Solved Threads: 18
winbatch's Avatar
winbatch winbatch is offline Offline
Posting Pro in Training

Re: Beginner and I would love help :) date validation

 
0
  #2
Apr 12th, 2006
Let me give you this hint - since several of the months have the same behavior - ie 1,3,5,7,8,10,12 are all 31 days, while 4,6,9,11 are all 30 days, then you could easily use a 'switch' statement on your month and not have to repeat all of that logic.
Reply With Quote Quick reply to this message  
Join Date: Mar 2006
Posts: 11
Reputation: porterrj is an unknown quantity at this point 
Solved Threads: 0
porterrj porterrj is offline Offline
Newbie Poster

Re: Beginner and I would love help :) date validation

 
0
  #3
Apr 13th, 2006
okay I came up with something a little more complex, but it's still not working. I know it's somewhere within my functions (either the bools or whatnot)......basically it's not telling them they entered an invalid date.

Any help?? I didn't put it in there, because I can't figure out where it goes logically.


/
  1. ************************Includes**************************************/
  2.  
  3. # include<iostream>
  4. # include<iomanip>
  5. using namespace std;
  6.  
  7. /************************Function call*********************************/
  8.  
  9. int isLeapYear(int year); //function
  10. int dateIsValid (int day, int month, int year); //function
  11.  
  12.  
  13. /***********************Main******************************************/
  14.  
  15. int main()
  16. {
  17. int value1, value2, value3;
  18. char again;
  19.  
  20. do
  21. {
  22. cout << "Enter the day, month, and year. \n";
  23. cin >> value1 >> value2 >> value3;
  24. dateIsValid(value1, value2, value3); //call function
  25. cout << value1<<" "<< value2 <<" "<< value3<<endl;
  26. cout << " Do you want to enter another date? (Y/N)";
  27. cin >>again;
  28.  
  29. }while (again =='Y' || again =='y');
  30.  
  31. return 0;
  32. }
  33.  
  34. /*************************FUNCTION*********************************/
  35.  
  36. int dateIsValid(int day, int month, int year)
  37.  
  38. {
  39. bool valid;
  40. int monthLength[13] = { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
  41.  
  42. if ( isLeapYear(year) )
  43. monthLength[2] = 29; // 29 days in February in a leap year
  44.  
  45. if ( month < 1 || month > 12 )
  46. valid = false;
  47. else if ( day < 1 || day > monthLength[month] )
  48. valid = false;
  49.  
  50. return ( valid );
  51.  
  52. }
  53.  
  54. /****************************FUNCTION*******************************************/
  55.  
  56. int isLeapYear(int year)
  57. {
  58. bool result;
  59.  
  60. if ( (year%4) != 0 ) // or: if ( year%4 )
  61. result = false; // means: if year is not divisible by 4
  62. else if ( (year%400) == 0 ) // or: if ( !(year%400) )
  63. result = true; // means: if year is divisible by 400
  64. else if ( (year%100) == 0 ) // or: if ( !(year%100) )
  65. result = false; // means: if year is divisible by 100
  66. else // (but not by 400, since that case
  67. result = true; // considered already)
  68.  
  69. return ( result );
  70.  
  71. }
Reply With Quote Quick reply to this message  
Join Date: Feb 2006
Posts: 23
Reputation: jlouang is an unknown quantity at this point 
Solved Threads: 1
jlouang jlouang is offline Offline
Newbie Poster

Re: Beginner and I would love help :) date validation

 
0
  #4
Apr 13th, 2006
put ur whole code so I can find the problem I did the same thing last week for my assignment.
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 5
Reputation: blacklight332 is an unknown quantity at this point 
Solved Threads: 0
blacklight332 blacklight332 is offline Offline
Newbie Poster

Re: Beginner and I would love help :) date validation

 
1
  #5
Mar 3rd, 2009
These examples are overly complicated. I found a simple one that totally handles leap years.

from: c++ checkdate date validation

  1. bool checkdate(int m, int d, int y)
  2. {
  3. //gregorian dates started in 1582
  4. if (! (1582<= y ) )//comment these 2 lines out if it bothers you
  5. return false;
  6. if (! (1<= m && m<=12) )
  7. return false;
  8. if (! (1<= d && d<=31) )
  9. return false;
  10. if ( (d==31) && (m==2 || m==4 || m==6 || m==9 || m==11) )
  11. return false;
  12. if ( (d==30) && (m==2) )
  13. return false;
  14. if ( (m==2) && (d==29) && (y%4!=0) )
  15. return false;
  16. if ( (m==2) && (d==29) && (y%400==0) )
  17. return true;
  18. if ( (m==2) && (d==29) && (y%100==0) )
  19. return false;
  20. if ( (m==2) && (d==29) && (y%4==0) )
  21. return true;
  22.  
  23. return true;
  24. }
Last edited by blacklight332; Mar 3rd, 2009 at 10:14 pm.
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 941
Reputation: MosaicFuneral is just really nice MosaicFuneral is just really nice MosaicFuneral is just really nice MosaicFuneral is just really nice MosaicFuneral is just really nice 
Solved Threads: 92
MosaicFuneral's Avatar
MosaicFuneral MosaicFuneral is offline Offline
Posting Shark

Re: Beginner and I would love help :) date validation

 
0
  #6
Mar 3rd, 2009
A few years late, blacklight?
"Jedenfalls bin ich überzeugt, daß der Alte nicht würfelt."
"I became very sensitive to what will happen to all this and all of us." -Two geniuses named Albert
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