Hi All,
I need to write a code where i get the first date of every month and display those values.I would like to know how to proceed in order to achieve that?
You mean you want to know if it's Monday or etc? Then, assuming that Monday is 0, Tuesday is 1, ..., the first day of a month is the first day of the previous month, and the number of days in that month and do modulo (%) 7.
Hello Hamsterman,
Thanks for your response.If i enter month 2/1/2012 then i need to get 3/1/2012 .....12/1/2012,1/1/2013.As u mentioned that i should write number of days in a month is it the right way to write ((firstday+ 30)%7).Do i need to write it for all the months?
void E::dates()
{
int month;
int year;
int day;
E et;
Date* dt;
d=new Date;
month=dt->getMonth(); //Get the month which is set(ex:1)
year=dt->getYear(); //Get the year which is set(ex:2010)
day=dt->getDay(); //Get the year which is set(ex:1)
bool leapyear = false; //Check whether the year is a leap year
for(month;month<=6;month++) //Need to push the elements of first 6 months.
{
dt->setDate(day, month,year);
dts.push_back(dt); //dts is the vector of Date pointers.
}
}
Problem with this code:After the first element is inserted with value 1/1/2011 then when i try to insert the second element during debugging i am able to see the value of month as 2 but in the result even the first value in the vector also set to 1/2/2011.So i am getting the below output for 6 months:
1/6/2011
1/6/2011
Why those values are being reset?Do guide me.
You have a vector of pointers. You change the object dt points to and push dt. Thus all pointers point to the same location as dt. The quickest way to solve this is to remove the * from declaration of dts (making it an vector of Date objects rather than pointers) and add it to line dts.push_back(*dt);. There is no need to use pointers anywhere here though at all.
Hello Hamsterman,
Thanks for your help.It worked.I have one more thing to get clarified.While displaying i would like to have this format Jan 1 Wed 2011 etc.Basically i wanted to display along with the day of week.This is the below code but it is not working.By default it is same for all the values Jan 1 Wed 2011,Jan 1 Wed 2011 etc.
vector <Date>::const_iterator iter; //vector of date objects
for(iter=dts.begin();iter!=dts.end();++iter) //Traversing through the vector
{
Date dat;
printMonth(dat.getMonth(),dayofweek(1,dat.getMonth(),dat.getYear()),dat.getYear());
}
//Finds what day of the week the year starts on
int E::dayofweek(const int day, const int month, const int year) const
{
int a,y,m;
a=(14-month)/12;
y=year-a;
m= month+12*a-2;
return (day+y+y/4-y/100 + y/400 + (31*m/12))%7;
}
void E::printMonth(const int month, const int startday, const bool leap) const
{
string Months[13] = {" ", "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"};
int DaysinMonth[13] = {0,31,28,31,30,31,30,31,31,30,31,30,31};
//Checks to see if it is a leap year and changes the days in February accordingly.
if(leap)
DaysinMonth[2] = 29;
cout<<setw(10)<<Months[month]<<setw(15)<<Days[startday]<<setw(15)<<dt->getDay()<<setw(15)<<dt->getYear();
cout << endl;
}
Thanks in advance.
Hello Hamsterman,
Thanks a lot.It helped me a lot.One more thing i wanted to try for any given date it should display 6 months from that date.Ex:1/2/2011 then it should display till July 1/7/2011.
I tried with both for loops:
for(month;month<=6;month++) //Need to push the elements of first 6 months.
{
dt->setDate(day, month,year);
dts.push_back(dt); //dts is the vector of Date pointers.
}
Output will be from Feb to June since i wrote the condition as month<=6.
for(month=month+6;month<=12;month++) //Need to push the elements of first 6 months.
{
dt->setDate(day, month,year);
dts.push_back(dt); //dts is the vector of Date pointers.
}
Output is starting from August since i wrote the start condition as month+6.