writing to a file and not standard output

I am supposed to create a program that accepts a 4 digit year and prints out a calendar for that year. It must be modified to write the calendar to a .datfile and not to standard output. This is what I have so far. I cant figure out how to get it to print the whole year and not just December. Please Help!

#include <iostream>
#include <string>
#include <iomanip>
#include <fstream>

using namespace std;

bool isleapyear(const int year);
void printmonth(const int month, const int startday, bool leap);
void printyear(const int year);
int dayofweek(const int day, const int month, const int year);

int main(void)
{
int year;
cout << "Enter a year after 1582 to be printed" << endl;
cin >> year;
if (year < 1582)
cout << "Sorry, please enter a year equal to or greater than 1582: ";
else
printyear(year);
}

void printyear(const int year)
{
printmonth(1,dayofweek(1,1,year),isleapyear(year));cout<<endl;
printmonth(2,dayofweek(1,2,year),isleapyear(year));cout<<endl;
printmonth(3,dayofweek(1,3,year),isleapyear(year));cout<<endl;
printmonth(4,dayofweek(1,4,year),isleapyear(year));cout<<endl;
printmonth(5,dayofweek(1,5,year),isleapyear(year));cout<<endl;
printmonth(6,dayofweek(1,6,year),isleapyear(year));cout<<endl;
printmonth(7,dayofweek(1,7,year),isleapyear(year));cout<<endl;
printmonth(8,dayofweek(1,8,year),isleapyear(year));cout<<endl;
printmonth(9,dayofweek(1,9,year),isleapyear(year));cout<<endl;
printmonth(10,dayofweek(1,10,year),isleapyear(year));cout<<endl;
printmonth(11,dayofweek(1,11,year),isleapyear(year));cout<<endl;
printmonth(12,dayofweek(1,12,year),isleapyear(year));cout<<endl;
}
bool isleapyear(const int year)
{
/* Check if the year is divisible by 4 or
is divisible by 400 */
bool leap = false;
if ((year % 4 == 0) && (year % 100 != 0) || ( year % 400 == 0))
return true;
else
return false;
}
//Finds what day of the week the year starts on
int dayofweek(const int day, const int month, const int year)
{
int a,y,m;
a=(14-month)/12;
y=year-a;
m= month+12*a-2;
return (day+y+y/4-y/100 + y/400 + (31*m/12))%7;
}


void printmonth(const int month, const int startday, const bool leap)
{
ofstream outputfile;

outputfile.open("cal.dat");

//Makes the arrays for the days of the week, the months, the days in each in month, and changes the days in Feb to 29 if leap is true.
string Days[7] = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};
string Months[13] = {" ", "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"};
int DaysinMonth[13] = {0,31,28,31,30,31,30,31,31,30,31,30,31};
//Checks to see if it is a leap year and changes the days in February accordingly.
if(leap)
DaysinMonth[2] = 29;

// Finds the length of the name of the month,divides it by 2, and adds that length + 14 spaces to the beginning, centering it.
string str = Months[month];
int len = str.length();
outputfile << setw((len/2)+14) << Months[month] << endl;
outputfile << " Sun Mon Tue Wed Thu Fri Sat" << endl;

// Adds four spaces for each day before startday
int i = 1;
int count = 0;
for(count = 0; count < startday ; count++)
{
outputfile << " ";
}
// Counts through the days of the month.
for(i=1;i<=DaysinMonth[month];i++)

{
// Outputs each days numerical value, checks to see if it is the end of the week, and goes to the next day
outputfile << setw(4) << i;
if((startday+i) % 7 == 0)
outputfile << endl;


}

outputfile.close();

cout << endl;

}
Last edited on
Topic archived. No new replies allowed.