Issue with Monthly Calendar Assignment.

Hello to those reading. My assignment is as follows:

"Your job is to write a program that will display a calendar for any given month of a given year. The user will need to type the number of the month as in integer from 1 to 12 (1 is for January, etc.), and the year as a 4-digit integer."

My main issue is determining what day of the week the month the user desires to display starts. I was given an equation, D (0 = Sunday, 1 = Monday, 2 = Tuesday, .... 6 = Saturday) which is supposed to give values 0-6 which correspond to days in the week, in which the value of D should be the day of the week the month starts on.

Here is the following equation (please excuse me if it is hard to read at all, please tell me if it is and I will try to make it neater):

D = ([(26 * (M + 1)) / 10] + Y + [Y / 4] + [C / 4] + 5 * C) mod 7

Where
M = the month (3 = March; 4 = April; 5 = May; 6 = June; 7 = July; 8 = August; 9 = September; 10 = October; 11 = November; 12 = December; 13 = January; 14 = February)

C is the "century" (more accurately, the first 2 digits of the user input for year - e.g. if year = 2014, C = 20)

Y is the year within the century (last 2 digits of the user input for year - e.g. if year = 2014, Y = 14)

Square brackets mean that the value inside should be truncate (not rounded), so I suppose simply converting the value to an int would do - e.g. [2.8] -> 2

Below is the bulk of my CURRENT code (it will not be representative of the end goal, which is to display a calendar for a desired month/year). This code's main purpose right now is to display "D". If I can figure this out, I think the rest of the problem will be relatively easy.

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


void variablesForD(int, int, int, int, int);
int D(int, int, int);

using namespace std;

int main() {
	int year;
	int monthNumber;

	cout << "Please enter a month as an integer (3 = March; 4 = April; 5 = May; 6 = June; 7 = July; 8 = August; 9 = September; 10 = October; 11 = November; 12 = December; 13 = Janruary; 14 = February): ";
	cin >> monthNumber;
	cout << "Please enter a year (as a 4-digit integer): ";
	cin >> year;

	
	cout << setw(15) << D << endl;
	system("pause");

	return 0;
}



void variablesForD(int m, int y, int c, int monthNumber, int year) {
	m = monthNumber;
	c = (year - y) / 100; 
	y = year - 100 * c;


}
int D(int m, int y, int c) {
	
	int D;
	D = (((26 * (m + 1)) / 10) + y + (y / 4) + (c / 4) + (5 * c)) % 7;
	return D;

}


Basic plug-and-chug aside, to determine variables C and Y, I noticed year = (100 * C) + Y, so I did basic algebraic manipulation and set the equation to solve for C and Y respectively in the "variablesForD" function. I couldn't notice any syntax errors or anything wrong with the equation for D, but my output is rather weird. I noticed that the output has letters in it, though my ineptitude for programming means I have no idea how to interpret that. Here is an example of the code's output (using the case of March 2014) :

1
2
3
4
Please enter a month as an integer (3 = March; 4 = April; 5 = May; 6 = June; 7 = July; 8 = August; 9 = September; 10 = October; 11 = November; 12 = December; 13 = Janruary; 14 = February): 3
Please enter a year (as a 4-digit integer): 2014
       0099140B
Press any key to continue . . .


I can't figure this out on my own (due to my aforementioned ineptitude in programming). May someone give me some insight on how to fix this, or directions for where to look? Thank you in advance for reading this!
Last edited on
Hi,

Compiling the code, the following warnings were issued.

In function 'int main()': 20:22: warning: the address of 'int D(int, int, int)' will always evaluate as 'true' [-Waddress] In function 'void variablesForD(int, int, int, int, int)': 28:24: warning: parameter 'm' set but not used [-Wunused-but-set-parameter]


You need to call function D with arguments - specifically 3 int s

Some other things:

Supply variable names for your function parameters, make sure they are the same in the function definition, it is not strictly required, but is so much better if you do.

Don't have using namespace std; put std:: before each std thing, it's the best way, and that is what the expert coders all do.

Lines 11 and 12: initialise your variables at declaration, even though you get input for them straight away it's good practice - so many errors come from uninitialised variables.

Line 14: There is no need to have a massively long std::cout statement. If you really do want to have such a long output, you can build it up with successive std::cout statements:

1
2
3
std::cout << "Please enter a month as an integer (3 = March; 4 = April; 5 = May; 6 = June; 7 = July;"
std::cout << " 8 = August; 9 = September; 10 = October; 11 = November; 12 = December;"
std::cout << " 13 = Janruary; 14 = February): ";


Line 28: This function is void and none of the parameters are references or pointers, so it won't have any effect on the value of those variables in the calling scope, so make them references:

28
29
30
31
32
void variablesForD(int m&, int y&, int c&, int monthNumber, int year) {
	m = monthNumber;
	c = (year - y) / 100; 
	y = year - 100 * c;
}


Well I guess that was you intention, but you don't call the function and those variables don't exist in main()

Any way good Luck
Sorry to be a bother again, but after I applied the recommended changes, I am getting errors I've never previously encountered, so I really have no clue what to do. I spent around an hour trying to debug the new code but made virtually no progress. Here is the new code (I only applied changes which would affect the value for D - I will take your word about the std:: thing when I get this issue resolved, though)

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


void variablesForD(int&, int&, int&, int, int);
int D(int m, int y, int c, int d);

using namespace std;

int main() {
	int year=0;
	int monthNumber=0;
	int m=0, y=0, c=0, d=0;

	cout << "Please enter a month as an integer (3 = March; 4 = April; 5 = May; 6 = June; 7 = July; 8 = August; 9 = September; 10 = October; 11 = November; 12 = December; 13 = Janruary; 14 = February): ";
	cin >> monthNumber;
	cout << "Please enter a year (as a 4-digit integer): ";
	cin >> year;

	variablesForD(m, y, c, monthNumber, year);
	D(m, y, c, d);
	cout << setw(15) << D << endl;
	system("pause");

	return 0;
}



void variablesForD(int &m, int &y, int &c, int monthNumber, int year) {
	m = monthNumber;
	c = (year - y) / 100; 
	y = year - 100 * c;
	


}
int D(int m, int y, int c, int d) {
	
	
	d = (((26 * (m + 1)) / 10) + y + (y / 4) + (c / 4) + (5 * c)) % 7;
	return d;

}


What is happening is that when I try running the program, a blank black box appears and the source code appears to switch "modes" I guess? When I close the output window, the source is still in said mode, and only goes back to normal if I click continue. Again, I am not really sure how to interpret this.

Specific errors:

In the int D () function, there's some weird error/warning about the breakpoint not currently being hit. Here is the actual error (which only actually appears when trying to run the source and said source switches to that other "mode"):

"The breakpoint will currently not be hit. No executable code of the target's code is associated with this line. Possible causes include: conditional compilation, compiler optimizations, or the target architecture of this line is not supported by the debugger code type. "


I am very bad with functions in general with C++. My teacher doesn't actually have a lecture - we just have a lab and then are assigned homework, and are given a short video lecture that basically tells us what a function is in C++, so I basically lack any degree of refinement in my knowledge of how functions work in C++. Again, sorry to bother you with my troubles, but I genuinely can't figure it out by myself.
closed account (48T7M4Gy)
Your code runs here!

Please enter a month as an integer (3 = March; 4 = April; 5 = May; 6 = June; 7 = July; 8 = August; 9 = September; 10 = October; 11 = November; 12 = December; 13 = Janruary; 14 = February): 3
Please enter a year (as a 4-digit integer): 2012
              1
 
Exit code: 0 (normal program termination)


Which IDE are you using. It sounds like VStudio
Last edited on
I am using VisualStudio 2015. It makes me feel very relieved that you managed to run the code, though, as this is the only hard part of the problem.

However, a new problem exists now. For March 2012, the month begins on a Thursday (i.e. "4") and not a Monday (which is represented by "1"). This is probably a simple syntax error where something probably rounded instead of being truncated. Unfortunately, it is a requirement that I have to use Visual Studio, so I have to worry about making my code compatible with visual studio before I do anything else. As I have previously stated, I have no clue how to do that. Your reply was much appreciated though, kemort.
Hi,

21
22
23
	D(m, y, c, d);
	cout << setw(15) << D(m, y, c, d) << endl;
	system("pause");


So this will print your starting number, it seems to work for Oct 2015 any way :+)

Are you running the program in debug mode, as opposed to the normal run mode?

When I try to run it with that change, I get the same problem. Also, since you mentioned debug mode, that "mode" I mentioned is just debug mode. Apparently reading a header is too hard for me. Anyway, I really have no clue how to fix this issue, but based on what I've been told it seems the main issue is technical issues regarding visual studio and not necessarily my code. I will try to run my program on something other than visual studio to verify this, and I will report on any other errors I can find with the code.
UPDATE: I ran the code in C++ Shell and it works perfectly. It also correctly displays "Thursday" for the first day of the month for March 2012, so there is apparently no actual error with the equation itself. There aren't any issues at all. If I can resolve the issue regarding compatibility with visual studio 2015, then I can do the rest of the problem on my own. Either way, all of your help is much appreciated - this is going better than expected.
closed account (48T7M4Gy)
But don't forget to correct the line as above:
cout << setw(15) << D(m, y, c, d) << endl;

That has to be done.
I will try to run my program on something other than visual studio to verify this, and I will report on any other errors I can find with the code.


We have compiled and run the code here, using the gear icon at the top right of your code posting. And it seems to work.

If you fix up the std::cout statements, the gear icon will be easier to see

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


void variablesForD(int&, int&, int&, int, int);
int D(int m, int y, int c, int d);

//using namespace std;

int main() {
	int year=0;
	int monthNumber=0;
	int m=0, y=0, c=0, d=0;

	std::cout << "Please enter a month as an integer (3 = March; 4 = April; 5 = May; 6 = June; 7 = July;\n"; 
	std::cout << "8 = August; 9 = September; 10 = October; 11 = November; 12 = December;\n";
	std::cout << "13 = Janruary; 14 = February): \n";
	
	std::cin >> monthNumber;
	std::cout << "Please enter a year (as a 4-digit integer): \n";
	std::cin >> year;

	variablesForD(m, y, c, monthNumber, year);
	
	std::cout << std::setw(15) << D(m, y, c, d) << "\n";
	system("pause");

	return 0;
}



void variablesForD(int &m, int &y, int &c, int monthNumber, int year) {
	m = monthNumber;
	c = (year - y) / 100; 
	y = year - 100 * c;
	


}
int D(int m, int y, int c, int d) {
	
	
	d = (((26 * (m + 1)) / 10) + y + (y / 4) + (c / 4) + (5 * c)) % 7;
	return d;

}


Please enter a month as an integer (3 = March; 4 = April; 5 = May; 6 = June; 7 = July; 8 = August; 9 = September; 10 = October; 11 = November; 12 = December; 13 = Janruary; 14 = February): 10 Please enter a year (as a 4-digit integer): 2015 
4


Edit:

For some reason the output isn't showing the same as what it does on cpp.sh
Last edited on
closed account (48T7M4Gy)
With VS you should be able to see in the toolbar a red square.

You need to press this stop button to reset the program otherwise system("pause") gets fouled up.
closed account (48T7M4Gy)
Also breakpoints appear as red circles on the left hand end of a line near the line number. It's easy to accidentally turn one on and the breakpoint makes the program stop at that point. You delete them by going to the Debug menu or right mouse button on the breakpoint symbol and select delete.

Breakpoints are useful debugging tools believe it or not.
closed account (48T7M4Gy)
Have you studied arrays yet because there is a nice way to convert the day number to an actual day? (OOPs maybe that's what you plan to do anyway)
Last edited on
Allow me to be more descriptive. What happens in Visual Studio 2015 is when I try to run the program (and yes, this is with the correction of replacing ""cout << D"" with ""cout << D(m, y, c, d)""), it automatically goes to debug mode. While in debug mode, the output window (i.e. the black box that opens up) is totally blank. Clicking the red "stop" icon to make it stop debugging automatically closes the output window. So I guess the newly diagnosed problem is that I can't get Visual Studio 2015 to run the program without it automatically going to debug mode for some reason.


EDIT: I found an option to "start without debugging" and the program works fine that way. I suppose the problem is technically solved at this point, but I would really like to know exactly why it automatically goes to debug mode, if possible. Thank you all for the assistance. You were all incredibly helpful.
Last edited on
IIRC there is a run without debug option somewhere.
closed account (48T7M4Gy)
I'm running your program on VS2015 in debug mode with no trouble. As long as you have system(pause) it should be OK. Maybe a clean and rebuild?
Hello again. Done with classes for the day and am very close to solving the problem (as far as I can tell, anyway.) I will show the code below and discuss the specific issue (P.S. I am being a little lazy about appending "std::" where appropriate, so I am still using "using namespace (std)" - I will make sure I make that change though):

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

#include <iostream>
#include <iomanip>
#include <cstdlib>


void variablesForD(int&, int&, int&, int, int);
int D(int m, int y, int c, int d);
int numOfDaysInMonth(int m);
void skip(int);
void printHeader(int);
void skipToDay(int);
void printMonth(int numDays, int weekDay);
void skipToDay(int b);

using namespace std;

int main() {
	int year=0;
	int monthNumber=0;
	int m=0, y=0, c=0, d=0;
	int numDays;
	int firstDayOfGivenMonth;

	firstDayOfGivenMonth = D(m, y, c, d);

	cout << "Please enter a month as an integer (3 = March; 4 = April; 5 = May; 6 = June; 7 = July; 8 = August; 9 = September; 10 = October; 11 = November; 12 = December; 13 = Janruary; 14 = February): ";
	cin >> monthNumber;
	cout << "Please enter a year (as a 4-digit integer): ";
	cin >> year;

	variablesForD(m, y, c, monthNumber, year);

	if (monthNumber >= 3 && monthNumber <= 14) {
		numDays = numOfDaysInMonth(m);
		printHeader(m);
		printMonth(numDays, firstDayOfGivenMonth);
		cout << endl << endl;
	}

	cout << setw(15) << D(m, y, c, d) << endl;
	system("pause");

	return 0;
}



void variablesForD(int &m, int &y, int &c, int monthNumber, int year) {
	m = monthNumber;
	c = (year - y) / 100; 
	y = year - 100 * c;
	


}
int D(int m, int y, int c, int d) {
	
	
	d = (((26 * (m + 1)) / 10) + y + (y / 4) + (c / 4) + (5 * c)) % 7;
	return d;

}
int numOfDaysInMonth(int m)
{
	if (m == 13) return(31);
	else if (m == 14) return(28);
	else if (m == 3) return(31);
	else if (m == 4) return(30);
	else if (m == 5) return(31);
	else if (m == 6) return(30);
	else if (m == 7) return(31);
	else if (m == 8) return(31);
	else if (m == 9) return(30);
	else if (m == 10) return(31);
	else if (m == 11) return(30);
	else if (m == 12) return(31);

	

}

void printHeader(int m)
{
	if (m == 13)
	{
		skip(7);
		cout << "January" << endl;
	}
	else if (m == 14) { skip(7); cout << "February" << endl; }
	else if (m == 3) { skip(7); cout << "March" << endl; }
	else if (m == 4) { skip(7); cout << "April" << endl; }
	else if (m == 5) { skip(7); cout << "May" << endl; }
	else if (m == 6) { skip(7); cout << "June" << endl; }
	else if (m == 7) { skip(7); cout << "July" << endl; }
	else if (m == 8) { skip(7); cout << "August" << endl; }
	else if (m == 9) { skip(7); cout << "September" << endl; }
	else if (m == 10) { skip(7); cout << "October" << endl; }
	else if (m == 11) { skip(7); cout << "November" << endl; }
	else if (m == 12) { skip(7); cout << "December" << endl; }


	cout << " S  M  T  W  T  F  S" << endl;
	cout << "____________________" << endl;
}

void skip(int i)
{
	while (i > 0)
	{
		cout << " ";
		i = i - 1;
	}
}

void printMonth(int numDays, int weekDay)
{
	int day = 1;
	skipToDay(weekDay);
	while (day <= numDays)
	{
		cout << setw(2) << day << " ";
		if (weekDay == 6)
		{
			cout << endl;
			weekDay = 0;
		}
		else weekDay = weekDay + 1;
		day = day + 1;
	}
}

void skipToDay(int b)
{
	return skip(3 * b);
}


The program runs fine except for one problem - I am having difficulty getting the number of spaces to represent the day of the week it corresponds to (e.g. if D = 6, "1", as in day 1 of user input for month, should fall under Saturday (the right S.)) The program always has it start on Tuesday for any given value. Granted this is probably not that difficult of an issue to debug, but I'm slightly braindead at this point so any further assistance would be greatly appreciated.
closed account (48T7M4Gy)
print D % 7 spaces then the date?
Topic archived. No new replies allowed.