Display Days in a Month including leap year
Sep 20, 2012 at 4:12am UTC
I'm new to programming and am taking a class in school. Our assignment is to display the day in a month taking into account leap years. This is what I have so far and I keep getting an error for the bolded line.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
#include <iostream>
using namespace std;
int main ()
{
int month, year, days;
bool is_leap_year;
cout << "Please enter the four digit year: \n" ;
cin >> year;
cout << "Please enter the two digit month: \n" ;
cin >> month;
if ((month == 4) || (month == 6) || (month == 9) || (month == 11))
days = 30;
else if (month == 2)
{
is_leap_year = ((year%400) == 0) || (year% 4 == 0) && ((year%100) !== 0);
days = is_leap_year?
29:28;
}
else
days = 31;
return 0;
}
Any help would be great. Thanks
Sep 20, 2012 at 4:30am UTC
remove the semicolon after the if condition on line 13.
Also this all on one line:
days = is_leap_year ? 29 : 28;
Sep 20, 2012 at 4:52am UTC
checking for a condition like you are doing on line 16 normally doesn't usually return a result.
try this:
1 2
if (((year%400) == 0) || (year% 4 == 0) && ((year%100) !== 0))
is_leap_year = true ;
also
the use of the ternary operator is not correct. it should be :
(is_leap_year)?days = 29:days = 28;
Sep 20, 2012 at 6:00pm UTC
the use of the ternary operator is not correct.
His usage was entirely correct, although a little awkward with the line break.
Topic archived. No new replies allowed.