Help Creating Program Object-Oriented

Okay, so the point of the program is for the user to input a Month/Day/Year. My Program then Validates if the user inputs the correct information (ie. 1-12 on Months and 1900-2015 in years.). If the user inputs the information incorrect it gives default format which is 1/1/2001. So lets say the user inputs the correct information (ie. 1/12/1995). I then must show that exact date in three different formats such as 1/12/1995, January 12, 1995, 12 January 1995. I am stuck with this program and need some light or a whole new direction. So far this is what I got. It is divided into three files the Header/implementation/Test. Any tips would be helpful


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
// HEADER FILE
#include <iostream>
#include <string>
using namespace std;

enum DateFormat {numeric, standard, alternative}; //Enum was giving by instructor but I have no idea where I will be incorporating this into the program.
const int MIN_YEAR = 1900;
const int MAX_YEAR = 2015;
const string monthStr [] =
{"January",  "February", "March",  "April", "May", "June",
"July",  "August", "September",  "October", "November",
"December"};
const int monthDays [] =

{31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};


class Date
{
private:
	int month;
	int year;
	int day;
public:
        bool isValidDate
	void print();
};


//TEST FILE 
#include <iostream>
#include <string>
using namespace std;
#include "Months.h"


void setDateValues (int&, int&, int&);

int main()
{
   int mth, day, yr;
   setDateValues (mth, day, yr);
   Date dateobj;/* Create a Date instance (object) from the user input here*/
   cout << "Date is:\n";

  /* Call your print member function 3 separate times to test print the date in each of 3 formats */
}


// TEST.CPP
#include <iostream>
#include <string>
using namespace std;
#include "Months.h"



void setDateValues(int& m, int& d, int& y)
{
   cout << "Enter month: ";
   cin >> m;
   cout << "Enter day: ";
   cin >> d;
   cout << "Enter year: ";
   cin >> y;


   
}
void Date::print()
{
//I was planning on making this member function the one that displays the three formats
}

bool Date::isValidDate(int m, int d, int y)
{

// this was just me experimenting with the code. No idea if this is correct.
   if (m > 12 && m < 1)
      return true;
   else
      return false;

   if (year < MAX_YEAR && year > MIN_YEAR)
	   return true;
   else
	   return false;
}
Last edited on
main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include "stdafx.h"
#include "TEST.H"

int _tmain(int argc, _TCHAR* argv[])
{
	Date *dateObject = new Date();
	dateObject->GetDate();
	dateObject->Print(numeric);
	dateObject->Print(standard);
	dateObject->Print(alternative);
	delete dateObject;
	system("pause");
	return 0;
}


TEST.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
25
26
#include <iostream>
#include <string>
using namespace std;

enum DateFormat {numeric, standard, alternative};
const int MIN_YEAR = 1900;
const int MAX_YEAR = 2015;
const string monthStr [] =
	{"January",  "February", "March",  "April", "May", "June",
	"July",  "August", "September",  "October", "November",
	"December"};
const int monthDays [] =
	{31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};


class Date
{
private:
	int month;
	int year;
	int day;
	bool IsValidDate();
public:
	void GetDate();
	void Print(DateFormat format);
};


TEST.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
#include "TEST.H"

bool Date::IsValidDate()
{
	if (month>12 || month<1)
		return false;
	if (day>monthDays[month] || day<1)
		return false;
	if (year>MAX_YEAR || year<MIN_YEAR)
		return false;
	return true;
}

void Date::GetDate()
{
	cout << "Enter month: ";
	cin >> month;
	cout << "Enter day: ";
	cin >> day;
	cout << "Enter year: ";
	cin >> year;
}

void Date::Print(DateFormat format)
{
	if (!IsValidDate())
	{
		month = 1;
		day = 1;
		year = MIN_YEAR;
		cout<<"Incorrect data. Set minimal values\n";
	}

	switch (format)
	{
		case numeric:
			cout<<day<<"/"<<month<<"/"<<year<<"\n";
			break;
		case standard:
			cout<<monthStr[month]<<" "<<day<<", "<<year<<"\n";
			break;
		case alternative:
			cout<<day<<" "<<monthStr[month]<<" "<<year<<"\n";
			break;
	}	
}
Last edited on
That main.cpp working with dynamic object "objectDate".

For static:
1
2
3
4
5
6
7
8
9
10
int _tmain(int argc, _TCHAR* argv[])
{
	Date dateObject;
	dateObject.GetDate();
	dateObject.Print(numeric);
	dateObject.Print(standard);
	dateObject.Print(alternative);
	system("pause");
	return 0;
}
Last edited on
Changed my username to Shuruki

Wow thank you, so much you saved me a lot of time here. Didn't expect someone to give me the entire code but I do have a couple questions lol, in any of my courses we have never named int main anything else or put any parameters into main. Explanation would be helpful when you have time please :). because I don't think were allowed to change int main or put any parameters. If there is no other choice I'll message my professor.


int _tmain(int argc, _TCHAR* argv[])
I keep getting an error with _TCHAR
2	IntelliSense: identifier "_TCHAR" is undefined	




Wow I would have never guessed to use switch case break in order to use enum for the format. Is that the only method in order to use enum or is there more?. I haven't used switch case break since the first week of ever taking C++. Need to practice those.


Why did bool isValidDate have to be private? is it because its trying to access the private variables such as day/month/year?.
bool IsValidDate();


No idea what this is
#include "stdafx.h"
Error	1	error C1083: Cannot open include file: 'stdafx.h': No such file or directory	


one more thing, and this is where it got me confused and I approached this the same way you did. I forgot to mention this (my fault). but if you see in my code void setDateValues (int&, int&, int&); is in Test.cpp File. Which, was giving to me by the professor. NO IDEA, why she would put it inside the Test.cpp. If the purpose of that function is to give values to the private variables. Not sure if she wanted us to figure it out ourselves to put it as a public member function or not sure what she was thinking.
Last edited on
int _tmain(int argc, _TCHAR* argv[]) generated automaticaly by my Visual Studio, in our case this is the same as int main().

"argc" gives you to know how much parameters are available and "argv" is an array of this parameters. For example if you start a program in command line as "programName.exe -t andSomethingElse" args will be equally 3 and argv[0] contains "programName.exe", argv[1] contains "-t", argv[2] contains "andSomethingElse".

TCHAR is a "type" that transforms into char (ASCII) or wchar_t (UNICODE) according to project settings or in case #define UNICODE is existing in your program. Just use "char" for your program.

Use enum when it's convenient.

isValidDate() private because there is no necessary to call it out of the object. Its internal mechanism. If u want to give main() access to isValidDate() than move isValidDate() to public. In any case isValidDate() has access to day/month/year. "private" for day/month/year means main() has no access to them.

Remove #include "stdafx.h" (its automatically generated by Visual Studio)

About void setDateValues (int&, int&, int&);
In the previous example void GetDate(); read data directly in to object. Change code next way:
read data in main() (d,m,y)
call setDateValues (int&, int&, int&);
----
setDateValues (int&, int&, int&) in test.cpp contains code that copy input parameters to object day month year.
Try modify code first, dont read bottom while has no trouble.




main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include "TEST.H"

int main()
{
	int m,d,y;
	cout << "Enter month: ";
	cin >> m;
	cout << "Enter day: ";
	cin >> d;
	cout << "Enter year: ";
	cin >> y;

	Date *dateObject = new Date();
	dateObject->SetDateValues(m,d,y);
	dateObject->Print(numeric);
	dateObject->Print(standard);
	dateObject->Print(alternative);
	delete dateObject;
	
	system("pause");
	return 0;
}


test.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
25
26
#include <iostream>
#include <string>
using namespace std;

enum DateFormat {numeric, standard, alternative};
const int MIN_YEAR = 1900;
const int MAX_YEAR = 2015;
const string monthStr [] =
	{"January",  "February", "March",  "April", "May", "June",
	"July",  "August", "September",  "October", "November",
	"December"};
const int monthDays [] =
	{31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};


class Date
{
private:
	int month;
	int year;
	int day;
	bool IsValidDate();
public:
	void SetDateValues(int&m, int&d, int&y);
	void Print(DateFormat format);
};


test.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
#include "TEST.H"

bool Date::IsValidDate()
{
	if (month>12 || month<1)
		return false;
	if (day>monthDays[month] || day<1)
		return false;
	if (year>MAX_YEAR || year<MIN_YEAR)
		return false;
	return true;
}

void Date::SetDateValues (int&m, int&d, int&y)
{
	month = m;
	day = d;
	year = y;
}

void Date::Print(DateFormat format)
{
	if (!IsValidDate())
	{
		month = 1;
		day = 1;
		year = MIN_YEAR;
		cout<<"Incorrect data. Set minimal values\n";
	}

	switch (format)
	{
		case (numeric):
			cout<<day<<"/"<<month<<"/"<<year<<"\n";
			break;
		case standard:
			cout<<monthStr[month]<<" "<<day<<", "<<year<<"\n";
			break;
		case alternative:
			cout<<day<<" "<<monthStr[month]<<" "<<year<<"\n";
			break;
	}	
}
Thanks Toshen I really appreciate the reply and the explanation.
I had already altered the code you gave me to the way the assignment was meant to be. The professor made it tricky but I was able to do it on my own.

She wanted the void setDateValues{int&,int&,int&) to be declared in the Test.cpp and not a member function. She wanted us to create a public 3-argument constructor in Class to give the private variables month/day/yea its values. Which are the values the user inputed, and she also wanted the bool private member function to be called inside the the 3-argument constructor. It wasn't to hard to do but it was a bit confusing for me but I read the textbook we are using about constructor's and it gave me an idea on how to do it.


Thanks again!


Topic archived. No new replies allowed.