Class called Date

I can't get my output to show the correct date, when I put in a date such as 10 10 1977 it shows 1/1/1977,January 1 1977, 1 January 1977

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
  /*I will design a class called Date that has integer data members to store month, day, and year. 
The class will have a 3-parameter default constructor that allows the date to be set at the time
a new Date object is created.  If the user creates a Date object without passing any arguments, 
or if any of the values passed are invalid, the default values of 1, 1, 2001(Jan. 1, 2001) should
be used.  I will demonstrate the class by a program that uses it.*/


#include <iostream>
#include <string>
using namespace std;

class Date
{
private:
	int month, day, year;
public:
	//These are consturctors
	Date();
	Date(int, int, int);
	//Destructor 
	~Date(){}
	//void do not return values
	void setDay(int);
	void setMonth(int);
	void setYear(int);
	void showDate1();
	void showDate2();
	void showDate3();
};
Date::Date()
{
	//Initialize variables.
	month = 0, day = 0, year = 0;
}
Date::Date(int Month, int Day, int Year)
{
	month = Month;
	day = Day;
	year = Year;
}
void Date::setDay(int d)
{
	if (d < 1 && d > 31)
		cout << "The day is invalid" << endl;
	else
	day = d;
	
}
void Date::setMonth(int m)
{
	if (m < 1 && m > 12)
		cout << "The month is invalid" << endl;
	else
	month = m;
	
}
void Date::setYear(int y)
{
	if (y < 1950 && y > 2020)
		cout << "The year is invalid" << endl;
	else
		year = y;
}
void Date::showDate1()
{
	cout << month << " /" << day << " /" << year << endl;
}
void Date::showDate2()
{
	string monthName[] = { "January", "February", "March",
		"April", "May", "June", "July",
		"August", "September", "October",
		"November", "December" };
	cout << monthName[month - 1] << "  " << day << "  " << year << endl;
}
void Date::showDate3()
{
	string monthName[] = { "January", "February", "March",
		"April", "May", "June", "July",
		"August", "September", "October",
		"November", "December" };
	cout << day << "  " << monthName[month - 1] << "  " << year << endl;
}

int main()
{
	int Month, Day, Year;
	string monthName[] = { "January", "February", "March",
		"April", "May", "June", "July",
		"August", "September", "October",
		"November", "December" };


	cout << "Please enter a month (between 1 - 12) " << endl;
	cin >> Month;
	
	cout << "Please enter a day (between 1 - 31) " << endl;
	cin >> Day;
	
	cout << "Please enter a year (between 1950 - 2020) " << endl;
	cin >> Year;
		
	
	Date newDate(Month, Day, Year);
	newDate.showDate1();
	newDate.showDate2();
	newDate.showDate3();

	cin.get();
	cin.get();
	return 0;
}
I have already tested it .. And this is my result

1
2
3
4
5
6
7
8
9
10
Please enter a month (between 1 - 12) 
10 
Please enter a day (between 1 - 31) 
10 
Please enter a year (between 1950 - 2020) 
1977 
10 /10 /1977
October 10 1977
10 October 1977


Try to run it in C++ shell which appears at top-right corner of your code ..
Last edited on
Hi,

Nothing to do with your problem, but you shouldn't have to re-declare your monthname array, make it a member of your class, and remove it from main()

Good luck !!

Edit:

If you use an initializer list in the constructor, then you wont need the set functions either.

And your default constructor shouldn't initialize to 0's, you specified 1,1,2001

Do you check for feb 30, or months that only have 30 days, but the user input 31?

Cheers
Last edited on
Topic archived. No new replies allowed.