| | |
Beginner and I would love help :) date validation
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Mar 2006
Posts: 11
Reputation:
Solved Threads: 0
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 ???!!!
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)
bool GoodDate(int month,int day,int year) { /*** Bad Month ***/ if(month < 1 || month > 12) return false; /*** January ***/ else if(month == 1) { if(day >=1 && day<= 31) return true; else return false; } /*** Febuary ***/ else if(month == 2) // divisible by 4 and either divisible by 400 or not divisible by 100 { /*** if the year is a leap year... ***/ if(year % 4 == 0 && (year % 400 == 0 || year % 100 != 0)) { if(day >= 1 && day <= 29) return true; else return false; } /*** if the year is not a leap year... ***/ else { if(day >=1 && day <= 28) return true; else return false; } } /*** March ***/ else if(month == 3) { if(day >=1 && day<= 31) return true; else return false; } /*** April ***/ else if(month == 4) { if(day >=1 && day<= 30) return true; else return false; } /*** May ***/ else if(month == 5) { if(day >=1 && day<= 31) return true; else return false; } /*** June ***/ else if(month == 6) { if(day >=1 && day<= 30) return true; else return false; } /*** July ***/ else if(month == 7) { if(day >=1 && day<= 31) return true; else return false; } /*** August ***/ else if(month == 8) { if(day >=1 && day<= 31) return true; else return false; } /*** September ***/ else if(month == 9) { if(day >=1 && day<= 30) return true; else return false; } /*** October ***/ else if(month == 10) { if(day >=1 && day<= 31) return true; else return false; } /*** November ***/ else if(month == 11) { if(day >=1 && day<= 30) return true; else return false; } /*** December ***/ else if(month == 12) { if(day >=1 && day<= 31) return true; else return false; } else { cout<<"Error\n"; exit(0); } return false; }
•
•
Join Date: Mar 2006
Posts: 11
Reputation:
Solved Threads: 0
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.
/
Any help?? I didn't put it in there, because I can't figure out where it goes logically.
/
C++ Syntax (Toggle Plain Text)
************************Includes**************************************/ # include<iostream> # include<iomanip> using namespace std; /************************Function call*********************************/ int isLeapYear(int year); //function int dateIsValid (int day, int month, int year); //function /***********************Main******************************************/ int main() { int value1, value2, value3; char again; do { cout << "Enter the day, month, and year. \n"; cin >> value1 >> value2 >> value3; dateIsValid(value1, value2, value3); //call function cout << value1<<" "<< value2 <<" "<< value3<<endl; cout << " Do you want to enter another date? (Y/N)"; cin >>again; }while (again =='Y' || again =='y'); return 0; } /*************************FUNCTION*********************************/ int dateIsValid(int day, int month, int year) { bool valid; int monthLength[13] = { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }; if ( isLeapYear(year) ) monthLength[2] = 29; // 29 days in February in a leap year if ( month < 1 || month > 12 ) valid = false; else if ( day < 1 || day > monthLength[month] ) valid = false; return ( valid ); } /****************************FUNCTION*******************************************/ int isLeapYear(int year) { bool result; if ( (year%4) != 0 ) // or: if ( year%4 ) result = false; // means: if year is not divisible by 4 else if ( (year%400) == 0 ) // or: if ( !(year%400) ) result = true; // means: if year is divisible by 400 else if ( (year%100) == 0 ) // or: if ( !(year%100) ) result = false; // means: if year is divisible by 100 else // (but not by 400, since that case result = true; // considered already) return ( result ); }
•
•
Join Date: Nov 2008
Posts: 5
Reputation:
Solved Threads: 0
These examples are overly complicated. I found a simple one that totally handles leap years.
from: c++ checkdate date validation
from: c++ checkdate date validation
c Syntax (Toggle Plain Text)
bool checkdate(int m, int d, int y) { //gregorian dates started in 1582 if (! (1582<= y ) )//comment these 2 lines out if it bothers you return false; if (! (1<= m && m<=12) ) return false; if (! (1<= d && d<=31) ) return false; if ( (d==31) && (m==2 || m==4 || m==6 || m==9 || m==11) ) return false; if ( (d==30) && (m==2) ) return false; if ( (m==2) && (d==29) && (y%4!=0) ) return false; if ( (m==2) && (d==29) && (y%400==0) ) return true; if ( (m==2) && (d==29) && (y%100==0) ) return false; if ( (m==2) && (d==29) && (y%4==0) ) return true; return true; }
Last edited by blacklight332; Mar 3rd, 2009 at 10:14 pm.
![]() |
Similar Threads
- Microsoft Access date validation Need a solution (Computer Science)
- need date validation help (MS Access and FileMaker Pro)
- Microsoft Access date validation Need a solution (Database Design)
- Microsoft Access date validation (Computer Science)
Other Threads in the C++ Forum
- Previous Thread: unable to add char array of numbers to another char array
- Next Thread: C++ / MFC Date Validation
| Thread Tools | Search this Thread |
api array based beginner bitmap c++ c/c++ calculator char class classes code coding compile compiler console conversion count database delete deploy desktop developer directshow dll download dynamic email encryption error file forms fstream function functions game givemetehcodez google graph gui homeworkhelp homeworkhelper iamthwee ifstream input int integer java lib linkedlist linker linux list loop looping loops map math matrix memory multiple news node number output parameter pointer problem program programming project python random read recursion recursive return sorting string strings struct temperature template templates test text text-file tree unix url variable vector video visualstudio win32 windows winsock word wordfrequency wxwidgets





