Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <iomanip>
- #include <string>
- #include <fstream>
- using namespace std;
- //function prototypes
- void printmonth(const int, const int);
- void printyear(const int);
- bool isleap(const int);
- int dayofweek(const int, const int, const int);
- //global variables
- fstream outFile; //allows user to input/output data to a seperate file
- int year;
- int startday;
- const int DAY_WIDTH = 3;
- // *********************************************************************
- // * Function Name: main *
- // * Description: This function opens the file for output and prompts *
- // * user to input a year. It then calls function printyear, which *
- // * uses function printmonth to output the calendar. *
- // * *
- // * Parameter Description: none *
- // * *
- // * Author: Matthew Draper *
- // * Date: February 3, 2012 *
- // *********************************************************************
- int main()
- {
- outFile.open("cal.txt"); // open file
- outFile.seekg(0L, ios::beg); // move read pointer to beg
- cout << "Welcome to the Calendar printing program! \n\n";
- cout << "Please enter a 4 digit year after 1581: ";
- cin >> year;
- cout << endl;
- while (year < 1582 || year > 9999)
- {
- cout << "Invalid. Enter a 4 digit year after 1581: ";
- cin >> year;
- cout << endl;
- }
- //calls printyear function
- printyear(year);
- cout << endl << endl;
- cout << "The Calendar for " << year << " has been sent to and displayed in"
- << " the newly created file." << endl << endl;
- //closes the file
- outFile.close();
- return 0;
- }
- // *********************************************************************
- // * Function Name: isleap *
- // * Description: The function determines if its a leap year *
- // * *
- // * Parameter Description: int year -> Numeric year *
- // * *
- // * Author: Matthew Draper *
- // * Date: February 3, 2012 *
- // *********************************************************************
- bool isleap(const int year)
- {
- if (year % 4 == 0)
- {
- if (year % 100 == 0 && year % 400 != 0)
- return false;
- else
- return true;
- }
- else
- return false;
- }
- // *********************************************************************
- // * Function Name: dayofweek *
- // * Description: The function uses the values passed in for the *
- // * starting date, the numeric month, and the user input year. *
- // * year. The day of the week that month started on is then *
- // * calculated and passed back to function 'printmonth' *
- // * *
- // * Parameter Description: int day -> numeric day of the month *
- // * int month -> numeric month of the year *
- // * int year -> numeric year *
- // * *
- // * Author: Matthew Draper *
- // * Date: February 3, 2012 *
- // *********************************************************************
- int dayofweek(const int day, const int month, const int year)
- {
- int a,y,m,d;
- a = (14 - month) / 12;
- y = year - a;
- m = month + (12 * a) - 2;
- d = (day + y + (y / 4) - (y / 100) + (y / 400) + (31 * m / 12)) % 7;
- startday = d;
- return startday;
- }
- // *********************************************************************
- // * Function Name: printmonth *
- // * Description: The function uses the values passed in for the *
- // * the numeric month, and user input year *
- // * it's a leap year. The month chosen is then printed out correctly. *
- // * *
- // * Parameter Description: int month -> Numeric Month 1-12 *
- // * int year -> Numeric year *
- // * *
- // * Author: Matthew Draper *
- // * Date: February 3, 2012 *
- // *********************************************************************
- void printmonth(const int month, const int year)
- {
- int DaysInMonth[13] = {0,31,28,31,30,31,30,31,31,30,31,30,31};
- //calls function isleap and returns true/false
- isleap(year);
- if (isleap(year) == true)
- DaysInMonth[2] = 29;
- string MonthCalled[13]={" ","January", "February", "March", "April", "May",
- "June", "July", "August", "September", "October", "November", "December"};
- string DaysHeader = "Su Mo Tu We Th Fr Sa";
- string centered = MonthCalled[month];
- //this centers the month name over the days and dates
- int chars = centered.length();
- int ws = chars/2 + 10;
- outFile << setw(ws) << MonthCalled[month] << endl;
- outFile << DaysHeader << endl;
- outFile << " ";
- //calls function dayofweek and uses the returned 'startday' value
- dayofweek(1, month, year);
- //loop keeps adding 3 spaces until startday is reached
- for(int i=1; i <= startday; i++)
- {
- outFile << " ";
- }
- //loop that prints out the date under the corresponding day of week.
- int DayMarker;
- for (int day = 1; day <= DaysInMonth[month]; day++)
- {
- DayMarker = startday + day;
- if (DayMarker % 7 == 0)
- outFile << day << endl << setw(DAY_WIDTH - 1);
- else
- outFile << day << setw(DAY_WIDTH); // correctly formats
- }
- outFile << endl << endl; //creates space between each printed month
- return;
- }
- // *********************************************************************
- // * Function Name: printyear *
- // * Description: This function prints the user requested year and *
- // * then calls function printmonth 12 times to print the calendar *
- // * for every month of that year.
- // * Parameter Description: year - numeric year sent in by user input *
- // * *
- // * Author: Matthew Draper *
- // * Date: February 3, 2012 *
- // *********************************************************************
- void printyear(const int year)
- {
- outFile << setw(12) << year << "\n\n";
- printmonth(1, year);
- printmonth(2, year);
- printmonth(3, year);
- printmonth(4, year);
- printmonth(5, year);
- printmonth(6, year);
- printmonth(7, year);
- printmonth(8, year);
- printmonth(9, year);
- printmonth(10, year);
- printmonth(11, year);
- printmonth(12, year);
- return;
- }
Add Comment
Please, Sign In to add comment