Help with c++ calendar

I need some help with setting up an output file for my full year calendar and displaying the start date for every month(I have a day of the week formula, but im not sure how to incorporate it) on the correct day while also displaying all the days of that month.
#include <iostream>
#include <string>
#include <iomanip>
#include <fstream>
using namespace std;
const int MIN = 1582;
const int MAX = 9999;
int startday[7] = { 0, 1, 2, 3, 4, 5, 6 };
int daysinmonths[13] = { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
const string MONTHS[13] = { " ", "January", "February", "March", "April", "May",
"June", "July", "August", "September", "October", "November", "December" };
bool isleapyear(const int year);
void printyear(const int year);
int dayofweek(const int day, const int month, const int year);
int main(void){
int year1;
ifstream File;
File.open("calendar.txt");
cout << "Please enter a year between 1582 and 9999\nto obtain the full calendar year of that date.\n";
cin >> year1;
while (year1 < MIN || year1 > MAX){
cout << "Incorrect input, please enter a year within the range of 1582 through 9999!\n";
cin >> year1;
}
printyear(year1);

return 0;


}


bool isleapyear(const int year){

if ((year % 400 == 0) == true &&! (year % 100 == 0)){
daysinmonths[2] = 29;
cout << "This is a leap year.\n" << endl;
}
else if ((year % 4 == 0) == true){
daysinmonths[2] = 29;
cout << "This is a leap year.\n" << endl;
}

return year;
}

int dayofweek(const int day, const int month, const int year){
int a, y, m, d;
string spaces = " ";
a = (14 - month) / 12;
y = year - a;
m = 1 + 12 * a - 2;
d = (day + y + y / 4 - y / 100 + y / 400 + (31 * m / 12)) % 7;
for (int count = 0; count == daysinmonths[d]; count++){
if (d == 0){
cout << " " << count + 1;
}

}
return year;
}
void printyear(const int year){

isleapyear(year);






int length = 0;
cout << setw(17) << year;
for (int count = 1; count <= daysinmonths[count]; count++)
{


length = MONTHS[count].length();
length = (30 - length) / 2 + length;

cout << setw(length);
cout << endl << endl << endl << setw(length) << MONTHS[count] << endl;

cout << " Sun Mon Tue Wed Thu Fri Sat\n";

dayofweek(1, count, year);
}

}
@oatmeal678

I replaced your dayofweek function with my version, as it only needed the month and year to find the day the first lands on. Here's your program printing all 12 months of the given year. You'll have to add the writing of the file yourself, as I see you open a file, but never use it.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
// C++ Calendar.cpp : main project file.

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

using namespace std;
const int MIN = 1582;
const int MAX = 9999;

int daysinmonths[13] = { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
const string MONTHS[13] = { " ", "January", "February", "March", "April", "May",
	"June", "July", "August", "September", "October", "November", "December" };
bool isleapyear(const int year);
void printyear(const int year);
int dayofweek(const int month, const int year);

int main()
{
	int year1;
	ifstream File;
	File.open("calendar.txt");
	cout << "Please enter a year between 1582 and 9999\nto obtain the full calendar year of that date.\n";
	cin >> year1;
	while (year1 < MIN || year1 > MAX){
		cout << "Incorrect input, please enter a year within the range of 1582 through 9999!\n";
		cin >> year1;
	}
	printyear(year1);

	return 0;
}

bool isleapyear(const int year)
{

	if ((year % 400 == 0) == true &&! (year % 100 == 0)){
		daysinmonths[2] = 29;
		cout << "This is a leap year.\n" << endl;
	}
	else if ((year % 4 == 0) == true){
		daysinmonths[2] = 29;
		cout << "This is a leap year.\n" << endl;
	}
	return year;
}

int dayofweek(int month, int year)
{
	int t[12] = { 0, 3, 2, 5, 0, 3, 5, 1, 4, 6, 2, 4 };
	int y = year - (month < 2);
	y = (y + y / 4 - y / 100 + y / 400 + t[month] + 1) % 7;
	// y = 0 for Sunday, 1 for Monday.. etc.
	return y;
}

void printyear(const int year)
{

	isleapyear(year);

	int length = 0, days, skip ;

	cout << setw(17) << year;
	for(int month=1;month<13;month++)
	{
		days=daysinmonths[month];
		skip = dayofweek(month-1, year);
		length = MONTHS[month].length();
		length = (30 - length) / 2 + length;

		cout << endl << setw(length) << MONTHS[month] << endl;
		cout << " Sun Mon Tue Wed Thu Fri Sat" << endl;
		cout << " ";
		for(int x = 0;x < skip;x++)
			cout << "    ";
		for (int count = 0; count < daysinmonths[month]; count++)
		{
			if ((count + skip) % 7 == 0 )
			{
				cout << endl << " "; // Make starting number under 'Sun'
			}
			if(count+1 < 10)
				cout << " "; // To keep numbers aligned
			cout << count+1 << "  ";
		}
		cout << endl << endl;
	}
}
I changed some of my code on the bottom, I dont want to copy your code, Is there a way that I can still do it this way to put the numbers on the right startday.
int dayofweek(const int month, const int year){
int a, y, m, d;

a = (14 - month) / 12;
y = year - a;
m = 1 + 12 * a - 2;
d = (1 + y + y / 4 - y / 100 + y / 400 + (31 * m / 12)) % 7;

for (int count = 1; count <= daysinmonths[month]; count++){
if ((count - 1) % 7 == 0){
cout << endl << " ";
}
if (count + 1 < 10)
cout << " ";
cout << count << " ";
}


return year;
}
void printyear(const int year){

isleapyear(year);






int length = 0;
cout << setw(17) << year;
for (int count = 1; count <= daysinmonths[count]; count++)
{


length = MONTHS[count].length();
length = (30 - length) / 2 + length;

cout << setw(length);
cout << endl << endl << endl << setw(length) << MONTHS[count] << endl;

cout << " Sun Mon Tue Wed Thu Fri Sat";
cout << "";
dayofweek(count, year);
}
cout << endl << endl;
}
one more thing I am just very confused on the way you set up the spaces to align and set the first on the right day. could you explain your thinking because I am fairly new at programming in general.
@oatmeal678

I tried different things with your dayofweek function, and I cannot get it to work, so, no, I don't think it will. Plus, it seems you're trying to print the month in the function. That's what your printyear function is for.

On to aligning the spaces for the right day. The dayofweek function returns the value y, which indicates which day the 1st falls on. As I show with the remark statement on line 54, if y = 0, then the first is on Sunday. A one indicates Monday, etc. So, I just use a for loop to print spaces up to the desired day of week. I use 4 spaces in each cout, since your week days each have 3 letters plus 1 space. That would place the starting position of the next print statement at the start of the first letter of the week name. Also, line 75 in my code, is to start the next statement under the 'S' in Sun. Same with line 82. Lines 84 and 85, print a pace IF the number to be printed is less than 10, just to keep the columns even, so the ones columns always line up.
Last edited on
Topic archived. No new replies allowed.