Calendar Program

I have to make a code for a calendar to show any date that the user asks for and then when the user gives the code the information that it asks for it should print out the correct calendar. But everytime that I try to make the code work, it does not work and prints the wrong calendar.

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
#include <iostream>
#include <iomanip>

using namespace std;

int cal;

int getMonth()
{
	int month;
	cout << "Enter A Month Number: ";
	cin >> month;

	while (month < 1 or month > 12)
	{
		cout << "Must Be Between 1 and 12: " << endl;
		cout << "Enter A Month Number: ";
		cin >> month;
	}
	return month;
}

int getYear()
{
	int year;
	cout << "Enter A Year: ";
	cin >> year;

	while (year < 1940)
	{
		cout << "Year Must be 1940 or later: " << endl;
		cout << "Enter A Year: ";
		cin >> year;
	}
	return year;
}

void monthset(int month, int year)
{
	if (month == 1)
		cout << "January, " << year << endl;
	if (month == 2)
		cout << "February, " << year << endl;
	if (month == 3)
		cout << "March, " << year << endl;
	if (month == 4)
		cout << "April, " << year << endl;
	if (month == 5)
		cout << "May, " << year << endl;
	if (month == 6)
		cout << "June, " << year << endl;
	if (month == 7)
		cout << "July, " << year << endl;
	if (month == 8)
		cout << "August, " << year << endl;
	if (month == 9)
		cout << "September, " << year << endl;
	if (month == 10)
		cout << "October, " << year << endl;
	if (month == 11)
		cout << "November, " << year << endl;
	if (month == 12)
		cout << "December, " << year << endl;
}

int getDay()
{
	int day;
	cout << "Enter A Day: ";
	cin >> day;

	while (day < 1 or day > 31)
	{
		cout << "Please Enter A Day that is either 28, 30, 31" << endl;
		cout << "Enter A Day: ";
		cin >> day;
	}
	return day;
}

void display(int month, int days, int year)
{
	cout << "  Su  Mo  Tu  We  Th  Fr  Sa" << endl;

	int pos = 1;

	month = (month + 0) % 7;

	for (int s = 0; s < month; s++, pos++)
	{
		cout << "    ";
	}

	for (int j = 1; j <= days; j++, pos++)
	{

		if (pos % 8 == 0)
		{
			pos = 1;
			cout << endl;
		}
		cout << setw(4) << j;

	}

	cout << endl;
}

int main()
{
	// Get Month
	int month = getMonth();
	// Get Year
	int year = getYear();
	// Get Offset
	monthset(month, year);
	// Get days
	int days = getDay();
	// Get display
	display(month, days, year);
	cin >> cal;

	return cal;
}


It will not output the correct calendar when given the information from the user. Is there something that I am doing wrong?

'Project2.exe' (Win32): Loaded 'C:\Users\Sri\Documents\Project 2\Project2\Debug\Project2.exe'. Symbols loaded.
'Project2.exe' (Win32): Loaded 'C:\Windows\SysWOW64\ntdll.dll'. Cannot find or open the PDB file.
'Project2.exe' (Win32): Loaded 'C:\Windows\SysWOW64\kernel32.dll'. Cannot find or open the PDB file.
'Project2.exe' (Win32): Loaded 'C:\Windows\SysWOW64\KernelBase.dll'. Cannot find or open the PDB file.
'Project2.exe' (Win32): Loaded 'C:\Windows\SysWOW64\msvcp140d.dll'. Cannot find or open the PDB file.
'Project2.exe' (Win32): Loaded 'C:\Windows\SysWOW64\vcruntime140d.dll'. Cannot find or open the PDB file.
'Project2.exe' (Win32): Loaded 'C:\Windows\SysWOW64\ucrtbased.dll'. Cannot find or open the PDB file.
The thread 0x40d0 has exited with code -1073741510 (0xc000013a).
The thread 0x22a0 has exited with code -1073741510 (0xc000013a).
The thread 0x2c8 has exited with code -1073741749 (0xc000004b).
The program '[8996] Project2.exe' has exited with code -1073741510 (0xc000013a).
In c++ there is a symbol for "or" which is "||" the key for this vertical line is right under the backspace and hold shift.

Also for "and" the symbol is "&&" you don't use it in this code but might be helpful later on.
DJFlash, please tell us what input you are giving the program, and exactly what the output should be. Note that you never use year in your display function. Not sure if intended.

month = (month + 0) % 7;
I suggest choosing better names here, perhaps something like "int starting_position = month % 7;"

Also, MrRobot, "or" and "and" are both acceptable in C++. That isn't the source any problems.
Last edited on
Ganado, the input is whatever the user enters when prompted to give the code a number for the month, year and day to determine what is the beginning day start and then shows the calendar to the user with the correct date. But whenever i try to do that it does not work and I do not know why it is not working

'Project2.exe' (Win32): Loaded 'C:\Users\Sri\Documents\Project 2\Project2\Debug\Project2.exe'. Symbols loaded.
'Project2.exe' (Win32): Loaded 'C:\Windows\SysWOW64\ntdll.dll'. Cannot find or open the PDB file.
'Project2.exe' (Win32): Loaded 'C:\Windows\SysWOW64\kernel32.dll'. Cannot find or open the PDB file.
'Project2.exe' (Win32): Loaded 'C:\Windows\SysWOW64\KernelBase.dll'. Cannot find or open the PDB file.
'Project2.exe' (Win32): Loaded 'C:\Windows\SysWOW64\msvcp140d.dll'. Cannot find or open the PDB file.
'Project2.exe' (Win32): Loaded 'C:\Windows\SysWOW64\vcruntime140d.dll'. Cannot find or open the PDB file.
'Project2.exe' (Win32): Loaded 'C:\Windows\SysWOW64\ucrtbased.dll'. Cannot find or open the PDB file.
'Project2.exe' (Win32): Loaded 'C:\Windows\SysWOW64\kernel.appcore.dll'. Cannot find or open the PDB file.
'Project2.exe' (Win32): Loaded 'C:\Windows\SysWOW64\msvcrt.dll'. Cannot find or open the PDB file.
'Project2.exe' (Win32): Loaded 'C:\Windows\SysWOW64\rpcrt4.dll'. Cannot find or open the PDB file.
'Project2.exe' (Win32): Loaded 'C:\Windows\SysWOW64\sspicli.dll'. Cannot find or open the PDB file.
'Project2.exe' (Win32): Loaded 'C:\Windows\SysWOW64\cryptbase.dll'. Cannot find or open the PDB file.
'Project2.exe' (Win32): Loaded 'C:\Windows\SysWOW64\bcryptprimitives.dll'. Cannot find or open the PDB file.
'Project2.exe' (Win32): Loaded 'C:\Windows\SysWOW64\sechost.dll'. Cannot find or open the PDB file.
The thread 0xcdc has exited with code -1073741510 (0xc000013a).
The thread 0x63ec has exited with code -1073741510 (0xc000013a).
The thread 0x3a98 has exited with code -1073741510 (0xc000013a).
The thread 0x3860 has exited with code -1073741510 (0xc000013a).
The program '[25288] Project2.exe' has exited with code -1073741510 (0xc000013a).
Last edited on
First, as long as your program is running, I think the messages about not loading PDB files are not important. The latter messages seem to be imply that you are exiting out of your program early.

You're not explaining what "not working means". I am not psychic. Is your program crashing? Is it not outputting any text?
I don't know what's happening.
Reread what I said.

Tell me exactly what you are inputting, what the result you're getting is, and what result SHOULD be. Be specific.

This is what prints for me:
Enter A Month Number: 3
Enter A Year: 1990
March, 1990
Enter A Day: 15
  Su  Mo  Tu  We  Th  Fr  Sa
               1   2   3   4
   5   6   7   8   9  10  11
  12  13  14  15
 

One issue I see is that March 1, 1990 started on a Thursday, not Wednesday.
https://www.timeanddate.com/calendar/monthly.html?month=3&year=1990&country=1
Your display function does not take into account the year, there's no way it can know what the correct day of the week it is.
http://mathforum.org/dr.math/faq/faq.calendar.html
https://cs.uwaterloo.ca/~alopez-o/math-faq/node73.html
Last edited on
When you run the program, it will ask for a month and year and then it will show a calendar of that month and year that you give to the program. But my problem is that the first week it is not in the right position. It is shifted to the right by a tab, and then the rest of the weeks it is in the correct positions. I have tried with different dates and different combinations and it keeps showing the first week like it is tabbed.
I don't see anywhere in your program where you determine the proper number of days to skip for the first week. Simply indenting by the month mod 7 is arbitrary.
month = month + 0 is a hint it's supposed to be month = month + day.
day calculation: day = (((year - 1) * 365) + floor(year - 1) / 4 - floor(year - 1) / 100 + floor(year - 1) / 400) + 1 % 7;

Why are you asking for the days, by the way?

Using the month and the year (using the year you can verify if it's a leap year) calculate number of days in the month (switch statement, you know your months don't you).

Don't know what you want to do with cin >> cal; and return cal; But with return cal; you are returning to the operating system, mind you, you must always return 0 with the main.
Last edited on
You don't need the floor calls if year is an integer. And the + 1 should be inside the final paren.

1
2
3
4
5
// 0 is Sunday, 6 is Saturday
int start_day_of_year(int year) {
    --year;
    return (year * 365 + year / 4 - year / 100 + year / 400 + 1) % 7;
}

That's just where Jan 1 starts. For other months the number of days to it's start need to be taken into account.
Last edited on
See this neatly-written program from @Duthomhas :
http://michael-thomas-greer.com/code/portfolio/calendar.cpp

@Duthomhas's program is Boost licensed.
Last edited on
Thank you all for these suggestions but unfortunately none of them have helped me get the calendar correct.
That's because you never gave concrete steps to actually reproduce your problem. The link that mbozzi posted, by Duthomhas, is basically is a fleshed-out, completed version of your program, with correct starting days for each month. Have you actually tried running it? I'm not sure why I'm even trying. Seems like everyone above is guessing as to what exactly your issue is.
Here is what is happening whenever I am running the code. Whenever I run the code it asks the user for the month and year that the user asks for and then it produces a calendar where the dates are wrong and the calendar is being shifted to the right as if you were to hit tab and it looks like I failed as a student and not creating the simple calendar that the teacher gave us 2 weeks to do.
So, do you not understand why it's shifting the first row to the right?
It's shifting the first row to the right because of this code right here
1
2
3
4
5
6
	month = (month + 0) % 7;

	for (int s = 0; s < month; s++, pos++)
	{
		cout << "    ";
	}

I don't just want to give the answer away, but you have to make the number of shifts depend on more than just the month % 7. The number of shifts is equivalent to saying what day of the week you want the month to start on. For example, November 2018 started on a Thursday, so this means that 2018-11-01 was the 5th day of the week.

See the code that mbozzi posted -- the day_of_week function.

I don't endorse Duthomhas' braces indentation style :))
Last edited on
And another problem that I was having when I was making this code was that we had to make sure that the code found what was the first day of the month located on, aka Monday Tuesday etc..., and that code miserably failed and I could not see why it was not working but I was trying to complete the code so that at least I had the framework of the calendar
I could not see why it was not working but I was trying to complete the code so that at least I had the framework of the calendar

This is certainly a major part of why this assignment was difficult.

The key to fixing an incorrect program is to attack problems one at a time. Because new code is always broken, it's absolute madness to continually pile new untested code atop broken components in the hope of fixing everything later.

Test your code continually, as you write it; fix visible errors as you make them.
Didn't you use tpb's snippet to calculate which day the month started with? Why do you say that it is failing in calculating the first day of the month?

From there it's just about displaying the calendar properly, i.e adjusting spaces. You've got everything else done (assuming you fixed the mistakes pointed out earlier).
Topic archived. No new replies allowed.