student needing help.

I am write a program for class and I am not sure if I am headed in the correct direction. I will first put what my instructions say then I will post my code for my function.

Design a class called Date that has integer data members to store month, day, year. The class should have three-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 (i.e January 1, 2001)should be used. The class should have member functions to print the date in the following format:
3/15/10
March 15, 2010
15 March 2010

Demonstrate the class by writing a program that uses it.

Input Validation: Only accept values between 1 and 12 for month, between 1 and 31 for days, and between 1950 and 2020 for the year.

This is what I have for my function

1
2
3
4
5
6
7
8
9
10
11
12
class Date
{
public:
	void input();
	void output();

	void set (int month, int day , int year);

	int get_month;
	int get_day;
    int get_year;
}


Please let me know if there is something wrong and why its wrong so I can correct it. Thank you in advance.
Last edited on
first off you
are missing a
semicolon ;

1
2
3
4
5
6
7
8
9
10
11
12
class Date
{
public:
	void input();
	void output();

	void set (int month, int day , int year);

	int get_month;
	int get_day;
    int get_year;
};  //<------- 


i need 5-10 min to
give you a little hint/example
for the rest of your
program.
I realized that after I posted it....lol
@kmartar i have this
error sometimes too

i assume that
you do not need
setters and
getters isn't?

you could complete
the rest of the program

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
//ClassDate.cpp
//##

#include <iostream>
using std::cout;
using std::cin;
using std::endl;


//class definition
class Date{
	private:
		int month;
		int day;
		int year;

	public:
		Date();
		Date(int month,int day,int year);  
		void print_f1(); //format 1
		void print_f2(); //format 2
		void print_f3();  //format 3  

};//end class Date



int main(){

	//testing class Date

	Date myDate; //no arguments
	//if you want pass arguments
	//you should do something
	//like this Date myDate(3,1,2014);  
		

		myDate.print_f1(); //print date
		cout<<endl;	//new line

return 0; //indicates success
}//end of main


//class implementation
Date::Date(){
	month=1;
	day=1;
	year=2001;
}//end Date constructor -no arguments passed-

Date::Date(int month,int day,int year){
	//code validation here
}//end Date constructor

void Date::print_f1(){
	cout<<month<<'/'<<day<<'/'<<year;
}//end method print_f1 



./ClassDate 
1/1/2001
Last edited on
You have been a big help. Two more things.
1. In void Date::print_f2() & void Date::print_f3() i need to change the digital numbers of the month to actual word for the month like 1= January and so on. can you get me started. I was thinking of an if statement and I'm not sure if it will work.
2. i tried to use iomanip to use setw() on one of my dates and I could not get it to work correctly. I kept getting an error saying setw() not defined.

If you would like to see what I have so far and any changes I have made let me know and I post the code.
Last edited on
Yes let me see
your code this
is an example using
switch statement for
print_f2();

 ./ClassDate 
1/1/2001
January/1/2001


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
//ClassDate.cpp
//##

#include <iostream>
using std::cout;
using std::cin;
using std::endl;

#include <string>
using std::string;

//class definition
class Date{
	private:
		int month;
		int day;
		int year;

	public:
		Date();
		Date(int month,int day,int year);
		void print_f1(); //format 1
		void print_f2(); //format 2
		void print_f3();  //format 3  

};//end class Date



int main(){

	//testing class Date

	Date myDate; //no arguments
	//if you want pass arguments
	//you should do something
	//like this Date myDate(3,1,2014);  
		

		myDate.print_f1(); //print date (format 1)
		cout<<endl;	//new line
		myDate.print_f2(); //print date in format 2
		cout<<endl; //new line

return 0; //indicates success
}//end of main


//class implementation
Date::Date(){
	month=1;
	day=1;
	year=2001;
}//end Date constructor -no arguments passed-

Date::Date(int month,int day,int year){
	//code validation here
}//end Date constructor

void Date::print_f1(){
	cout<<month<<'/'<<day<<'/'<<year;
}//end method print_f1

void Date::print_f2(){
	
	string Month; 
	
	switch(month){//class data member: 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;
	}//end switch

	
	cout<<Month<<'/'<<day<<'/'<<year; //you can use , / - ...etc.
}//end method print_f2 
An alternative to a switch-case for month names is a lookup table.
1
2
3
4
5
6
7
    const string montab[12] = { "January", "February", "March", "April","May",
        "June", "July", "August", "September", "October","November", "December"};
	
    if (month>0 && month<13)
        Month = montab[month-1];
    else
        Month = "Unknown";
Last edited on
chervil is the alternative using an array?


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
 
//ClassDate.cpp

#include <iostream>

using namespace std;
using std::cout; 
using std::cin;
using std::endl;


//class definition
class Date{
	private:                               // User manipuation.
		int month;
		int day;
		int year;

	public:                             // Only the program can change this.
		Date();
		Date(int month,int day,int year);  
		void print_f1();                    //format 1. 01/01/2014
		void print_f2();                    //format 2. January 1, 2014
		void print_f3();                    //format 3  1 January 2014

};//end class Date


	int main()
	{
            //testing class Date

		Date myDate; 
		myDate.print_f1();                  //print date #1
		cout<<endl;	

        
		 myDate.print_f2();                 //print date #2
		cout<<endl;

         myDate.print_f3();                  //print date #3
		cout<<endl;	

		system("pause");
		return 0; 
}





//class implementation
	Date::Date()
	{
	cout << "Enter the number for the month.";
	cin >> month;
	
	cout << "Enter the day.";
	cin >> day;
	
	cout << "Enter a four digit year.";
	cin >> year;
}                                        //end Date constructor -no arguments passed-

	Date::Date(int month,int day,int year)        // Validation.
	{
	if((month > 1) || (month > 12) || (day <1) || (day > 31) || (year < 1950) || (year > 2020))
		cout << "Illegal data." << endl;
	}										//end Date constructor

	void Date::print_f1()                        //Print menthod #1.
{
	string Month; 
	
	switch(month){//class data member: 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::print_f2()                        //Print menthod #2.
{
	cout << month << '/' << day << '/' << year;
}
Last edited on
Can someone tell me why line 124 gives me this error.

IntelliSense: no operator "<<" matches these operands
operand types are: std::ostream << std::string
You need to #include <string> to use strings properly. The reason that this error is being recieved is probably that the definition for those operators are in the string header.
@kmartar you can
implement your own
if-else statements instead
of the switch example or
use @Chervil's implementation
(if you are allowed to use arrays)


Error compilation
Option 1
1
2
3
4
5
6
7
#include <iostream>
using std::cout;
using std::cin;
using std::endl;

#include <string>
using std::string;


Option 2

1
2
3
4
#include <iostream>
#include <string>

using namespace std;


If you write using namespace std;
statement after the headers:
1
2
#include <iostream>
#include <string> 


you do not need
to specify that you are
using objects/functions
from a header i.e.
1
2
3
4
5
6
7
#include <iostream>
using std::cout; //using standard C++ object console output 
using std::cin; //using standard C++ object console input
using std::endl; 

#include <string>
using std::string;


I am a little busy
right now but in a
few minutes i can help
to you further
In put validation is not working.
Maybe your compiler doesn't like the combination of strings and character for line 124.
 
cout << Month << "/"<< day << "/" <<year; 
@kmartar i can
see a little mistake in
your code
1
2
3
4
if((month > 1) || (month > 12) || (day <1) || (day > 31) || (year < 1950) || (year > 2020))
		cout << "Illegal data." << endl;
}
 

Should be month less than one month<1

Here is an example
let me see what you have
later!!

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
//ClassDate.cpp
//##

#include <iostream>
using std::cout;
using std::cin;
using std::endl;

#include <string>
using std::string;

//class definition
class Date{
	private:
		int month;
		int day;
		int year;

	public:
		Date();
		Date(int Month,int Day,int Year);
		void print_f1(); //format 1
		void print_f2(); //format 2
		void print_f3();  //format 3  

};//end class Date



int main(){

	//testing class Date

	Date myDate; //no arguments
	//if you want pass arguments
	//you should do something
	//like this Date myDate(3,1,2014);  
		

		myDate.print_f1(); //print date (format 1)
		cout<<endl;	//new line
		myDate.print_f2(); //print date in format 2
		cout<<'\n'<<endl; //new line

	Date D_Day(6,6,1944);
	D_Day.print_f1();
	cout<<endl;
	D_Day.print_f2();
	cout<<"\n\n";

	Date Fall_Berlin_Wall(11,9,1989);
	Fall_Berlin_Wall.print_f1();
	cout<<endl;
	Fall_Berlin_Wall.print_f2();
	cout<<endl;

return 0; //indicates success
}//end of main


//class implementation
Date::Date(){
	month=1;
	day=1;
	year=2001;
}//end Date constructor -no arguments passed-

Date::Date(int Month,int Day,int Year){
	if((Month<1||Month>12)||(Day<1||Day>31)||(Year<1950||Year>2020)){
		month=1;
		day=1;
		year=2001;
		
	}else{
		month=Month;
		day=Day;
		year=Year;
	}//end if-else
}//end Date constructor

void Date::print_f1(){
	cout<<month<<'/'<<day<<'/'<<year;
}//end method print_f1

void Date::print_f2(){
	
	string Month; 
	
	switch(month){//class data member: 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;
	}//end switch

	
	cout<<Month<<'/'<<day<<'/'<<year; //you can use , / - ...etc.
}//end method print_f2 
1/1/2001
January/1/2001

1/1/2001
January/1/2001

11/9/1989
November/9/1989
Topic archived. No new replies allowed.