Help with C++ Calendar!

Feb 17, 2017 at 1:30am
I have a small problem with my code for my calendar. most of the months displayed have the right start days but are to spaced out due to the setw that is needed to be placed on their following days.

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
  #include <iostream>
#include <string>
#include <iomanip>
using namespace std;
const string MONTHS[13] = { " ", "January", "February", "March", "April", "May",
"June", "July", "August", "September", "October", "November", "December" };
void PrintMonth(const int month, const int startday, const bool leap);
int main(void)
{
	// ******************************************************************
	// * Print just a bunch of Random months - Assignment #3 *
	// ******************************************************************
	PrintMonth(1, 0, false); // Print January 1st, on a Sunday
	PrintMonth(2, 1, true); // Print February 1st leap year, on Monday
	PrintMonth(1, 2, false); // Print January 1st, on a Tuesday
	PrintMonth(2, 3, false); // Print February 1st not leap, on Wednesday
	PrintMonth(1, 4, false); // Print January 1st, on a Thursday
	PrintMonth(2, 5, false); // Print February 1st, on a Friday
	PrintMonth(1, 6, false); // Print January 1st, on a Saturday
	PrintMonth(6, 1, false); // Print June 1st, on Monday
	PrintMonth(12, 4, false); // Print December 1st, on a Thursday

	return 0;
}


void PrintMonth(const int month, const int startday, const bool leap)
{
	int days_num[32] = { 0, 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 };
	int daysinmonths[13] = { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
	if (leap == true){
		daysinmonths[2] = 29;
	}

	int length = 0;
	length = MONTHS[month].length();
	length = (30 - length) / 2 + length;
	cout << setw(length) << endl << MONTHS[month] << endl;
	cout << "  Sun Mon Tue Wed Thu Fri Sat";



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

		case 0:
			
			cout << setw(4) << days_num[count];
			break;
		case 1:
			
			cout << setw(8) << days_num[count];
			break;
		case 2:
			cout << setw(12) << days_num[count];
			break;
		case 3:
			cout << setw(16) << days_num[count];
			break;
		case 4:
			cout << setw(20) << days_num[count];
			break;
		case 5:
			cout << setw(24) << days_num[count];
			break;
		case 6:
			cout << setw(28) << days_num[count];
		}
 
	}
}

Last edited on Feb 17, 2017 at 3:12am
Feb 17, 2017 at 4:26am
@oatmeal678

Your main problem with your code, IMHO, is your printing the calendar days, still using the setw you started with when printing day 1 of the month. I got rid of all your switch statements, and just used the startday, to find where to begin.

Here is a corrected version of your calendar program. If you have any questions, please ask..

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
#include <iostream>
#include <string>
#include <iomanip>
using namespace std;
const string MONTHS[13] = { " ", "January", "February", "March", "April", "May",
	"June", "July", "August", "September", "October", "November", "December" };
void PrintMonth(const int month, const int startday, const bool leap);
int main(void)
{
	// ******************************************************************
	// * Print just a bunch of Random months - Assignment #3 *
	// ******************************************************************
	PrintMonth(1, 0, false); // Print January 1st, on a Sunday
	PrintMonth(2, 1, true); // Print February 1st leap year, on Monday
	PrintMonth(1, 2, false); // Print January 1st, on a Tuesday
	PrintMonth(2, 3, false); // Print February 1st not leap, on Wednesday
	PrintMonth(1, 4, false); // Print January 1st, on a Thursday
	PrintMonth(2, 5, false); // Print February 1st, on a Friday
	PrintMonth(1, 6, false); // Print January 1st, on a Saturday
	PrintMonth(6, 1, false); // Print June 1st, on Monday
	PrintMonth(12, 4, false); // Print December 1st, on a Thursday

	return 0;
}

void PrintMonth(const int month, const int startday, const bool leap)
{
  // int days_num[32] = { 0, 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 };
// Above NOT needed. Get days from count loop below	
   int daysinmonths[13] = { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
	if (leap == true)
	{
		daysinmonths[2] = 29;
	}
	string spaces="    ";
	int length = 0;
	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<startday;x++)
		cout << spaces;


	for (int count = 0; count < daysinmonths[month]; count++)
	{
		if ((count + startday) % 7 == 0 )
		{
			cout << endl << "  "; // Make starting number under 'Sun'
		}
		if(count+1 < 10)
			cout << " "; // To keep numbers aligned
		cout << count+1 << "  ";
	}
	cout << endl << endl;
}
Last edited on Feb 18, 2017 at 5:11am
Feb 17, 2017 at 3:35pm
Hey, when you are available could you give me your thoughts on what you did when you edited my program.
Feb 17, 2017 at 7:30pm
@oatmeal678

I'll be glad to answer your questions, but first you'll have to ask one. I already added some comments in the source code, but I don't know what you're still uncertain of. So, ask a specific question about what you're not sure of, and I'll explain it a bit more in detail, but I'm not going over it line by line, telling you things, that you may already be familiar with.
Topic archived. No new replies allowed.