#include <iostream>
#include <conio.h>
#include <iomanip>
#include <string>
usingnamespace 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.
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.
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.