943,548 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 5570
  • C++ RSS
Apr 12th, 2006
0

Beginner and I would love help :) date validation

Expand Post »
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 ???!!!


C++ Syntax (Toggle Plain Text)
  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. }
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
porterrj is offline Offline
11 posts
since Mar 2006
Apr 12th, 2006
0

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

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.
Reputation Points: 68
Solved Threads: 18
Posting Pro in Training
winbatch is offline Offline
466 posts
since Feb 2005
Apr 13th, 2006
0

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

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.


/
C++ Syntax (Toggle Plain Text)
  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. }
Reputation Points: 10
Solved Threads: 0
Newbie Poster
porterrj is offline Offline
11 posts
since Mar 2006
Apr 13th, 2006
0

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

put ur whole code so I can find the problem I did the same thing last week for my assignment.
Reputation Points: 10
Solved Threads: 1
Newbie Poster
jlouang is offline Offline
23 posts
since Feb 2006
Mar 3rd, 2009
1

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

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.
Reputation Points: 21
Solved Threads: 0
Newbie Poster
blacklight332 is offline Offline
5 posts
since Nov 2008
Mar 3rd, 2009
0

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

A few years late, blacklight?
Reputation Points: 888
Solved Threads: 114
Nearly a Posting Virtuoso
MosaicFuneral is offline Offline
1,270 posts
since Nov 2008
Jun 6th, 2011
0
Re: Beginner and I would love help :) date validation
  1. bool data(int dzien,int miesiac,int rok){
  2. int rokp[13] = { 31,29,31,30,31,30,31,31,30,31,30,31 };//tablica przestepna
  3. int rokz[13] = { 31,28,31,30,31,30,31,31,30,31,30,31 };//tablica zwykla
  4.  
  5. if(rok>=0){//rok
  6. if((rok%4 == 0 && rok%100 != 0) || rok%400 == 0){//przestepny
  7. if(miesiac<=12){//miesiace
  8. if(dzien>0 && rokp[miesiac-1]>=dzien){return true;}else{return false;}//dnie
  9. }
  10. }else{//zwykly
  11. if(miesiac<=12){//miesiace
  12. if(dzien>0 && rokz[miesiac-1]>=dzien){return true;}else{return false;}//dnie
  13. }
  14. }
  15. }//rok
  16. }
Reputation Points: 10
Solved Threads: 0
Newbie Poster
regonix is offline Offline
1 posts
since Jun 2011
Jun 6th, 2011
0
Re: Beginner and I would love help :) date validation
Please use English when posting on this formun. Another thing, you do realize this thread died over 2 years ago?
Reputation Points: 215
Solved Threads: 186
Veteran Poster
NathanOliver is offline Offline
1,066 posts
since Apr 2009

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: Video Converter Program
Next Thread in C++ Forum Timeline: GL list logic?





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


Follow us on Twitter


© 2011 DaniWeb® LLC