Hi,
So for my C++ class I am required to create a program that will "Write a program to generate a calendar for a year. The program should accept the year and the day of the week for january 1 of that year (0=Sunday, 1=Monday, etc.)" (problem statement) and I am completely stuck. Can anyone help me? I've posted what I have so far below:
#include <iostream>
#include <iomanip>
usingnamespace std;
bool isLeapYear(int year);
int daysInMonth(int month, bool lpYear);
int printCalendar(int month);
int printDay(int dow);
string daysofweek="Su Mo Tu We Th Fr Sa";
int month=1; // What month is it
int maxdaysinmonth=0; // Max days to print for month
int endofweek=6; // end of week is Sa
int startingspaces=0; // leading spaces
int daycount=1; // Current day to print
int main ()
{
int dow, year;
cout << "What year is it?\n";
cin >> year;
cout << "What day of the week does January start on? Enter 0 for Sunday, 1 for Monday, etc.\n";
cin >> dow;
// Print Year
cout << "\t" << year << endl<< endl;
// Print Month
if (month==1)
{
cout << " January" << endl;
maxdaysinmonth = 31;
}
cout << daysofweek << endl;
// Determine leading spaces
if (dow==0)
{startingspaces=1;}
elseif (dow==1)
{startingspaces=4;}
elseif (dow==2)
{startingspaces=7;}
elseif (dow==3)
{startingspaces=10;}
elseif (dow==4)
{startingspaces=13;}
elseif (dow==5)
{startingspaces=16;}
elseif (dow==6)
{startingspaces=19;}
for (int i=1; i <=startingspaces;i++)
{cout << " ";}
while (daycount < 10)
{
if (daycount < 9)
{
cout << daycount << " ";
}
else
{
cout << daycount<< " ";
}
daycount++;
if (dow==6)
{
cout << endl << " ";
dow=0;
}
else
{dow++;}
}
while (daycount <=maxdaysinmonth)
{
cout << daycount << " ";
daycount++;
if (dow==6)
{
cout << endl;
dow=0;
}
else
{dow++;}
}
month++;
cout << endl;
cout << "Next Month = " << month << endl;
cout << "Starting day of week for next month: " << dow << endl;
return 0;
}
bool isLeapYear(int year)
{
return ((year%400==0) || (year%4==0 && year%100!=0));
}
Output:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
What year is it?
2013
What day of the week does January start on? Enter 0 for Sunday, 1 for Monday, etc.
2
2013
January
Su Mo Tu We Th Fr Sa
1 2 3 4 5
6 7 8 9 10 11 12
13 14 15 16 17 18 19
20 21 22 23 24 25 26
27 28 29 30 31
Next Month = 2
Starting day of week for next month: 5