ValidateBirthday

Hi,
I want make validate to birthday have to month 1-12
and the day depends on the days of the month 1-30 or 1-31 or feb 1-29
thank you
Make an array with the number of days in each month, and check to see if the given birthday fits for the entry in that month.
assuming these are unsigned types,
if( month && (month%13 == month) ) //note the +1, so that the mod allows 0-12, right?

you can use a similar check for whatever else you want to test. You can check the months in bulk groups, is it feb, else is it one of the 30s, else its a 31

you can also probably try to put the value into one of the date formatted objects and see if it is valid, I can't remember but its probable that you can catch an error that way.
Last edited on
From Deitel's book "C++ for Programmers":
1
2
3
4
5
6
7
8
9
10
11
12
13
14
class Date 
{
public:
   Date( int = 1, int = 1, int = 1900 ); // default constructor
   void print() const; // print date in month/day/year format
   ~Date(); // provided to confirm destruction order
private:
   int month; // 1-12 (January-December)
   int day; // 1-31 based on month
   int year; // any year

   // utility function to check if day is proper for month and year
   int checkDay( int ) const; 
}; 


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#include <iostream>
using std::cout;
using std::endl;

#include "Date.h" // include Date class definition

// constructor confirms proper value for month; calls
// utility function checkDay to confirm proper value for day
Date::Date( int mn, int dy, int yr )
{
   if ( mn > 0 && mn <= 12 ) // validate the month
      month = mn;
   else 
   {                     
      month = 1; // invalid month set to 1
      cout << "Invalid month (" << mn << ") set to 1.\n";
   } // end else

   year = yr; // could validate yr
   day = checkDay( dy ); // validate the day

   // output Date object to show when its constructor is called
   cout << "Date object constructor for date ";
   print();                   
   cout << endl;
} // end Date constructor

// print Date object in form month/day/year
void Date::print() const
{
   cout << month << '/' << day << '/' << year; 
} // end function print

// output Date object to show when its destructor is called
Date::~Date()
{ 
   cout << "Date object destructor for date ";
   print();
   cout << endl;
} // end ~Date destructor

// utility function to confirm proper day value based on 
// month and year; handles leap years, too
int Date::checkDay( int testDay ) const
{
   static const int daysPerMonth[ 13 ] = 
      { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };

   // determine whether testDay is valid for specified month
   if ( testDay > 0 && testDay <= daysPerMonth[ month ] )
      return testDay;

   // February 29 check for leap year 
   if ( month == 2 && testDay == 29 && ( year % 400 == 0 || 
      ( year % 4 == 0 && year % 100 != 0 ) ) )
      return testDay;

   cout << "Invalid day (" << testDay << ") set to 1.\n";
   return 1; // leave object in consistent state if bad value
} // end function checkDay 
or...

<chrono>

month m(value);

if(month.ok()) //this is fairly new, and awesome.



this is what I was saying above, but I forgot where to find the function and had to look for a bit.

you will still have to work a little on day.ok() as it just checks to 31, and isn't aware of what month it is. looks like what he needs is month_day.ok () ? I think?
Last edited on
Another option:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
bool is_leapyear(int year)
{
  return year % 400 == 0 || (year % 4 == 0 && year % 100 != 0);
}

bool valid_date(int day, int month, int year)
{
  int days[13] = { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
  
  if (is_leapyear(year))
    days[2] = 29;

  if (month < 1 || month > 12)
    return false;

  return (day >= 1 && day <= days[month]);
}
Topic archived. No new replies allowed.