0% found this document useful (0 votes)
59 views6 pages

Calender

This C++ program prints a calendar for a given year by: 1) Getting the year from the user or generating a random one 2) Checking if the year is a leap year 3) Calculating what day of the week January 1st falls on 4) Printing the month names and days of the week headings 5) Counting the number of days in each month and printing the calendar
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
59 views6 pages

Calender

This C++ program prints a calendar for a given year by: 1) Getting the year from the user or generating a random one 2) Checking if the year is a leap year 3) Calculating what day of the week January 1st falls on 4) Printing the month names and days of the week headings 5) Counting the number of days in each month and printing the calendar
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

#include <iostream.

h>
#include <stdlib.h>
#include <ctype.h>
#include <time.h>
int getYear(); // has the user enter a valid year
bool isLeap(int year); // check for leap years
void dayName(); // prints the names for the day of
the week
void monthNameHeader(int year); // puts head for the month name
int startDay(int year); // decides what week day Jan starts
on
int monthCount(int counter); // how many days are in each month

void newMonth(int startDOW); // what day of the week new month starts on

void printAll(int year); // puts everything together, prints


to screen

int year = 0; // uesr inputed year || rand gen


per 0
int counter = 1; // counter for month name & #
days in month
int startDOW, // day of the week Jan starts on
wrap, // check for if weekday is
Saturday
daysInMonth; // total days in each month
int weekNumber = 0; // flag for first week of the month

int main()
{

year = getYear(); // has user enter year number


printAll(year);

return 0;
}

int getYear() //prompts the user to enter a valid year


{
char c;
srand(time(NULL));
cout << "Enter the year, or 0 for and random year: ";
do { // gets whole number value
cin.get(c);
if(isdigit(c))
{
year=year*10;
year +=(int)(c-'0');
}
} while(c!='\n');
if (year == 0) // if no response or 0 are enter,
random a year
{
year = rand() % 8600 + 1400;
cout << "\nThe random year " << year << " will be evaluated\n\n";
}
return year;
}

bool isLeap(int year) // checking for possible leap year


{
if (year % 400 == 0)
return true;
if (year % 100 == 0)
return false;
if (year % 4 == 0)
return true;
return false; // else return false
}

void dayName()
{
cout << " S M T W T F S" << endl;
cout << "---------------------" << endl;
}

void monthNameHeader(int year)


{

switch (counter)
{
case 1:
cout << " January " << year << endl;
break;

case 2:
cout << " February " << year << endl;
break;

case 3:
cout << " March" << year << endl;
break;

case 4:
cout << " April " << year << endl;
break;

case 5:
cout << " May " << year << endl;
break;

case 6:
cout << " June " << year << endl;
break;

case 7:
cout << " July " << year << endl;
break;

case 8:
cout << " August " << year << endl;
break;

case 9:
cout << " September " << year << endl;
break;

case 10:
cout << " October " << year << endl;
break;

case 11:
cout << " November " << year << endl;
break;

case 12:
cout << " December " << year << endl;
break;
}
}

int monthCount(int counter) // how many days are in the month


{
switch (counter)
{
case 1:
daysInMonth = 31; // current month days
break;

case 2: // checks for


possible leap year
if(isLeap(year))
daysInMonth = 29;
if(!isLeap(year))
daysInMonth = 28;
break;

case 3:
daysInMonth = 31;
break;
case 4:
daysInMonth = 30;
break;

case 5:
daysInMonth = 31;
break;

case 6:
daysInMonth = 30;
break;

case 7:
daysInMonth = 31;
break;

case 8:
daysInMonth = 31;
break;

case 9:
daysInMonth = 30;
break;

case 10:
daysInMonth = 31;
break;

case 11:
daysInMonth = 30;
break;

case 12:
daysInMonth = 31;
break;
}
}

int startDay(int year)


{

startDOW = (year + (year - 1 ) /4 - (year - 1) / 100 + (year - 1)


/400) %7;
return startDOW; // formula for what DoWeek
year starts on
}

void printAll(int year)


{

for (counter = 1; counter <= 12; counter++)


{

monthNameHeader(year); // prints
month day
dayName(); //
prints the name of days
if (counter==1)
wrap = startDay(year) ; // what day Jan starts
on
else
startDOW = wrap; // what day
other months start on

cout << " ";

for (int loopCount = 0; loopCount < startDOW; loopCount++)


{
cout << " "; // how many space to
indent new month
}

monthCount(counter); // how many days in


month

for (int dayCounter=1;dayCounter<=daysInMonth; dayCounter++)


{

if (wrap == 7) //if Saturday, carriage


return
{
cout << "\n ";
wrap = 0; //resets day
of week counter
weekNumber++; //no longer first
week of month
}
if (dayCounter<10) //adds space for
single digit days
cout << " ";
cout << dayCounter << " "; //prints the day
#
wrap++;
}

// cout << "\nthis month starts on day number " << startDOW; *testing*
// cout << "\ndays in this month are " << daysInMonth; *testing*
cout << "\n\n";
system("PAUSE");
cout << endl;
} // end BIG for loop

You might also like