c++; using multiple functions to create a yearly calendar and printing to an output file

I have to write a program that asks a user for a year. It can be any year from 1582 to 9999. Once the user has input the year the program will output a calendar for that year with the days correctly aligned, etc. I've already done all of this using functions and whatnot. The program is finished EXCEPT that I wish to output the calendar to an output file called "CalendarProgram.txt". Unfortunately I cannot seem to get the program to do this. Any words of advice to point me in the right direction are appreciated! I've already looked and found a few tips but none were fruitful. My code is below, if it helps.

//This program will allow a user to input a year, any year, 
//and will pull the correct days of the year and output them
//into a file

#include <iostream>
#include <cstdlib>
#include <fstream>
#include <iomanip>

using namespace std;

//Function prototypes
bool IsLeap(int); //Checks for leap year
int JanFirst(int); //Pulls the correct January first to line up the days
int DaysInMonth(int, bool); //Takes the number of the month and a flag    stating if it is a leap
int Header(int, int); //Takes the number of the month, the first day of said month, and prints a header
void PrintMonth(int, int&); //The first day of the following month
int Skip(int); //Prints the correct spaces
int SkipDays(int); //Prints leading spaces

int main()
{
    ofstream fout;
    fout.open("CalendarProgram.txt");

{
    int year, dayone, month, numdays, print;
    bool leap;

    do {
        cout << "What year would you like a calendar for? " << endl;
        cin >> year;
        cout << endl;
        if (year < 1582 || year > 9999)
            cout << "ERROR! That year was not recorded in the Gregorian Calendar System!"
            << "\nPope Gregory XVIII introduced the Gregorian Calendar System in 1582.\n\n";
    } while (year < 1582 || year > 9999);

    dayone = JanFirst(year);
    leap = IsLeap(year);
    Skip(9);

    month = 1;

    while (month <= 12)
    {
        numdays = DaysInMonth(month, leap);
        Header(month, year);
        PrintMonth(numdays, dayone);
        cout << endl << endl << endl;
        month = month + 1;
    }
    cout << endl;
}
}

//Function definition: IsLeap
//Preconditions: The user has given a year they want to view a calendar of
//Postconditions: This function will check whether the year is a leap year or not
bool IsLeap(int year)
{
bool days;

if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0)
{
    days = true;
}
else
{
    days = false;
}

return days;
}

//Function definition: JanFirst
//Preconditions: The user has input the year they wish to view a calendar for and IsLeap checked for leap year
//Postconditions: This function will output the correct days in their respective spots on the calendar
int JanFirst(int year)
{
int daystart;

year = year - 1;

daystart = (1 + year + (year / 4) - (year / 100) + (year / 400)) % 7;

return daystart;
}

//Function definition: DaysInMonth
//Preconditions: The year has been checked for leap
//Postconditions: Will output the number of days in the month
int DaysInMonth(int month, bool leap)
{
if (month == 1)
    return(31);
else if (month == 2)
    if (leap)
        return(29);
    else
        return(28);
else if (month == 3)
    return (31);
else if (month == 4)
    return (30);
else if (month == 5)
    return (31);
else if (month == 6)
    return (30);
else if (month == 7)
    return (31);
else if (month == 8)
    return (31);
else if (month == 9)
    return (30);
else if (month == 10)
    return (31);
else if (month == 11)
    return (30);
else if (month == 12)
    return (31);
}

//Function definition: Header
//Preconditions: The year has been selected and checked for leap
//Postconditions: Will output the header
int Header(int month, int year)
{
cout << endl;
switch (month)
{
case 1: Skip(3);
    cout << "January " << year << endl << endl;
    break;
case 2:  Skip(3);
    cout << "February " << year << endl << endl;
    break;
case 3:  Skip(3);
    cout << "March " << year << endl << endl;
    break;
case 4:  Skip(3);
    cout << "April " << year << endl << endl;
    break;
case 5:  Skip(3);
    cout << "May " << year << endl << endl;
    break;
case 6:  Skip(3);
    cout << "June " << year << endl << endl;
    break;
case 7:  Skip(3);
    cout << "July " << year << endl << endl;
    break;
case 8:  Skip(3);
    cout << "August " << year << endl << endl;
    break;
case 9: Skip(3);
    cout << "September " << year << endl << endl;
    break;
case 10: Skip(3);
    cout << "October " << year << endl << endl;
    break;
case 11:  Skip(3);
    cout << "November " << year << endl << endl;
    break;
case 12:  Skip(3);
    cout << "December " << year << endl << endl;
    break;
default: month = 1;
}
cout << "---------------------------" << endl;
cout << " S   M   T   W   R   F   S " << endl;
cout << "---------------------------" << endl << endl;
return month;
}

//Function definition: PrintMonth
//Preconditions: The year has been selected and checked for leap
//Postconditions: This will align the days
void PrintMonth(int numdays, int&weekday)
{
int day = 1;
SkipDays(weekday);
while (day <= numdays)
{
    cout << setw(2) << day << "  ";
    if (weekday == 6)
    {
        cout << endl;
        weekday = 0;
    }
    else
        weekday = weekday + 1;
    day = day + 1;
}
}

//Function definition: Skip
//Preconditions: The year has been selected and days figured out
//Postconditions: Will place the days of the months in the correct positions
int Skip(int i)
{
while (i > 0)
{
    cout << "  ";
    i = i - 1;
}
return i;
}

//Function definition: SkipDay
//Preconditions: PrintMonth has been called
//Postconditions: This should align the first day of every month correctly
int SkipDays(int day)
{
return Skip(2 * day);
}

Upvotes: 0

Views: 712

Answers (1)

Some programmer dude
Some programmer dude

Reputation: 409166

If you can output to std::cout, then you can output to any output stream.

Instead of using cout hard-coded like you do, pass a reference to the output stream to the functions, and you can then simply pass a output file stream (or any other output stream).

Upvotes: 1

Related Questions