Problem displaying month names from class

I am having trouble associating the month names with the month numbers. When the program runs, it should output the date in three styles.

Also I do not know if my input validations work correctly(day cannot be greater than 31 or less than 1, month cannot be greater than 12 or less than 1).

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
#include <iostream>
using namespace std;

char again;

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

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

};

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

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

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

int Date::getMonth() const
{
	if (month > 12 || month < 1)
		cout << "Month Invalid.";
	
	return month;
}

int Date::getDay() const
{
	if (day > 31 || day < 1)
		cout << "Day Invalid.";

	return day;
}

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

int main()
{
	do
	{
		Date date;
		int dateMonth;
		int dateDay;
		int dateYear;

		string monthName[12] = {"January", "February", "March", "April", "May", "June", "July", 
								"August", "September", "October", "November", "December"};
		

		cout << "-----Date Class-----" << endl;
		cout << "What is the month? ";
		cin >> dateMonth;
		cout << "What is the day? ";
		cin >> dateDay;
		cout << "What is the year? ";
		cin >> dateYear;

		date.setMonth(dateMonth);
		date.setDay(dateDay);
		date.setYear(dateYear);
		
		cout << "The date is: " << endl;
		cout << date.getMonth() << "/" << date.getDay() << "/" << date.getYear() << endl;
		cout << monthName[date.getMonth()-1] << " " << date.getDay() << "," << date.getYear() << endl;
		cout << date.getDay() << " " << monthName[date.getMonth()-1] << " " << date.getYear() << endl;


		cout << "\nDo you want to run this program again? Y/N: ";
		cin >> again;

	} while (again == 'y' || again == 'Y');

	return 0;
}
Why do you validate the month when you get it, and not when you set it?

"Would you like red, blue, or purple?"
"I'd like yellow."
"OK, sure thing!"
"Wait, what color did I choose again?"
"Yellow is invalid, sorry!"
Something like this?

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
#include <iostream>
using namespace std;

char again;

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

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

};

void Date::setMonth(int m)
{
	if (month > 12 || month < 1)
		cout << "Month Invalid.";
	month = m;
}

void Date:: setDay(int d)
{
	if (day > 31 || day < 1)
		cout << "Day Invalid.";
	day = d;
}

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

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

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

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

int main()
{
	do
	{
		Date date;
		int dateMonth;
		int dateDay;
		int dateYear;

		string monthName[12] = {"January", "February", "March", "April", "May", "June", "July", 
								"August", "September", "October", "November", "December"};
		

		cout << "-----Date Class-----" << endl;
		cout << "What is the month? ";
		cin >> dateMonth;
		cout << "What is the day? ";
		cin >> dateDay;
		cout << "What is the year? ";
		cin >> dateYear;

		date.setMonth(dateMonth);
		date.setDay(dateDay);
		date.setYear(dateYear);
		
		cout << "The date is: " << endl;
		cout << date.getMonth() << "/" << date.getDay() << "/" << date.getYear() << endl;
		cout << monthName[date.getMonth()-1] << " " << date.getDay() << "," << date.getYear() << endl;
		cout << date.getDay() << " " << monthName[date.getMonth()-1] << " " << date.getYear() << endl;


		cout << "\nDo you want to run this program again? Y/N: ";
		cin >> again;

	} while (again == 'y' || again == 'Y');

	return 0;
}
The problem now is that you're checking "month" and "day", which haven't been assigned to yet. You need to check the parameters "m" and "d" for validity.

Also, other than this, the program seems to work - what problems are you having with the association of month to month name?
Your first program here works fine, the only thing I added was an #include <string> statement in the beginning. Only problem I see is when you put in the wrong month or day it freezes the console.

Note: I think what L B was trying to tell you in his first post is you should more your if statements into your do while loop so you ask when the input is given, this will stop the freezing, or should. What it is doing is asking for the date and accepting them, then when you go look in the class it is not there, which is bad.
Last edited on
1
2
cout << monthName[date.getMonth()-1] << " " << date.getDay() << "," << date.getYear() << endl;
cout << date.getDay() << " " << monthName[date.getMonth()-1] << " " << date.getYear() << endl;


These two lines give me errors. It says that no operator << matches these operands. It must be something with my compiler. I'm using Microsoft Visual C++ 2010 Express.

The errors are...
Error 1 error C2679: binary '<<' : no operator found which takes a right-hand operand of type 'std::string' (or there is no acceptable conversion) c:\users\fred steinman\documents\visual studio 2010\projects\date class\date class\date class.cpp 85
Error 2 error C2679: binary '<<' : no operator found which takes a right-hand operand of type 'std::string' (or there is no acceptable conversion) c:\users\fred steinman\documents\visual studio 2010\projects\date class\date class\date class.cpp 86
3 IntelliSense: no operator "<<" matches these operands c:\users\fred steinman\documents\visual studio 2010\projects\date class\date class\date class.cpp 85
4 IntelliSense: no operator "<<" matches these operands c:\users\fred steinman\documents\visual studio 2010\projects\date class\date class\date class.cpp 86
The #include <string> fixed my error. Thank you both for the help!
The error you are getting is the string error. at the top of your code after #include <iostream> add in #include <string>

You need to use this call anytime you use a string in your code.
Topic archived. No new replies allowed.