| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Display Modes |
|
#1
|
|||
|
|||
|
Validating Dates in C++
I am currently writing a simple numerology program. I am having problems validating the dates entered by a user. The major source of the problem is how to make sure the date the user entered is actually within the month (for example: 4/31 can’t be entered). This is what I currently have for checking dates:
Code:
int month, day, year;
do
{
// In here I have the program asking the user for the date. I also have an if/else
statement telling the user which part of the date is wrong.
}
while( month<1 || month>12 || day<1 || day>31 || year<1800 || year>3500);
Any hints would be great. If you give some type of example of how I could do this I ask that the example be basic c++. I can’t use functions on this program. I am pretty limited on what I can and cannot use. |
|
#2
|
|||
|
|||
|
lol
Can you use strings?? use it then use the cout << month << "/" << day << endl; |
|
#3
|
|||
|
|||
|
With the exception of the freak month (Feb) that can be handled with a special case, we know that every second month has
the same amount of days up until aug, at which point things are reversed. Do you know the modulus operator "%" (remainder of integer division), we can use it like so: int month = the number of the month (with Jan being 1, feb 2 ...) int day = day to check; if ( month <= 7) { //less than or equal to july if ((month % 2) == 0) { //every second month from feb to june if (month == 2) { //deal with feb if (day > 28) //or 29, or however you choose to deal with this one DO SOMETHING HERE } else if (day > 30) DO SOMETHING HERE } else { //every second month jan to july (31 days) if (day > 31) DO SOMETHING HERE } } else { //greater than july if ((month % 2) == 0) { //every second month from aug to dec (31 days) if (day > 31) DO SOMETHING HERE } else { //every second month sept to nov (30 days) if (day > 30) DO SOMETHING HERE } } Hope this helps, Kernel Mustard |
![]() |
| Viewing: Dev Articles Community Forums > Programming > C/C++ Help > Validating Dates in C++ |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|