this is what I'd do.
You need to test and understand how arrays work, and how Switch uses the value within an int var. Understand private functions. ( which I do not fully understand yet), I just knew that no one needs to know or use this function other then the class itself, so it is private, then use it when creating the Class and filling the values first. simple logic.
the logic next is that you need all of the data to use it against each other to check if leap year, then it yes is it feb then if yes what is the day . fix if necessary.
move on, this all needs to be done before setting anything, day, month, year.
I hope this helps.
class
Code:
#ifndef DATE_H
#define DATE_H
class TheDate
{
private:
int year;
int month;
int day;
void validate_dates(int dd, int mm, int yy);
public:
TheDate(int dd = 0, int mm = 0, int yy = 0000)
{
validate_dates(dd,mm,yy);
}
void setDay(int dd);
int getDay();
void setMonth(int mm);
int getMonth();
void setYear(int yy);
int getYear();
void get_Day_of_week(int day);
int get_is_leap_year(int year);
void get_what_month(int month);
};
#endif
cpp
Code:
#include <iostream>
#include "date.h"
const char* day_of_week[] = {
"Monday", "Tuesday", "Wensday", "Thursday", "Firday", "Saturday", "Sunday"
};
const char* The_Month[] = {
"January", "February", "March", "April", "May", "June", "July",
"August", "September", "October", "November", "December"
};
void TheDate::setDay(int dd)
{
day = dd;
}
int TheDate::getDay()
{
return day;
}
void TheDate::setMonth(int mm)
{
month = mm;
}
int TheDate::getMonth()
{
return TheDate::month;
}
void TheDate::setYear(int yy)
{
TheDate::year = yy;
}
int TheDate::getYear()
{
return TheDate::year;
}
void TheDate::get_Day_of_week(int day)
{
using std::cout;
using std::endl;
std::cout << day_of_week[day - 1] << std::endl;
}
int TheDate::get_is_leap_year(int year)
{
using std::cout;
using std::endl;
//set return values for bool.
if (year % 4 == 0)
{
if (year % 100 == 0)
{
if (year % 400 == 0)
{
std::cout << year << " is a leap year." << '\n';
return 1;
}
else
{
std::cout << year << " is not a leap year." << '\n';
return 0;
}
}
else
{
std::cout << year << " is a leap year." << '\n';
return 1;
}
}
else
{
std::cout << year << " is not a leap year." << '\n';
return 0;
}
// because it has to return something.
// well it should not even reach this actually.
return year;
}
void TheDate::get_what_month(int month)
{
using std::cout;
using std::endl;
std::cout << The_Month[month - 1] << endl;
}
void TheDate::validate_dates(int dd, int mm, int yy)
{
int leap;
using std::cout;
using std::endl;
std::cout << "in validate dates" << '\n';
// make sure proper year other wise it all off anyways, or just cuz. ;)
if (yy <= 2000)
{
setYear(2001);
}
else
{
setYear(yy);
}
// Now find out if year is leap year or not.
// ensures proper year is being checked.
// by passing in getYear.
leap = get_is_leap_year(getYear());
//if true check for month is feb.
// if it is a leap year and feb check day
if (leap == 1 && mm == 2)
{
if (dd <= 0 || dd > 29)
{
setYear(2001);
}
else
{
setDay(dd);
}
}
else
{
std::cout << " month is " << mm << std::endl;
//check for which months have same amount of ending days grouping case.
switch(mm)
{
case 2: // feb
if (dd <= 0 || dd > 28)
{
setDay(1);
setMonth(mm);
}
else
{
setDay(dd);
setMonth(mm);
}
break;
//31 days
case 1: //jan
case 3: //march
case 5: //may
case 7: //july
case 8: //augsut
case 10: //Oct
case 12: //dec
if ( dd <= 0 || dd > 31)
{
setDay(1);
setMonth(mm);
std::cout << " in 31 days months " << getDay() <<std::endl;
}
else
{
setDay(dd);
setMonth(mm);
std::cout << " dd = dd in 31 days months " << getDay() <<std::endl;
}
break;
//look for 30 days
case 4: // april
case 6: //june
case 9: //sept
if ( dd <= 0 || dd > 30)
{
setDay(1);
setMonth(mm);
}
else
{
setDay(dd);
setMonth(mm);
}
break;
default:
// if it gets here then the month has to then fall out of specs
// then day too is also invalid by default,
//so just set them both to a default day, month.
setMonth(1);
setDay(1);
break;
} // end switch
} // end else if
}// end function
main
Code:
#include <iostream>
#include "date.h"
int main()
{
using namespace std;
int d,m,y;
std::cout << "enter day, month, year in number form" << std::endl;
std::cin >> d >> m >> y ;
TheDate trying(d,m,y);
/*
trying.setDay(d);
trying.setMonth(m);
trying.setYear(y);
*/
std::cout << '\n' << " in main " << '\n';
trying.get_Day_of_week(trying.getDay());
trying.get_what_month(trying.getMonth());
trying.get_is_leap_year(trying.getYear());
std::cout << '\n' << "day " << trying.getDay() << " month " << trying.getMonth() << " year " << trying.getYear() << "\n";
return 0;
}
Results:
Code:
userx@/media/data/C-Projects/VSC/C++/class-date <> g++ -Wall date.cpp main.cpp
userx@/media/data/C-Projects/VSC/C++/class-date <> ./a.out
enter day, month, year in number form
33 23 1703
in validate dates
2001 is not a leap year.
in main
Monday
January
2001 is not a leap year.
day 1 month 1 year 2001
note: if day is greater then 7 it seg faults because the days array it is not set up to evaluate and change the number day of month to properly give day of week depending on year and month. if ya know what I mean. but I do not think your doing the show what day of week it is, if yes, then just fix the code to make it always not go over 7 on day of week. hehe