Unfortunity I cannot edit my last post without filling out loads of paper work
So I am posting this again, with added feature that should solved this situation.
Is this a solution to your problem? it may need more testing by you.
The assignment:
check to be sure that day, month, and year are valid, at all times else set default values if not.
the Problem:
you need to have everything available at the same time, day,month,year, so you can check for the leap year, if true then check if month is feb then check that for the day. If not leap year act on it accordingly as well.
you cannot by any means check to see if feb days limits are being valid or not without knowing the year first. so on and so forth. So you have to develop a means to get all of that data at once to use it to check everything in a logical order then set the data afterwords.
the header "declaring/defining" a Class that is using a private function to do this.
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
The code to it.
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)
{
using std::cout;
using std::endl;
if ( dd <= 0 || dd > 31)
{
std::cout << "invalid day" << '\n';
}
else
{
day = dd;
}
}
int TheDate::getDay()
{
return day;
}
void TheDate::setMonth(int mm)
{
using std::cout;
using std::endl;
if ( mm <= 0 || mm > 12)
{
std::cout << "invalid Month" << '\n';
}
else
{
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.
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';
// first find out if year is leap year or not.
leap = get_is_leap_year(yy);
std::cout << leap << " is leap " << '\n';
std::cout << dd << " "<< mm << " " << yy << std::endl;
//if true check for month is feb.
// if it is a leap year and feb check day
if (leap == 1 && mm == 2)
{ std::cout << "in checking for feb" << '\n';
if (dd <= 0 || dd > 29)
{
dd = 1;
}
else
{
dd = dd;
}
setDay(dd);
}
else
{ std::cout << " in switch" << '\n';
//check for which months have same ending days
switch(mm)
{
case 2: // feb
if (dd <= 0 || dd > 28)
{
dd = 1;
}
else
{
dd = dd;
}
setDay(dd);
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)
{
dd = 1;
}
else
{
dd = dd;
}
std::cout << "switch for setDay" << dd << '\n';
setDay(dd);
break;
//look for 30 days
case 4: // april
case 6: //june
case 9: //sept
if ( dd <= 0 || dd > 30)
{
dd = 1;
}
else
{
dd = dd;
}
setDay(dd);
break;
default:
break;
} // end switch
} // end else if
if (yy <= 2000)
{
yy = 2001;
setYear(yy);
}
else
{
setYear(yy);
}
if (mm <= 0 || mm > 12)
{
mm = 1;
setMonth(mm);
}
else
{
setMonth(mm);
}
}
correct me if I am wrong (someone that knows more about switches then I )
the switch works off an int, the value within it, if the case matches it number it gets "sucked" into the case, if no break it then drops through until it finds something to do, when it does then do it, break out of the switch, done.
the order of the number values is not as important as the value themselves. therefore one can mix of the order of the numbers.
main implementation.
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);
*/
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;
}
the results that I am sure need be tested more, but I am not a want a be tester, hehe :p
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
31 2 1994
in validate dates
1994 is not a leap year.
0 is leap
31 2 1994
in switch
(here are results from main)
Monday
February
2001 is not a leap year.
day1 month 2 year 2001
You should already know why this works having gotten to Classes in your class on C++. The teacher should have already covered why the private function gets called on creation of the Class.