WHAT IS WRONG WITH MY CODES? :( tryna print a calendar

Hi everyone
I am a total beginner and my calendar keeps coming up like this :

1980년 부터 2030년 사이의 연월을 입력하세요 (ex: 2011 5)
2000 2
입력하신 달은 2000년 2월 입니다.

< 2000 February >
Sun Mon Tue Wed Thu Fri Sat
----------------------------
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


---------------------------







---------------------------


Ignore the foreign language.
Basically getting a year and a month in two numbers and printing that month's calendar, including leap years.
But the first week of the month won't show up and there are several unnecessary lines of "----------" at the bottom of the results.
Please help me :(

+
why does this message come up? :

Run-Time Check Failure #2 - Stack around the variable 'cal' was corrupted.


++
Is there any way I could make all the dates on my calendar 2-digit numbers? for instance 01 02 03 04, instead of 1 2 3 4 ?

Thank you



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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
 
#include <iostream>
using namespace std;

int days(int year, int month)
{
	int days, i;

	for (i = 1; i < month; i++)
	{
		if (i == 1 || i == 3 || i == 5 || i == 7 || i == 8 || i == 10 || i == 12)
			days = days + 31;
		else if (i == 4 || i == 6 || i == 9 || i == 11)
			days = days + 30;
		else if (i == 2)
			days = days + 28;
	}
	days = days + 365 * (year - 1980);
	return days;
}

int leapyear(int year, int month)
{
	int i;
	i = (year - 1977) / 4;

	if (year % 4 == 0 && month > 2)
	{
		i++;
	}
	return i;
}

int weekdays(int days)
{
	int i;
	i = days % 7;
	return i;
}

void calendar(int year, int month, int week)
{
	int i, j, k, d, cal[49] = {0};
	char m[12][10] = { "January", "February", "March", "April", "May", "June", "July", "Agust","September", "Octorber", "November", "December" };
	switch (month)
	{
		case 1: d = 31;
			break;
		case 2 :
		{
			if ((year % 4) == 0) d = 29;
			else d = 28;
		}
			break;
		case 3: d = 31;
			break;
		case 4: d = 30;
			break;
		case 5: d = 31;
			break;
		case 6: d = 30;
			break;
		case 7: d = 31;
			break;
		case 8: d = 31;
			break;
		case 9: d = 30;
			break;
		case 10: d = 31;
			break;
		case 11: d = 30;
			break;
		case 12: d = 31;
			break;
	}
	cout << "    < " << year << " " << m[month - 1] << " >" << endl;
	cout << " Sun Mon Tue Wed Thu Fri Sat" << endl;
	cout << "----------------------------" << endl;

	j = 1;
	for (i = (week + 2) % 7; i < (d + (week + 2) % 7); i++)
	{
		cal[i] = j;
		j++;
	}
	k = 0;
	for (i = 0; i < 6; i++)
	{
		for (j = 0; j < 6; j++)
		{
			if (cal[k] == 0)
			{
				cout << "  " << endl;
			}
			else
			{
				cout << cal[k] << "  ";
			}
			k++;
		}
		cout << "\n---------------------------" << endl;
	}
}


int main()
{
	int year, month, total_days, week;

	cout << "1980년 부터 2030년 사이의 연월을 입력하세요 (ex: 2011 5)" << endl;
	cin >> year >> month;

	if (year >= 1980 && year <= 2030 && month >= 1 && month <= 12)
	{
		cout << "입력하신 달은 " << year << "년 " << month << "월 입니다.\n" << endl;
		
		total_days = days(year, month) + leapyear(year, month);
		week = weekdays(total_days);
		calendar(year, month, week);
	}
	else
	{
		cout << "범위를 벗어났습니다" << endl;
	}
	system("PAUSE");
	return 0;
}
Last edited on
Your function days doesn't set days = 0 at the start, so it could be any value at all.
You're also doing yourself no favours by using char arrays instead of strings, by using arrays instead of vectors. You're choosing to use intermediate level, difficult arrays instead of beginner, simple vectors and strings.
Take a look here on how to calculate the leap year:

https://en.wikipedia.org/wiki/Leap_year

You only determine leap year when month > 2. I.e. when the user enters january there will be no leap year.

and there are several unnecessary lines of "----------" at the bottom of the results.
This is because your loops (line 87/89) are always going up to 6 regardless whether or not there are days left to show. Why 6 not 7?

What are you trying to do on line 81? Considering that you just have to count from 1 to d?
Topic archived. No new replies allowed.