C++ Homework

Hello guys, I've been trying for quite long, so please, if anyone can help, I really need it.

Here's the question:
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
/*
Date
Design a class called Date that has integer data members to store month, day, and
year. The class should have a 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 formats:
	3/15/16
	March 15, 2016
	15 March 2016
Demonstrate the class by writing a program that uses it.
	Input Validation: Only accept values between 1 and 12 for the month, between 1
	and 31 for the day, and between 1950 and 2020 for the year.

In addition, I also need to:

1. add member functions to set the month, day, and year.

2. add a function that will accept as an argument another object of type Date and compare if that object's date occurred before, after or at the same time as the current object's date.

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

// Function prototype
void timeSettngs(Date &);

int main()
{
	Date Time;
	char Choice;

	cout << "Your time is incorrect.\n"
		 << "Enter time settings (Y/N)? ";
	cin  >> Choice;

	if (Choice == 'y' || Choice == 'Y')
		timeSettngs(Time);

	return 0;
}

void timeSettngs(Date &Obj)
{
	int m, d, y, c;

	cout << "Welcome to time settings!\n"
		 << "Enter the month: ";
	cin  >> m;
	cout << "Enter the day: ";
	cin  >> d;
	cout << "Enter the year: ";
	cin  >> y;

	Obj.setDate(m, d, y);

	cout << "Enter a number from the menu to choose "
	     << "how your time will be displayed.\n"
	     << " 1. 3/15/16\n"
	     << " 2. March 15, 2016\n"
	     << " 3. 15 March 2016\n"
	     << " 4. Exit time settings.\n";
	cin  >> c;

	switch(c)
	{
		case 1 : Obj.IntDate(Obj);
				 break;
		case 2 : Obj.MthDayYr(Obj);
				 break;
		case 3 : Obj.DayMthYr(Obj);
				 break;
		case 4 : cout << "Exiting time settings....";
	}
}
Last edited on
So do you have any Date.h and Date.cpp files to show us?

Please use [code][/code] tags when posting code (the <> icon on the right).
Date.h

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#ifndef DATE_H
#define DATE_H

class Date
{
private:
	int month,
		day,
		year;
public:
	Date()
	{
		month = 1;
		day = 1;
		year = 2001;
	}
	Date(int, int, int);
	void setDate(int, int, int);
	void validater(int, int, int);
	void IntDate(Date D);
	void MthDayYr(Date D);
	void DayMthYr(Date D);
};
#endif 


Date.cpp

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

//Function prototypes
string monthHelper(int);
int yearHelper(int);

/**************************************************************************
*                         Date constructor                               *
**************************************************************************/
Date::Date(int m, int d, int y)
{
	validater(m, d, y);
}

/**************************************************************************
*                             setDate                                    *
**************************************************************************/
void Date::setDate(int m, int d, int y)
{
	validater(m, d, y);
}

/**************************************************************************
*                            IntDate                                     *
* This function accepts a Date object and prints the date in the format  *
* 3/15/2016                                                              *
**************************************************************************/
void Date::IntDate(Date D)
{
	cout << D.month << "/" << D.day << "/" << yearHelper(D.year) << endl;
}

/**************************************************************************
*                           MthDayYr                                     *
* This function accepts a Date object and prints the date in the format  *
* March 15, 2016                                                         *
**************************************************************************/
void Date::MthDayYr(Date D)
{
	cout << monthHelper(D.month) << " " << D.day << ", " << D.year << endl;
}

/**************************************************************************
*                           DayMthYr                                     *
* This function accepts a Date object and prints the date in the format  *
* format 15 March 2016                                                   *
**************************************************************************/
void Date::DayMthYr(Date D)
{


	cout << D.day << " " << monthHelper(D.month) << " " << D.year << endl;

}
/***************************************************************************
*                      monthHelper function                               *
* This function accepts and integer as its an argument and returns a      *
* string of that month                                                    *
***************************************************************************/
string monthHelper(int Num)
{
	string mth;

	switch (Num)
	{
	case 1: mth = "January";
		break;
	case 2: mth = "February";
		break;
	case 3: mth = "March";
		break;
	case 4: mth = "April";
		break;
	case 5: mth = "May";
		break;
	case 6: mth = "June";
		break;
	case 7: mth = "July";
		break;
	case 8: mth = "August";
		break;
	case 9: mth = "September";
		break;
	case 10: mth = "October";
		break;
	case 11: mth = "November";
		break;
	case 12: mth = "December";
		break;
	}
	return mth;
}

/***************************************************************************
*                      yearHelper function                                *
* This function accepts and integer as its an argument and returns an     *
* integer formatted for the year display.                                 *
***************************************************************************/
int yearHelper(int y)
{
	if (y < 2000)
		return y - 1900;
	else
		return y - 2000;
}

/***************************************************************************
*                          validater                                      *
* This function accepts three integers as its arguments validates that    *
* variables m is between 1 and 12, d is between 1 and 31 and, y is        *
* between 1950 and 2020.                                                  *
***************************************************************************/
void Date::validater(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 >= 1950 && y <= 2020)
		year = y;
	else
		year = 2001;
}


Main.cpp

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

// Function prototype
void timeSettngs(Date &);

int main()
{
	Date Time;
	char Choice;

	cout << "Your time is incorrect.\n"
		<< "Enter time settings (Y/N)? ";
	cin >> Choice;

	if (Choice == 'y' || Choice == 'Y')
		timeSettngs(Time);

	return 0;
}

void timeSettngs(Date &Obj)
{
	int m, d, y, c;

	cout << "Welcome to time settings!\n"
		<< "Enter the month: ";
	cin >> m;
	cout << "Enter the day: ";
	cin >> d;
	cout << "Enter the year: ";
	cin >> y;

	Obj.setDate(m, d, y);

	cout << "Enter a number from the menu to choose "
		<< "how your time will be displayed.\n"
		<< " 1. 3/15/16\n"
		<< " 2. March 15, 2016\n"
		<< " 3. 15 March 2016\n"
		<< " 4. Exit time settings.\n";
	cin >> c;

	switch (c)
	{
	case 1: Obj.IntDate(Obj);
		break;
	case 2: Obj.MthDayYr(Obj);
		break;
	case 3: Obj.DayMthYr(Obj);
		break;
	case 4: cout << "Exiting time settings....";
	}
}
In addition to that, I also need to:

1. add member functions to set the month, day, and year.

2. add a function that will accept as an argument another object of type Date and compare if that object's date occurred before, after or at the same time as the current object's date.
Point 2 you could yield by operator overloading:
...
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
bool Date::operator<( const Date & other) 
{
    if (year < other.year) return true;
    if (month < other.month) return true;
    if (day < other.day) return true;
    return false;
}
bool Date::operator==( const Date & other)
{
    if (year == other.year && month == other.month && day == other.day)
        return true;
    return false;
}
bool Date::operator>( const Date & other)
{
    if (*this < other || *this == other) return false;
    return true;
}
...
    Date a(1,1,1950);
    Date b(3,12,2018);
    
    
    std::cout <<(a < b ? "true" : "false") << '\n';
    std::cout <<(a > b ? "true" : "false") << '\n';
    std::cout <<(a == b ? "true" : "false") << '\n';
...
Last edited on
Is that for number 2? What about number 1?
Last edited on
> void DayMthYr(Date D);
It seems strange that none of your member functions actually use the member variables of the current instance.

Eg
1
2
3
4
5
void Date::DayMthYr()
{
    // output the current 'this' member variables.
    cout << day << " " << monthHelper(month) << " " << year << endl;
}


Which you would call with
Obj.DayMthYr();

"1. add member functions to set the month, day, and year."
1
2
3
4
5
6
7
8
9
10
11
void Date::setDay( int d)
{
    // check if d is a valid day
    if (d < 0 || d >31 ) return;
    if ((month == 2 || month == 4 || month == 6 || month == 9 || month == 11)
        && d > 30
    ) return;

    day = d;
}
... // Implement the other setters like this. 
Last edited on
Date::validater() should return a bool, flagging whether the date is valid. Setting the Date values should been transferred to setter methods.
IntDate(), MthDayYr() DayMthYr(), and yearHelper() don't need to take a parameter. They should operate on the object that they're called on. For example:
1
2
3
4
void Date::IntDate()
{
	cout << month << "/" << day << "/" << yearHelper() << endl;
}


MonthHelper() could be shortened by use of an array:
1
2
3
4
5
6
7
8
string monthHelper(int Num)
{
    static const char *months[] = {
        "January", "February", "March", "April", "May", "June",
        "July", "August", "September", "October", "November", "December"
    };
    return months[Num-1];
}
Last edited on
Topic archived. No new replies allowed.