Need help with a class assignment.

I was assigned to make a console app that when debugged should ask for a month then ask for a year then it should determine whether it is a leap year or not. This is what I have at this point in time.

1
2
3
4
5
6
7
8
9
10
11
12
int year;
int monthNumber;

cout << "Please enter a month as an integer (Jan. = 1, Feb = 2, etc.): ";
cin >> monthNumber;
cout << "Please enter a year (4-digit integer): ";
cin >> year;

int remainder = year % 4;
switch (remainder) { 
    case 1: 
        break;


I also have to use the switch function to do so. I just can't figure out where to go from here. Any words of wisdom? In the end I am supposed to be able to enter "2" for February and then enter 2008, then it would put out 29 days for it is a leap year. Then if I were to enter "2" then enter 2009 it would output 28. Then just the regular 30-31 days for the rest of the months.
Be careful how you word things. Your assignment appears to be:

    “Print the number of days in a month, given the month and year.”

I recommend you write yourself a couple of functions. The first is:

    bool is_leap_year( int year )

Determining whether or not a year is a leap year in the Revised Gregorian system is a well-known formula. See https://www.geeksforgeeks.org/program-check-given-year-leap-year/

The second function is:

    int days_in_month( int month, int year )

This function should tell you the number of days in a month given a year. It should use the first function to compute the answer for February.

BTW. You are not "debugging" an application when you "run" or "execute" it.

Hope this helps.
Thanks BIG time. My terminology isn't on point yet lol. Almost halfway through the semester I don't know these things yet. When I learn em I'll think of you lmao! I also have been working on this through out the day and have gotten this so far if you wanna comment on this edit?


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
	int y;
	int m;

	cout << "Please enter a month as an integer (Jan. = 1, Feb. = 2, etc.): ";
	cin >> m;
	cout << "Please enter a year (4-digit integer): ";
	cin >> y;

	int remainder = m % 4;
	switch (remainder) { 
		case 1: 
			cout << "31 days" << endl;
			cin >> remainder;
			break;
		case 2:
			cout << "28 days" << endl;
			cin >> remainder;
			break;
		case 3:
			cout << "31 days" << endl;
			cin >> remainder;
			break;
		case 4:
			cout << "30 days" << endl;
			cin >> remainder;
			break;
		case 5:
			cout << "31 days" << endl;
			cin >> remainder;
			break;
		case 6:
			cout << "30 days" << endl;
			cin >> remainder;
			break;
		case 7:
			cout << "31 days" << endl;
			cin >> remainder;
			break;
		case 8:
			cout << "31 days" << endl;
			cin >> remainder;
			break;
		case 9:
			cout << "30 days" << endl;
			cin >> remainder;
			break;
		case 10:
			cout << "31 days" << endl;
			cin >> remainder;
			break;
		case 11:
			cout << "30 days" << endl;
			cin >> remainder;
			break;
		case 12:
			cout << "31 days" << endl;
			cin >> remainder;
			break;
Last edited on
Remember, computers cannot look ahead to see how things work. They only understand things one step at a time.

What is "remainder" here?

What is the range of values you can get when computing the remainder of division by 4?

What is the consequence of starting with January == 1?

There is a pattern here that has two twists:
Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec
31  2?  31  30  31  30  31  31  30  31  30  31
    ↑                       ↑
    February is weird       The pattern skips at August

You can write a very small function that handles this weirdness very easily:

    month 2 (February) is 28 + is leap year
    months 1..7 are 30 days + is odd
    months 8..12 are 30 days + is even

You do not need to input (cin) anything after your initial inputs for month and year.
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
int main() 
{

	int year;
	int month;

	cout << "Please enter a month as an integer (Jan. = 1, Feb. = 2, etc.): ";
	cin >> month;
	cout << "Please enter a year (4-digit integer): ";
	cin >> year;

	int remainder = month % 4;
	switch (month) {
			// 31 days.
		case 1:
		case 3:
		case 5:
		case 7:
		case 8:
		case 10:
		case 12: 
			cout << "31 days" << endl;
				break;
			// 30 days.
		case 4:
		case 6:
		case 9:
		case 11: 
			cout << "30 days" << endl;
				break;
			// February is special.
		case 2: 
			cout << "28 days" << endl;
				break;
			// Invalid value for month!
		default:
			cout << "29 days" << endl;
				break;
		}
	system("pause");
	return 0;


A lot of thing are of me using other people's work as a guide. Then my unknowing self I guess just tries to mesh it all together lol. I've got a little help from a class mate. This is the new and ?improved?

(disclaimer) I kinda just threw the 29 in down there cause I was trying to do some tinkering and it doesn't work the way I thought.
Last edited on
Topic archived. No new replies allowed.