setDate and getDate issue

Design and Implement a class called Date that has data members to store month (as a number), day, year, and name of the month. The class should have a three-parameter constructor that allows the data to be set at the time of new Data object instances are created. Default constructor that does not take any parameters should set the default values of 1(month),1(day),2001(year). The class should have following three member functions to display date following formats
showDate1() should display the date in 1/1/2001 format
showDate2() should display the date in January 1,2001 format
showDate3() should display the date in 1 January 2001 format
Also the class should have method to set the date(setDate()).This method should take month, day, and year as parameters and set the object instance data member values.

I can not get my code to return the (8, 29, 1986) in the format required by the question.

My Date.h header file:

#include <string>
using namespace std;

class Date {

public:
Date();
Date(int , int, int);
void setMonth(int);
int getMonth();
void setDay(int);
int getDay();
void setYear(int);
int getYear();

void setDate(int, int, int);
int getDate();

void showDate1();
void showDate2();
void showDate3();

private:
int day = 1;
int month = 1;
int year = 2001;
};

My Date.cpp file:

#include <string>
#include <iostream>
#include "Date.h"
using namespace std;

Date::Date()
{
}


Date::Date(int m, int d, int y)
{
setMonth(m);
setDay(d);
setYear(y);
}


void Date::setDay(int d)
{
day = d;
}

int Date::getDay()
{
return day;
}

void Date::setMonth(int m)
{
month = m;
}

int Date::getMonth()
{
return month;
}


void Date::setYear(int y)
{
year = y;
}

int Date::getYear()
{
return year;
}




void Date::setDate(int m, int d, int y)
{

month = m;
day = d;
year = y;

}

int Date::getDate()
{

return month, day, year;

}

My Assignment5.cpp file:

#include <iostream>
#include <string>
using namespace std;
#include "Date.h"

int main()
{
Date d1;
Date d2(2, 12, 2010);

// Modified the code so I can see the results

d1.showDate1();
cout << endl;
d2.showDate2();
cout << endl;

d1.setDate(8, 29, 1986);
d2.showDate3();
cout << endl;

system("pause");
return 0;
}

My output is:
1/1/2001
February 12, 2010
12 February 2010

The correct output should be:
1/1/2001
February 12, 2010
29 August 1986

I don't know why my d1.setDate(8, 29, 1986) is not being set. Any help would be greatly appreciated. I have watched alternative videos and read the other posts in this forum but I couldn't find one that helped with this problem. Thanks again.
http://www.cplusplus.com/articles/z13hAqkS/


Please use code tags. Can use the <> button on the format toolbar.

You haven't posted all the code: Where is the code for the 3 showDate functions?

A function can only return one value. getDate could take some references as arguments, you could set these, then they will be available in the calling scope. The getDate function wasn't asked for in the assignment.

There is no need to call set functions in any member function including the constructors, these have direct access to the member variables. An even better idea to use a member initialisation list.

The default constructor was supposed to set values 1,1,2001 , but you do it in the class definition. This makes the default constructor redundant.

Good Luck !!
Sorry, must have not copied all of it over

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
#include <string>
#include <iostream>
#include "Date.h"
using namespace std;

Date::Date()
{
}


Date::Date(int m, int d, int y)
{
	setMonth (m);
	setDay (d);
	setYear (y);
}


void Date::setDay(int d)
{
	day = d;
}

int Date::getDay()
{
	return day;
}

void Date::setMonth(int m)
{
	month = m;
}

int Date::getMonth()
{
	return month;
}


void Date::setYear(int y)
{
	year = y;
}

int Date::getYear()
{
	return year;
}


void Date::setDate(int m, int d, int y)
{

	month = m;
	day = d;
	year = y;
	
}

int Date::getDate()
{

	return month, day, year;

}


void Date::showDate1()
{
	cout << month << "/" << day << "/" << year;
}

void Date::showDate2()
{
	
	string Month;

	switch (month)  
	{
	case 1: Month = "January";
		break;

	case 2: Month = "February";
		break;

	case 3: Month = "March";
		break;

	case 4: Month = "April";
		break;

	case 5: Month = "May";
		break;

	case 6: Month = "June";
		break;

	case 7: Month = "July";
		break;

	case 8: Month = "August";
		break;

	case 9: Month = "September";
		break;

	case 10: Month = "October";
		break;

	case 11: Month = "November";
		break;

	case 12: Month = "December";
		break;
	}
	
	cout << Month << " " << day << ", " << year;

}

void Date::showDate3()
{
	string Month;

	switch (month)
	{
	case 1: Month = "January";
		break;

	case 2: Month = "February";
		break;

	case 3: Month = "March";
		break;

	case 4: Month = "April";
		break;

	case 5: Month = "May";
		break;

	case 6: Month = "June";
		break;

	case 7: Month = "July";
		break;

	case 8: Month = "August";
		break;

	case 9: Month = "September";
		break;

	case 10: Month = "October";
		break;

	case 11: Month = "November";
		break;

	case 12: Month = "December";
		break;
	}

	cout << day << " " << Month << " " << year;
}
I thought I had to use the getDate line of code to retrieve the setDate stored information.
With your code for the showDate2 and showDate3 functions, you could have a private function which has that switch statement to set Month. Each of the constructors could call this function, then you won't have repetition of your code. If you have repetition, then there is a better way :+)

Now your actual problem is rather simple :+)

1
2
d1.setDate(8, 29, 1986);
d2.showDate3(); //   <----- 12 February 2010 need d1 instead 


I thought I had to use the getDate line of code to retrieve the setDate stored information.



Why? You don't call the function, IMO it's not needed at all.
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>
#include <string>
using namespace std;
#include "Date.h"

int main()
{
	Date d1;
	Date d2(2, 12, 2010);

	// Modified the code so I can see the results

	d1.showDate1();
	cout << endl;		
	d2.showDate2();
	cout << endl;

	d1.setDate(8, 29, 1986);
	d2.showDate3();
	cout << endl;

	system("pause");
	return 0;
}


What was modified? It looks like you have shown what is already in my Assignment5.cpp file.
For the getDate line, I was trying to follow the teachers notes but when I removed it, the code compiled anyway with the same results. I am still confused on what the actual problem is.
Wait, am I supposed to change the d2 to d1?! That part of the code was provided by the instructor. I guess I assumed that the provided code was correct.
You were right. I changed the d2.showDate3(); to d1.showDate3(); and the code compiled as it should. I spent a 3 days thinking my code was wrong and it looks to be a typo on the provided code. I can sleep easy now.
I gather you can see that d2 is Feb 12 2010 ? The code works fine (except I would have the private function like I mentioned) Important to realise here that just because code works, it doesn't mean it is right :+)

For the getDate line, I was trying to follow the teachers notes but when I removed it, the code compiled anyway with the same results. I am still confused on what the actual problem is.


The problem is that one can only return 1 value form a function.

What actually happens when the compiler encounters return month, day, year; is the comma operator comes into effect. With the comma operator only the right hand expression is considered. So in this case year is returned.

The comma operator is used in other situations where the side effects of the expressions (other than the last one) are relied upon. Side effects usually happen when there is assignment in one of the expressions. So in your case nothing happens, apart form year being returned.

Your code compiles, but it wouldn't have the right effect, had you actually called the function. Because you never called the function, it doesn't make any difference to anything. Hence why I think it unnecessary.

http://www.cplusplus.com/doc/tutorial/operators/
Hi again,

With the code that sets the Month name: an easier thing to do would be to have an array of Month name strings, then look up the name via the array subscript. This is better than having a big long switch.
Topic archived. No new replies allowed.