The problem is in dayOfWeek () I think. It may be overloaded or something.
#ifndef DATE_H
#define DATE_H
class Date
{
public:
Date( int = 1, int = 1, int = 1900 ); // default constructor
void print() const; // print date in month/day/year format
int dayOfWeek (); // This one returns the day of the week from the current m/d/y
// ~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;
}; // end class Date
#endif
// Fig. 11.11: Date.cpp
// Date class member-function definitions.
#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::dayOfWeek ()
{
/* We use the formula below where floor() denotes the integer floor function,
W = (k + floor(2.6m - 0.2) - 2C + Y + floor(Y/4) + floor(C/4)) mod 7
k is day (1 to 31)
m is month (1 = March, ..., 10 = December, 11 = Jan, 12 = Feb) Treat Jan & Feb as months of the preceding year
C is century (1987 has C = 19)
Y is year (1987 has Y = 87 except Y = 86 for Jan & Feb)
W is week day (0 = Sunday, ..., 6 = Saturday)
from the website
http://www.mcs.csueastbay.edu/~malek/Mathlinks/Weekdays.html
*/
int k, m, C, Y, W;
m = month - 2; // Fix m to start from 1 for march
if (m <=0)
m += 12;
k = day; //
C = year / 100;
Y = year % 100;
if (m >= 11)
Y = Y -1; // Adjust for January and Febrarury
W = (k + (int)(2.6*m - 0.2) - 2*C + Y + Y/4 + C/4) % 7 ;
return W;
}
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
#include <iostream>
//#include "Date.h"
using namespace std;
int main () {
char *DOW [8] = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday",
"Saturday"};
Date d(7,1,2010);
d.print();
int x = d.dayOfWeek();
cout << "Day of week for 11/2/2009 is " << DOW[x] << endl;
}