#include <cstdlib>
#include <iostream>
#include <iomanip>
using namespace std;
int year;
int firstday;
int month;
void PrintHeading ();
void DoAMonth(int,int,int&);
int numdays;
int janday;
int main(int argc, char *argv[])
{
cout << "Calendar Program. " << endl;
cout << "What year do you want a calendar for? " << endl;
cin >> year;
cout << "What day of the week does Jan.1 fall on? " << endl;
cout << "Enter 1 for Sunday, 2 for Monday, etc. " << endl;
cin >> firstday;
cout << endl << endl;
while (month >= 0 && month <= 12)
{
PrintHeading ();
DoAMonth (year, month, janday);
month++;
}
system("PAUSE");
return EXIT_SUCCESS;
}
void DoAMonth(int year, int month, int& day)
{
if(month == 1 ||month == 3 ||month == 5 ||month== 7 ||month == 8 ||month== 10 ||month == 12)
{
numdays = 31;
}
else if(month == 4 ||month == 6 ||month == 9 ||month == 11)
{
numdays = 30;
}
else if(month == 2)
{
numdays = 28;
if(year%4 == 0)
numdays = 29;
}
cout << endl;
for(int day = 0; day < janday; day++)
{
cout << " ";
}
for(int day = 1; day <= numdays; day++)
{
cout << setw(3) <<day;
if((day + janday)% 7 == 0)
{
cout << endl;
}
}
}
void PrintHeading ()
{
if(month == 1)
{
cout << setw(9)<< year << endl;
cout << endl;
cout << setw(14)<<"January" << endl;
cout << " S M T W Th F S" << endl;
cout << "~~~~~~~~~~~~~~~~~~~~~~~" << endl;
}
else if(month == 2)
{
cout << setw(15)<<"February" << endl;
cout << " S M T W Th F S" << endl;
cout << "~~~~~~~~~~~~~~~~~~~~~~~" << endl;
}
else if(month == 3)
{
cout << setw(13)<<"March" << endl;
cout << " S M T W Th F S" << endl;
cout << "~~~~~~~~~~~~~~~~~~~~~~~" << endl;
}
else if(month == 4)
{
cout << setw(13)<<"April" << endl;
cout << " S M T W Th F S" << endl;
cout << "~~~~~~~~~~~~~~~~~~~~~~~" << endl;
}
else if(month == 5)
{
cout << setw(12)<<"May" << endl;
cout << " S M T W Th F S" << endl;
cout << "~~~~~~~~~~~~~~~~~~~~~~~" << endl;
}
else if(month == 6)
{
cout << setw(13)<<"June" << endl;
cout << " S M T W Th F S" << endl;
cout << "~~~~~~~~~~~~~~~~~~~~~~~" << endl;
}
else if(month == 7)
{
cout << setw(13)<<"July" << endl;
cout << " S M T W Th F S" << endl;
cout << "~~~~~~~~~~~~~~~~~~~~~~~" << endl;
}
else if(month == 8)
{
cout << setw(14)<<"August" << endl;
cout << " S M T W Th F S" << endl;
cout << "~~~~~~~~~~~~~~~~~~~~~~~" << endl;
}
else if(month == 9)
{
cout << setw(16)<< "September" << endl;
cout << " S M T W Th F S" << endl;
cout << "~~~~~~~~~~~~~~~~~~~~~~~" << endl;
}
else if(month == 10)
{
cout << setw(14)<< "October" << endl;
cout << " S M T W Th F S" << endl;
cout << "~~~~~~~~~~~~~~~~~~~~~~~" << endl;
}
else if(month == 11)
{
cout << setw(15)<< "November" << endl;
cout << " S M T W Th F S" << endl;
cout << "~~~~~~~~~~~~~~~~~~~~~~~" << endl;
}
else if(month == 12)
{
cout << setw(15)<< "December" << endl;
cout << " S M T W Th F S" << endl;
cout << "~~~~~~~~~~~~~~~~~~~~~~~" << endl;
}
}
9 Replies - 628 Views - Last Post: 24 January 2011 - 06:41 PM
#1
How to start the next month with a new day! Calendar Program
Posted 23 January 2011 - 10:52 AM
Replies To: How to start the next month with a new day! Calendar Program
#2
Re: How to start the next month with a new day! Calendar Program
Posted 23 January 2011 - 10:58 AM
#3
Re: How to start the next month with a new day! Calendar Program
Posted 23 January 2011 - 11:11 AM
#4
Re: How to start the next month with a new day! Calendar Program
Posted 23 January 2011 - 08:40 PM
My code
#include <cstdlib>
#include <iostream>
#include <iomanip>
using namespace std;
int year;
int firstday;
int month;
void PrintHeading ();
void DoAMonth(int,int,int&);
int numdays;
int janday;
int main(int argc, char *argv[])
{
cout << "Calendar Program. " << endl;
cout << "What year do you want a calendar for? " << endl;
cin >> year;
cout << "What day of the week does Jan.1 fall on? " << endl;
cout << "Enter 1 for Sunday, 2 for Monday, etc. " << endl;
cin >> firstday;
cout << endl << endl;
while (month >= 0 && month <= 12)
{
PrintHeading ();
DoAMonth (year, month, janday);
month++;
}
system("PAUSE");
return EXIT_SUCCESS;
}
void DoAMonth(int year, int month, int& day)
{
if(month == 1 ||month == 3 ||month == 5 ||month== 7 ||month == 8 ||month== 10 ||month == 12)
{
numdays = 31;
}
else if(month == 4 ||month == 6 ||month == 9 ||month == 11)
{
numdays = 30;
}
else if(month == 2)
{
numdays = 28;
if(year%4 == 0)
numdays = 29;
}
cout << endl;
for(int day = 0; day < janday; day++)
{
cout << " ";
}
for(int day = 1; day <= numdays; day++)
{
cout << setw(3) <<day;
if((day + janday)% 7 == 0)
{
cout << endl;
}
}
return;
}
void PrintHeading ()
{
if(month == 1)
{
cout << setw(9)<< year << endl;
cout << endl;
cout << setw(14)<<"January" << endl;
cout << " S M T W Th F S" << endl;
cout << "~~~~~~~~~~~~~~~~~~~~~~~" << endl;
}
else if(month == 2)
{
cout << endl;
cout << setw(15)<<"February" << endl;
cout << " S M T W Th F S" << endl;
cout << "~~~~~~~~~~~~~~~~~~~~~~~" << endl;
}
else if(month == 3)
{
cout << endl;
cout << setw(13)<<"March" << endl;
cout << " S M T W Th F S" << endl;
cout << "~~~~~~~~~~~~~~~~~~~~~~~" << endl;
}
else if(month == 4)
{
cout << endl;
cout << setw(13)<<"April" << endl;
cout << " S M T W Th F S" << endl;
cout << "~~~~~~~~~~~~~~~~~~~~~~~" << endl;
}
else if(month == 5)
{
cout << endl;
cout << setw(12)<<"May" << endl;
cout << " S M T W Th F S" << endl;
cout << "~~~~~~~~~~~~~~~~~~~~~~~" << endl;
}
else if(month == 6)
{
cout << endl;
cout << setw(13)<<"June" << endl;
cout << " S M T W Th F S" << endl;
cout << "~~~~~~~~~~~~~~~~~~~~~~~" << endl;
}
else if(month == 7)
{
cout << endl;
cout << setw(13)<<"July" << endl;
cout << " S M T W Th F S" << endl;
cout << "~~~~~~~~~~~~~~~~~~~~~~~" << endl;
}
else if(month == 8)
{
cout << endl;
cout << setw(14)<<"August" << endl;
cout << " S M T W Th F S" << endl;
cout << "~~~~~~~~~~~~~~~~~~~~~~~" << endl;
}
else if(month == 9)
{
cout << endl;
cout << setw(16)<< "September" << endl;
cout << " S M T W Th F S" << endl;
cout << "~~~~~~~~~~~~~~~~~~~~~~~" << endl;
}
else if(month == 10)
{
cout << endl;
cout << setw(14)<< "October" << endl;
cout << " S M T W Th F S" << endl;
cout << "~~~~~~~~~~~~~~~~~~~~~~~" << endl;
}
else if(month == 11)
{
cout << endl;
cout << setw(15)<< "November" << endl;
cout << " S M T W Th F S" << endl;
cout << "~~~~~~~~~~~~~~~~~~~~~~~" << endl;
}
else if(month == 12)
{
cout << endl;
cout << setw(15)<< "December" << endl;
cout << " S M T W Th F S" << endl;
cout << "~~~~~~~~~~~~~~~~~~~~~~~" << endl;
}
}
#5
Re: How to start the next month with a new day! Calendar Program
Posted 23 January 2011 - 09:12 PM
Jan 1st = Sun = 1
Feb 1st = Wed = 4
Mar 1st = Wed = 4
etc.
if year 1906
You are told Jan 1st = Mon (2), (difference Mon - Sun = 2 - 1 = 1)
Feb 1st = Thu (added 1 day)
Mar 1st = Thu (added 1 day)
If year is leap year you will need to add 1 for that (Mar and so on).
Wondering if this will work for a difference of more than one leap year.
This post has been edited by #define: 23 January 2011 - 09:14 PM
#6
Re: How to start the next month with a new day! Calendar Program
Posted 23 January 2011 - 09:14 PM
Take a look at this to see how they are calculated: http://visualbasic.a...ykleapyr1-1.gif
There are two more checks you need to run to see if it is, in fact, a leap year.
This may be a solution to your starting day problem:
1. Keep track of the number of days skipped at the beginning of January (i.e. If January 1st is a Tuesday, you skipped 2 days: Sunday and Monday)
2. Mod the number of days in the current month by 7, then add the number of days skipped from the previous month to this to get the offset for the next month. (31 (for January) % 7 = 3; 3 + 2 = 5; You need to skip 5 days at the beginning of Feb, so Feb 1st lands on a Friday. If you end up with a total greater than 7, simply subtract 7)
3. Update the daysSkipped variable with the number of days skipped at the beginning of Feb and repeat for March, April, etc.
I know this sounds confusing, but grab a calendar and think through it.
Hope this helps
#7
Re: How to start the next month with a new day! Calendar Program
Posted 23 January 2011 - 10:10 PM
This post has been edited by JackOfAllTrades: 24 January 2011 - 05:19 AM
Reason for edit:: Removed unnecessary quote
#8
Re: How to start the next month with a new day! Calendar Program
Posted 24 January 2011 - 03:45 AM
#9
Re: How to start the next month with a new day! Calendar Program
Posted 24 January 2011 - 06:29 PM
Ex.S M T W Th F S
1 2 3 4 5 6 7
8 9 10 11 12 13 14...
The days other than the first week are perfectly fine
#include <cstdlib>
#include <iostream>
#include <iomanip>
using namespace std;
int year;
int firstday;
int month;
void PrintHeading ();
void DoAMonth(int,int,int&);
int numdays;
int janday;
int main(int argc, char *argv[])
{
cout << "Calendar Program. " << endl;
cout << "What year do you want a calendar for? " << endl;
cin >> year;
cout << "What day of the week does Jan.1 fall on? " << endl;
cout << "Enter 1 for Sunday, 2 for Monday, etc. " << endl;
cin >> firstday;
cout << endl << endl;
while (month >= 0 && month <= 12)
{
PrintHeading ();
DoAMonth (year, month, janday);
month++;
}
system("PAUSE");
return EXIT_SUCCESS;
}
void DoAMonth(int year, int month, int& day)
{
if(month == 1 ||month == 3 ||month == 5 ||month== 7 ||month == 8 ||month== 10 ||month == 12)
{
numdays = 31;
}
else if(month == 4 ||month == 6 ||month == 9 ||month == 11)
{
numdays = 30;
}
else if(month == 2)
{
numdays = 28;
if(year%4 == 0)
numdays = 29;
}
cout << endl;
for(int day = 0; day < janday; day++)
{
cout << " ";
}
for(int day = 1; day <= numdays; day++)
{
cout << setw(3) <<day;
if((day + janday)% 7 == 0)
{
cout << endl;
}
}
janday=(janday+(numdays))%7;
if(janday == 0)
janday = 7;
}
void PrintHeading ()
{
if(month == 1)
{
cout << setw(9)<< year << endl;
cout << endl;
cout << setw(14)<<"January" << endl;
cout << " S M T W Th F S" << endl;
cout << "~~~~~~~~~~~~~~~~~~~~~~~" << endl;
}
else if(month == 2)
{
cout << endl;
cout << setw(15)<<"February" << endl;
cout << " S M T W Th F S" << endl;
cout << "~~~~~~~~~~~~~~~~~~~~~~~" << endl;
}
else if(month == 3)
{
cout << endl;
cout << setw(13)<<"March" << endl;
cout << " S M T W Th F S" << endl;
cout << "~~~~~~~~~~~~~~~~~~~~~~~" << endl;
}
else if(month == 4)
{
cout << endl;
cout << setw(13)<<"April" << endl;
cout << " S M T W Th F S" << endl;
cout << "~~~~~~~~~~~~~~~~~~~~~~~" << endl;
}
else if(month == 5)
{
cout << endl;
cout << setw(12)<<"May" << endl;
cout << " S M T W Th F S" << endl;
cout << "~~~~~~~~~~~~~~~~~~~~~~~" << endl;
}
else if(month == 6)
{
cout << endl;
cout << setw(13)<<"June" << endl;
cout << " S M T W Th F S" << endl;
cout << "~~~~~~~~~~~~~~~~~~~~~~~" << endl;
}
else if(month == 7)
{
cout << endl;
cout << setw(13)<<"July" << endl;
cout << " S M T W Th F S" << endl;
cout << "~~~~~~~~~~~~~~~~~~~~~~~" << endl;
}
else if(month == 8)
{
cout << endl;
cout << setw(14)<<"August" << endl;
cout << " S M T W Th F S" << endl;
cout << "~~~~~~~~~~~~~~~~~~~~~~~" << endl;
}
else if(month == 9)
{
cout << endl;
cout << setw(16)<< "September" << endl;
cout << " S M T W Th F S" << endl;
cout << "~~~~~~~~~~~~~~~~~~~~~~~" << endl;
}
else if(month == 10)
{
cout << endl;
cout << setw(14)<< "October" << endl;
cout << " S M T W Th F S" << endl;
cout << "~~~~~~~~~~~~~~~~~~~~~~~" << endl;
}
else if(month == 11)
{
cout << endl;
cout << setw(15)<< "November" << endl;
cout << " S M T W Th F S" << endl;
cout << "~~~~~~~~~~~~~~~~~~~~~~~" << endl;
}
else if(month == 12)
{
cout << endl;
cout << setw(15)<< "December" << endl;
cout << " S M T W Th F S" << endl;
cout << "~~~~~~~~~~~~~~~~~~~~~~~" << endl;
}
}
#10
Re: How to start the next month with a new day! Calendar Program
Posted 24 January 2011 - 06:41 PM
|
|

New Topic/Question
Reply




MultiQuote










|