I need help writing a c++ program that creates a calendar. The user needs to put
ID: 667901 • Letter: I
Question
I need help writing a c++ program that creates a calendar.
The user needs to put in a year between 1500 and 8500 only.
The user also needs to put in the day of the week they'd like to start the year. This needs to be put in as the actual strings and not numbers. So for instance: Sunday, Monday, Tuesday, Wednesday....Saturday.
I have to account for leap years. Leap years occur every 4 years and every 400 years. Every 100th year is NOT a leap year though.
I need to use the tab character to asign data into columns.
Also, I need to use #include <iostream> and void functions if possible?
Any help would be appreciated. I've been stuck as to how to create a calendar with the string input of a week day and not a number...if that makes sense?
Explanation / Answer
// calendar.cpp
// For lack of time i can't input the day of the week i hope help you with this code.
#include <iomanip>
#include <iostream>
#include <string>
using namespace std;
using std::cout;
int getYear();
int computeOffset(int year, int month);
int numDaysYear(int year);
int numDaysMonth(int year, int month);
bool isLeapYear(int year);
void display(int year, int month, int offset);
/***********************************************************************
* Gets the year between 1500 and 8500 only
**********************************************************************/
int getYear()
{
int year = 0;
cout << "Enter year: ";
cin >> year;
while ( year > 8500 || year < 1500)
{
cout << "Year must be between 1500 and 8500. "
<< "Enter year: ";
cin >> year;
}
return year;
}
/***********************************************************************
* Computes the offset.
**********************************************************************/
int computeOffset(int year, int month)
{
int offset = 0;
int count = year - 1753;
for (int iYear = 0; iYear < count; iYear++)
{
offset = (offset + 365 + isLeapYear(year)) % 7;
}
for (int iMonth = 1; iMonth < month; iMonth++)
{
offset = (offset + numDaysMonth(year, iMonth)) % 7;
}
return offset;
}
/***********************************************************************
* Computes the number of days in the given year.
**********************************************************************/
int numDaysYear(int year)
{
return 365 + isLeapYear(year);
}
/***********************************************************************
* Calculates the number of days in the given month.
**********************************************************************/
int numDaysMonth(int year, int month)
{
int days[] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
int daysMonth = days[month-1];
if (month == 2 && isLeapYear(year))
{
daysMonth = 29;
}
return daysMonth;
}
/***********************************************************************
* Determines if given year is a leap year.
**********************************************************************/
bool isLeapYear(int year)
{
return (year % 400 == 0) || (year % 100 != 0 && year % 4 == 0);
}
/**********************************************************************
* Displays the calender table.
**********************************************************************/
void display(int year, int month, int offset)
{
int day;
cout << endl;
string mth[] = { "January", "February", "March",
"April", "May", "June", "July", "August", "September",
"October", "November", "December" };
cout << mth[month-1];
cout << ", " << year << " ";
// Display month header
cout << " Su Mo Tu We Th Fr Sa ";
// Gets the correct offset width and end the line on the right
// day of the week
if (0 <= offset && offset <= 6)
{
day = ((offset + 1) % 7) + 1;
cout << setw((day - 2) * 4 + 6);
}
else
cout << "Error offset must be >= 0 and <=6 ";
// The loop for displaying the days and ending the line in the right place
for (int dayOfWeek = 1; dayOfWeek <= numDaysMonth(year, month); dayOfWeek++ )
{
cout << " " << setw(2) << dayOfWeek;
++day;
if (day == 8)
{
cout << " ";
day = 1;
}
}
if (day >= 2 && day <= 7)
cout << " ";
return;
}
string getDayOfWeek()
{
string day;
string dayOfWeek[] = { "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday" };
string sub = dayOfWeek[0];
int i = 0;
//With the following two lines you can input string
cout <<"Day of the week that you like to start: ";
cin >> day;
while (day != sub)
{
}
return day;
}
/**********************************************************************
* This function will call all the functions necessary to make a calendar
* for any given year.
***********************************************************************/
int main()
{
int offset;
int year;
string dayOfWeek;
year = getYear();
dayOfWeek = getDayOfWeek();
for (int month = 1; month <= 12; ++month)
{
offset = computeOffset(year, month);
display(year, month, offset);
}
return 0;
}