Image

Imagepixelpoet wrote in Imagecpp 😟thirsty

Listens: Alex To - Didn't Mean to Hurt You

humm...i did as7
anyone want to help me with as8



//Write a program that prints a calendar month given the month and the day of the week the month starts on.

(sorrie i can't include the < and the > signs in the libraries--it won't show up on the screen)

#include iostream
#include string
#include iomanip
#include fstream

using namespace std;

string numToMonth (int); //function for to print month name
int howManyDays (int, char); //function for days
void printCalender (ostream&, string&, int, char); //function for printing calender
void errorCheck (int &); //error checking function

void main ()
{
int month, days; //month, days of month
char leap, week, print; //leap year, day of week, print to file or screen
string monthName; //name of month
char filename[40]; //filename

//greeting
cout << "Hi, I am a neat little program here to help you print a calender! \n";
cout << "But, you need need to help me by filling out some information: \n";
//prompts user for month
cout << "\nWhat month are you thinking about (1-12): ";
cin >> month;
//error checking for month
errorCheck(month);
//prompts for leap
cout << "Are you thinking of a leap year (y/n): ";
cin >> leap;
//error checking for leap
while (leap != 'y' && leap != 'Y' && leap != 'n' && leap != 'N')
{
cout << "Error! Enter (y/n): ";
cin >> leap;
}
//prompts for start of week
cout << "So, what day of the week does the month start on?";
cout << "(S, M, T, W, R, F, s): ";
cin >> week;
//error checking for week
while (week != 'S' && week != 's' && week != 'M' && week != 'T' && week != 'W'
&& week != 'R' && week != 'F')
{
cout << "Error, what day of the week does the month start on?\n";
cin >> week;
}
//propmts for output of calender
cout << "So, do you want to print calender on (s)creen or in a (f)ile: ";
cin >> print;

//function call of month name
monthName = numToMonth (month);

//function call of days
days = howManyDays(month, leap);

//if (f) prints calender to file, else prints calender to screen
if(print == 'f' || print == 'F')
{
cout <<"filename: ";
cin >> filename;
ofstream outfile;
outfile.open(filename);

if (outfile.fail())
{
cout <<"Error opening file!\n";
exit (-1);
}
printCalender(outfile, monthName, days, week);

outfile.close();

cout << "Your calender has been been printed to: " << filename << endl;
}
else
{
printCalender(cout, monthName, days, week);
}

cout << "\nCopyrighted 2002, by Carina Yun (cs10bs)\n";

} //end of main

//function for number to month name
string numToMonth (int month)
{
string monthName; //name of month

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

return monthName;
}

//function for number of days in month
int howManyDays (int month, char leap)
{
int days; //days in month

switch (month)
{
case 4:
case 6:
case 9:
case 11:
days = 30;
break;

case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:
days = 31;
break;

case 2:
if (leap == 'y' || leap == 'Y')
days = 29;
else if (leap == 'n' || leap == 'N')
days = 28;
break;
}
return days;
}

//function to print calender
void printCalender (ostream & out, string & monthname, int numofdays, char firstday)
{
int i, count;

out << setw(10) <<" " << monthname << endl;
out << " S M T W R F S " << endl;
out << setfill ('-') << setw (27) << "-" << endl;

//generates spaces before printing of days
switch (firstday)
{
case 'S':
count = 1;
out << " ";
break;
case 'M':
count = 2;
out << " ";
break;
case 'T':
count = 3;
out << " ";
break;
case 'W':
count = 4;
out << " ";
break;
case 'R':
count = 5;
out << " ";
break;
case 'F':
count = 6;
out << " ";
break;
case 's':
count =7;
out << " ";
break;
}
//prints one automatically after spaces
out << 1;

//allocates 'spaces' in between numbers and returns line when appropiate
for (i=2; i <= numofdays; i++)
{


if (count > 6)
{
out << endl;
out << setfill(' ') << setw(2) << i;
count = 1;
}
else
{
out << setfill(' ') << setw(4) << i;
count++;
}
}
out << endl;
}

//function for error checking
void errorCheck (int &num)
{
while (cin.fail() || num < 1 || num > 12)
{
cin.clear ();
cin.ignore (80, '\n');
cout << "\nError! Try Again: ";
cin >> num;
}
}