Number of days in the month

So I have to code a program that will output how many days are in the month not excluding leaper year. I have placed the code below, and I unable to find the error, I am getting an error of "expression must be a modifiable lvalue". Any help would be appreciated.

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
#include <iostream>
using namespace std;

int main()
{
	cout << "Enter a Date to obtain how many days are in a month:" << endl;
	int year = 0;
	int month = 0;
	int days;
	cout << "Enter Year: ";
	cin >> year;
	cout << "Enter Month: ";
	cin >> month;

	
	if (month == 4 || month == 6 || month == 9 || month = 11)
		days = 30;
		
	else if (month == 02)
	{
		bool leapyear = (year % 4 == 0 && year % 100 != 0) || (year % 400 == 0);

		if (leapyear == 0)
				days = 29;
		else 
				days = 29;
	}

	else 
	days = 31;

return days;
}
Line 16: needs to be 'month == 11' instead of 'month = 11'
Also, line 24 needs to be

days = 28;

otherwise every February has 29 days!
The error message should give you the number of the line on which the error occurs.

I can't immediately spot where that particular error is. I'm guessing it might have something to do with the way precedences are resolved in line 21, but I may be wrong. If you use more parentheses to enforce the precedence that you actually intend, then it might fix the problem. But, as I say, I'm just guessing.

I've noticed some other problems with your code:

At line 16, the month = 11 clause is, presumably, a typo for month == 11. This will cause that if condition to always evaluate to true, regardless of the initial value of month.

At line 19, using a leading in 02 instructs the compiler to consider this an octal value, rather than a decimal one. In this case, it doesn't cause a problem, as 2 in octal is the same as 2 in decimal, but it's worth taking care over it.

At line 23, it would be more idiomatic to compare a bool variable to true or false:

if (leapyear == false)

but most programmers would find the following even more idiomatic:

if (!leapyear)

Presumably line 24 is intended to be setting the number of days to 28, not 29?

EDIT: It's normal to have main return 0 for success, and a non-zero code for failure. Did you intend to write the number of days out to the standard output, rather than returning it?
Last edited on
Topic archived. No new replies allowed.