Help on program to find days in month with leap years

Hello!

So I have a code here which determines whether a year is a leap year or not:

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

bool isLeapYear(int year);

int main()
{
    int year; 
    cout << "Enter a year: ";
    cin >> year;
    if (isLeapYear(year) == false)
    {
        cout << year << " is not a leap year.  ";
    }
    else
    {
        cout << year << " is a leap year. ";
    }
    
    return 0;
}

bool isLeapYear(int year)
{
    bool is_leap_year = false;
    if (year % 4 == 0)
    {
       is_leap_year = true;
    }
    if (year % 100 == 0)
    {
        is_leap_year = false;
    }
    if (year % 400 == 0)
    {
        is_leap_year = true;
    }
    return is_leap_year;
}


I want to make an output like this:

1
2
3
4
Enter a year: 2016
2016 is a leap year.
Enter a month number: 2
There are 29 days in February 2016


I need to use a function that returns the number of days in a given month in a given year, and a function that returns the name of a given month.

Could somebody help? Unsure how to go about this.

Thank you!
Last edited on
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
#include <iostream>
using namespace std;

int month;

bool isLeapYear(int year)
{
    bool is_leap_year = false;
    if (year % 4 == 0)
    {
       is_leap_year = true;
    }
    if (year % 100 == 0)
    {
        is_leap_year = false;
    }
    if (year % 400 == 0)
    {
        is_leap_year = true;
    }
    return is_leap_year;
}

int getNumberOfDays(int month, int year)
{
	if( month == 2)
	{
		if((year%400==0) || (year%4==0 && year%100!=0))	
			return 29;
		else	
			return 28;
	}
	else if(month == 1 || month == 3 || month == 5 || month == 7 || month == 8
	||month == 10 || month==12)	
		return 31;
	else 		
		return 30;
} 

int main()
{
    int year; 
    cout << "Enter a year: ";
    cin >> year;
    if (isLeapYear(year) == false)
    {
        cout << year << " is not a leap year.  ";
    }
    else
    {
        cout << year << " is a leap year. ";
    }
    
    cout << endl << "Enter a month number: ";
    cin >> month;
    
    int days=0;

    days = getNumberOfDays(month, year);

    cout << "There are " << days << " days in this month of " << year << ".";

    return 0;
}


Output:

1
2
3
4
Enter a year: 2016
2016 is a leap year.
Enter a month number: 2
There are 29 days in this month of 2016.


Just need to know now how to change "this month of" to "February".
Last edited on
1
2
3
4
5
6
7
#include <string>

std::string getMonthName(int month)
{
    static std::string months[12] = { "January", "February", "March", "and the rest" };
    return months[month - 1];
}
Last edited on
Edit: Solved, thanks for help.
Last edited on
Topic archived. No new replies allowed.