Calculating Days in a Month (Need help)

I could use help with an assignment I'm trying to complete. The goal is to get an output like this

Enter a month (1-12): 2
Enter a year: 2016
29 days

however, I'm new t programming and I can't piece together what exactly I'm missing or did wrong. Any and all assistance is appreciated.

#include<iostream>
using namespace std;

int main()
{
cout <<"Enter a month and a year " << endl;
int year = 0;
int month = 0;
int days;
cout << "Enter a month: ";
cin >> month;
cout << "Enter a year: ";
cin >> year;
if (month == 4) || month == 6 || month == 9 || month == 11)
days = 30;

else if (month == 02)
{
bool leapyear = ((year%400==0) || ((year%100!=0)&&(year%4==0)));
if leapyear==0
days = 28;
else
days = 29;
}
else
days = 31;

return days;
Last edited on
Can you explain a bit more what the issue is that you are having?
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
#include <iostream>

int main()
{
    // get the month from the user
    int month = 0;
    std::cout << "Enter a month[1-12]: " ;
    std::cin >> month;

    // sanity check that we have a valid month
    if( month < 1 || month > 12 )
    {
        std::cout << "invalid month\n" ;
        return 1 ; // return without doing anything more
    }

    int days = 31; // all months have 31 days

    // except that these months have 30 days
    if( (month == 4) || (month == 6) || (month == 9) || (month == 11) ) days = 30 ;

    // and february has either 28 or 29 days
    else if( month == 2 )
    {
        // february, so we need to check if it is a leap year
        int year ;
        std::cout << "Enter the year: " ;
        std::cin >> year ;

        const bool leapyear = ( (year%400==0) || ( (year%100!=0) && (year%4==0) ) );
        if(leapyear) days = 29 ;
        else days = 28 ;
    }

    // print out the computed number of days
    std::cout << days << " days.\n" ;
}
I meant to say this when you first posted, but thank you very much.
Topic archived. No new replies allowed.