Problem with array and calendar

Write your question here.

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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
 #include <iostream>
#include <conio.h>
#include <iomanip>
#include <string>

using namespace std;

int getDays(int);
string getMonthName(int);


int getDays(int m)
{
	int days = 0;

	switch (m)
	{
	case 1:
	case 3:
	case 5:
	case 7:
	case 8:
	case 10:
	case 12:
		days = 31;
		break;
	
	case 4:
	case 6:
	case 9:
	case 11:
		days = 30;
		break;

	case 2:
		days = 28;
		break;
	}

	return days;
}

string getMonthName(int m)
{
	string name = "";

	switch (m)
	{
	case 1:
		name = "January";
		break;
	case 2:
		name = "February";
		break;
	case 3:
		name = "March";
		break;
	case 4:
		name = "April";
		break;
	case 5:
		name = "May";
		break;
	case 6:
		name = "June";
		break;
	case 7:
		name = "July";
		break;
	case 8:
		name = "August";
		break;
	case 9:
		name = "September";
		break;
	case 10:
		name = "October";
		break;
	case 11:
		name = "November";
		break;
	case 12:
		name = "December";
		break;
	}

	return name;
}

void main(void)
{
	int row = 0, col, cal[6][7] = {0}, d, month, start = 1, days;
	char ans = 'n';
	string monthname;
	

	cout << "Which month do you want a calendar for? ( 1 = Jan, 2 = Feb. etc...) ";
	cin >> month;

	cout << "What day of the week does Jan. 1st occur on? ( 0 = Sun, 1 = Mon, etc...) ";
	cin >> col;

	cout << "Is this year a leap year? (Y/N) ";
	cin >> ans;

while(start <= month)	
{   
	
	if (ans == 'y' && start == 2 || ans == 'Y' && start == 2)
		days = 29;
	else
		days = getDays(start);

	for (d = 1; d <= days; d++)
		{
			cal[row][col] = d;
			col++;
		
			if (col == 7)
			{
				row++;
				col = 0;
			}
			

		}
	
		start++;

	if(start <= month)
		row = 0;
		
	
}

	monthname = getMonthName(month);
	for (int i = 1; i <= (21 - monthname.length()) / 2; i++)
		cout << " ";
	cout << monthname << endl;
	cout << " Su Mo Tu We Th Fr Sa" << endl;
	for (row = 0; row <= 5; row++)
	{
		for (col = 0; col <= 6; col++)
			if (cal[row][col] == 0)
				cout << setw(3) << " ";
			else
				cout << setw(3) << cal[row][col];
		cout << endl;
	}

	_getch();
}


I have to write a program that displays a calendar for a single month. The program asks three questions: What month you want, when Jan. 31 occurs in the week, and whether or not it is a leap year. For the most part it seems to be working ok, but I see two issues (maybe one causing two problems) First of all, it leaves the data from the previous month in the elements that aren't used in the month you want. For example, if you ask for February, it prints the calendar correctly but will show days 1,2, and 3, from January because they were written to those elements prior and not overwritten with a 0, which the program needs to see to recognize to print a space there. It also seems to be getting off on it's days later in the calendar, and I'm not sure if this is a related problem or not. Can anybody point me in the right direction as to where the problem is? I've been suspecting lines 119-136, but I'm not sure what the issue is. The program does retrieve the proper number of days through each iteration of the while loop because I've monitored that and its working just fine. Any help is appreciated. Thanks.
Last edited on
I also decided to try and reinitialize the array to 0 before each pass through the for loop, but for some reason I then get nothing inside of the final calendar. I was using:

int cal[6][7] = {0};

I put this at the beginning of the while loop so that it would run it each time it passed through but that's not working.
Anyone? I'm really at a loss as to why this isn't working correctly.
closed account (48T7M4Gy)
Which month do you want a calendar for? ( 1 = Jan, 2 = Feb. etc...) 2
What day of the week does Jan. 1st occur on? ( 0 = Sun, 1 = Mon, etc...) 1
Is this year a leap year? (Y/N) y
      February
 Su Mo Tu We Th Fr Sa
     1  2  3  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


This is what I got running your program. The input isn't accurate. But the output looks OK to me.

To get a specific answer to your problem you'll have to be specific and perhaps show us the output. :)
It's right there in what you posted. Look at the first week of that month. It's 1,2,3,1,2,3.

The first 1,2,3 is what's left in the array elements from when went through January on its count. This gets worse as you go later in the requested months because more and more of the array elements have been written to along the way.


I'm trying to figure out how to get rid of that. I thought I could reinitialize the entire array to 0 before each pass of the while loop, but when I do that, I get nothing in the output except the month and the days of the week, no dates

int cal[6][7] = {0} which should work to initialize the array to 0. I think it's more about the placement of it, but no matter where I try to place it it either does the same thing, or it does nothing at all.
Last edited on
I figured it out. I used memset(cal, 0, sizeof(cal)); at the first step in the while loop and it's working perfectly now.
Topic archived. No new replies allowed.