int main()
{
    switch (month) 
    {
    case 1:
        days = 31;
        break;
    case 2:
        days = 28;
        break;
    case 3:
        days = 31;
        break;
    }
}

1 Answers

2
JeJo On

The simplest way of doing by having an array of Days where the array indexing is corresponding to month-1.

See a demo

#include <iostream>
#include <array>

int main() 
{
    using Day = int;
    const std::array<Day, 3> days{ 31 , 28, 31};

    const auto findDay = [&](const std::size_t month) /* noexcept -> const std::size_t*/  
    {
        if (1 <= month && month <= days.size()) return days[month - 1];
        std::cout << "Invalid month\n"; // if not found
        return 0;
    };

    std::cout << findDay(1) << '\n';
    std::cout << findDay(2) << '\n';
    std::cout << findDay(3) << '\n';
    std::cout << findDay(0) << '\n';
}

Output:

31
28
31
Invalid month
0