Zachary Fernandez Zachary Fernandez - 12 days ago 7
C++ Question

How to output a calendar program with functions to a .txt file

The question I have is figuring out what is wrong with my program and why it will not write to an output file that I have specified.

#include <iostream>
#include <cstdlib>
#include <iomanip>
#include <fstream>
using namespace std;

int numOfDaysInAMonth(int, int);
void printMonth(int);
void printDaysofMonth(int, int&);
void skipToDay(int);
int main()
{
ofstream ofile;
ofile.open("calendar.txt");

int year, firstDayInCurrentMonth;
int currentMonth = 1;
int numDays;

cout << "What year is this? ";
cin >> year;

cout << endl;

cout << " 0 - Sunday\n";
cout << " 1 - Monday\n";
cout << " 2 - Tuesday\n";
cout << " 3 - Wednesday\n";
cout << " 4 - Thursday\n";
cout << " 5 - Friday\n";
cout << " 6 - Saturday\n";

cout << "What day of then week is Jan 1? ";

cin >> firstDayInCurrentMonth;

ofile << year << endl;

while (currentMonth <= 12) {
numDays = numOfDaysInAMonth(currentMonth, year);
printMonth(currentMonth);
printDaysofMonth(numDays, firstDayInCurrentMonth);
ofile << endl << endl << endl;
currentMonth = currentMonth + 1;
}

ofile << endl;

system("pause");
return 0;
}


I use ofile to output the text but it is not being returned correctly.

// This function returns the number of days in a month
int numOfDaysInAMonth(int m, int y)
{
if (m == 1)
return(31);
else if (m == 2 && y % 4 != 0)
return(28);
if (m == 2 && y % 4 == 0)
return(29);
else if (m == 3)
return(31);
else if (m == 4)
return(30);
else if (m == 5)
return(31);
else if (m == 6)
return(30);
else if (m == 7)
return(31);
else if (m == 8)
return(31);
else if (m == 9)
return(30);
else if (m == 10)
return(31);
else if (m == 11)
return(30);
else if (m == 12)
return(31);
}

// It takes the number of the month and prints outs the name of the month and the frame of the calander
void printMonth(int m)
{
ofstream ofile;
ofile.open("calendar.txt");

if (m == 1)
{
ofile << "January" << endl;
}
else if (m == 2)
{
ofile << "February" << endl;
}
else if (m == 3)
{
ofile << "March" << endl;
}
else if (m == 4)
{
ofile << "April" << endl;
}
else if (m == 5)
{
ofile << "May" << endl;
}
else if (m == 6)
{
ofile << "June" << endl;
}
else if (m == 7)
{
ofile << "July" << endl;
}
else if (m == 8)
{
ofile << "August" << endl;
}
else if (m == 9)
{
ofile << "September" << endl;
}
else if (m == 10)
{
ofile << "October" << endl;
}
else if (m == 11)
{
ofile << "November" << endl;
}
else if (m == 12)
{
ofile << "December" << endl;
}


ofile << " S M T W T F S" << endl;
ofile << "_____________________" << endl;
}

// Helps with the skipToDay function
void skip(int i)
{
ofstream ofile;
ofile.open("calendar.txt");

while (i > 0)
{
ofile << " ";
i = i - 1;
}
}

// This function prints out the days in the month after the header for each month
void printDaysofMonth(int numDays, int &weekDay)
{
ofstream ofile;
ofile.open("calendar.txt");

int day = 1;
skipToDay(weekDay);
while (day <= numDays)
{
ofile << setw(2) << day << " ";
if (weekDay == 6)
{
ofile << endl;
weekDay = 0;
}
else weekDay = weekDay + 1;
day = day + 1;
}
}

// Prints spaces in monthly calander
void skipToDay(int d)
{
return skip(3 * d);
}

Answer
void printDaysofMonth(int numDays, int &weekDay)
{
    ofstream ofile;
    ofile.open("calendar.txt");
    ...
}

int main()
{
  ofstream ofile;
  ofile.open("calendar.txt");
  printDaysofMonth(...);
  ofile << "data...";
  ...
}

The file is already open. The first stream will write over what the second stream had written, if any. Change the function to to pass ofstream by reference:

void printDaysofMonth(ofstream &ofile, int numDays, int &weekDay)
{
    ...
}

int main()
{
  ofstream ofile;
  ofile.open("calendar.txt");
  printDaysofMonth(ofile, ...);
  ...
}