Not returning how many days are in the month

When I use my switch function to try and return the number of days in a given month I don't see anything.

If I enter a invalid month number I do see it say Invalid, not sure why I can't see it return the number of days though.

Thanks for looking at it.

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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
#include <iostream>
using namespace std;

class date
{
	public:
		void setDate(int m, int d, int y);
		int daysOfMonths(int m, int y);
		void printDate() const;
		void printLongDate() const;
		bool leapYear(int y) const;
		bool dateValidation();
		date (int m = 1, int d = 1, int y = 1990);
	private:
		int month;
		int day;
		int year;
		int days;
};

int main()
{
	date calendar;
	int m1, d1, y1;

	cout << "Please enter a date: ";
	cin >> m1 >> d1 >> y1;

	calendar.setDate(m1, d1, y1);

    calendar.leapYear(y1);

	calendar.dateValidation();

	calendar.printDate();
	calendar.printLongDate();

	cout << "Days in the month: ";
	calendar.daysOfMonths(m1, y1);

	// your code goes here
	return 0;
}

//Class Object that sets the date
void date::setDate(int m, int d, int y)
{

   	if (m >= 1 && m <= 12)
		month = m;
	else
		month = 1;

	if (d >= 1 && d <= 31)
		day = d;
	else
		day = 1;

    if (y >= 1582)
		year = y;
	else
		year = 1990;

}

int date::daysOfMonths(int m, int y)
{
    switch (m)
    {
        case 1:
        case 3:
        case 5:
        case 7:
        case 8:
        case 10:
        case 12:
            days = 31;
            return days;
            break;
        case 4:
        case 6:
        case 9:
        case 11:
            days = 30;
            return days;
            break;
        case 2:
            if(leapYear(y))
            {
                days = 28;
                return days;
            }
            else
                days = 29;
                return days;
            break;
        default:
            cout << "Invalid." << endl;
            break;
    }
}

bool date::leapYear(int y) const
{
	bool leapYear = (y % 4 == 0 && y % 100 !=0) || (y % 400 == 0);

   	if(leapYear)
   		return true;
   	else
   		return false;
}

bool date::dateValidation()
{
  if (! (1 >= month && month<=12) )
     return false;
  if (! (1<= day && day<=31) )
     return false;
  if ( (day==31) && (month==2 || month==4 || month==6 || month==9 || month==11) )
     return false;
  if ( (day==30) && (month==2) )
     return false;
  if ( (month==2) && (day==29) && (year%4!=0) )
     return false;
  if ( (month==2) && (day==29) && (year%400==0) )
     return true;
  if ( (month==2) && (day==29) && (year%100==0) )
     return false;
  if ( (month==2) && (day==29) && (year%4==0)  )
     return true;


}

//Class Object that prints
void date::printDate() const
{
	if(month <= 9 && day <= 9)
		cout << "The date you entered is: " << "0" << month << "-" << "0" << day << "-" << year;
	else if (month <= 9 && day >= 9)
		cout << "The date you entered is: " << "0" << month << "-" << day << "-" << year;
	else if (month >= 10 && day <=9)
		cout << "The date you entered is: " << month << "-0"  << day << "-" << year;
	else
	cout << "The date you entered is: " << month << "-" << day << "-" << year;
	cout << endl;

}

void date::printLongDate() const
{
    cout << "You have entered ";
    if(month <= 1)
        cout << "January " << day << ", " << year;
    else if(month <= 2)
        cout << "February " << day << ", " << year;
    else if(month == 3)
        cout << "March " << day << ", " << year;
    else if(month == 4)
        cout << "April " << day << ", " << year;
    else if(month == 5)
        cout << "May " << day << ", " << year;
    else if(month == 6)
        cout << "June " << day << ", " << year;
    else if(month == 7)
        cout << "July " << day << ", " << year;
    else if(month == 8)
        cout << "August " << day << ", " << year;
    else if(month == 9)
        cout << "September " << day << ", " << year;
    else if(month == 10)
        cout << "October " << day << ", " << year;
    else if(month == 11)
        cout << "November " << day << ", " << year;
    else
        cout << "December " << day << ", " << year;
    cout << endl;
}

//Constructor with parameters
date::date (int m, int d, int y)
{
	month = m;
	day = d;
	year = y;
}
Change line 38: cout << "Days in the month: " <<;
Awesome, thanks a ton :)
A few items:

1. What is the point of having the days member in your date class? It appears to only be used in int date::daysOfMonths(int m, int y). Every date object you instantiate will have its own days member. You do not even need to define a variable to hold the return value in daysOfMonths(). In the switch statement you can directly return the value in each case.

case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:
return 31;
break;

Similarly for the other cases.

Also, you do not return a value in the default case. I'm surprised the compiler does not issue a warning about not all control paths returning a value. You could return zero as a code to show the month was invalid or even throw an exception.

2. In order to call the function: bool leapYear(int y) const;

you will need to instantiate a date object. But testing whether a particular year is a leap year has nothing to do with any particular date that has been instantiated. Instead you could make it a static member function. That way you can call it through any date object or through the scope resolution operator.

static bool leapYear(int y) const;

1
2
date christmas(( 12, 25, 2015);
std::cout << christmas.leapYear( 2020 );


or

std::cout << date::leapYear(2020 );

3. If you eliminate your days member then you could also make your dayOfMonths function a static const member function.

static int daysOfMonths(int m, int y) const;
Topic archived. No new replies allowed.