Program not completely doing its job

For my class we were suppose to create a small program outputting the users Month/Day/Year.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. The professor gave us a prototype function called void setDateValues(int&,int&,int&); If you look at my Test.cpp File. So, the problem is that my professor wants us to create a 3-argument constructor which passes the values to the private members. Which I think I did correctly. Now she also wanted us to call the bool IsValidDate(); which I did but when it tries to validate it doesn't do it. Any help please!


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
//DATE.H FILE
#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:
	Date(int m, int d, int y)
	{

	if (!IsValidDate())
	{
		month = 1;
		day = 1;
		year = 2001;
	}
		month = m;
		year = y;
		day = d;
	}
	void Print(DateFormat format);
};


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
//TEST.CPP
#include "Date.H"

void setDateValues(int&,int&,int&);
int main()
{
	int mth, day, yr;
	setDateValues (mth, day, yr);
	Date *dateObject = new Date(mth, day, yr);
	/*dateObject->GetDate();*/
	dateObject->Print(numeric);
	dateObject->Print(standard);
	dateObject->Print(alternative);
	delete dateObject;

	return 0;
}


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
//DATE.CPP
#include "Date.H"




bool Date::IsValidDate()
{
	if (month>12 || month<1)
		return false;
	if (day>monthDays[month+1] || day<1)
		return false;
	if (year>MAX_YEAR || year<MIN_YEAR)
		return false;
	return true;
}
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(DateFormat format)
{
	

	switch (format)
	{
		case numeric:
			cout<<month<<"/"<<day<<"/"<<year<<"\n";
			break;
		case standard:
			cout<<monthStr[month-1]<<" "<<day<<", "<<year<<"\n";
			break;
		case alternative:
			cout<<day<<" "<<monthStr[month-1]<<" "<<year<<"\n";
			break;
	}	
}
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
int mth, day, yr; //¿it would kill you to write two more characters?

//Date *dateObject = new Date(mth, day, yr);
Date dateObject( mth, day, yr ); //constructing an object

Date(int m, int d, int y)
{
//learn to indent
	if (!IsValidDate()) 
	{
		month = 1;
		day = 1;
		year = 2001;
	}
	month = m;//this would always execute 
	year = y;
	day = d;
}
Last edited on
Thanks for the help! and yes it would kill me :3. Just kidding that was part of the code given by the instructor. Take it out on her!.
Also sorry about not indenting! didn't mean to make it look messy.

I think i understood your tip "this would always execute". I had to put those three variables BEFORE the "if" statement!.


At first I constructed the object like you did but, my instructor wrote this on the code given
/* Create a Date instance (object) from the user input here*/
Maybe I complicated things by creating a pointer.


One more thing, and I'll get off your back. For some strange reason, when I input 11 and 12. Which are November and December for some strange reason they come out as false and are outputted by the default format. I can't seem to find whats causing it.

1
2
3
4
5
6
7
if (month>12 || month<1)
		return false;
	if (day>monthDays[month+1] || day<1)
		return false;
	if (year>MAX_YEAR || year<MIN_YEAR)
		return false;
	return true;

Last edited on
You don't need to necessarily put those three variable assignments before the if, just enclose them in their own else statement, such as:

1
2
3
4
5
6
7
8
9
10
11
12
13
if (!IsValidDate())
{
     month = 1;
     day = 1;
     year = 2001;
}

else
{
     month = m;
     year = y;
     day = d;
}


As far as the issues with the 11 and 12 not working, monthDays is an array starting at index 0 = 31 to index 11 = 31. That means January would associate with 0, December with 11. You are adding one where you should actually be subtracting 1. If a user enters 1 for January, you should be subtracting 1 to get the 0 index. By adding 1 instead of subtracting, if the user enters 11 or 12, the array will be out of bounds and who knows what you may get, but most likely it'll result in a result of false (maybe, again, who knows) evaluation. Good luck.
Last edited on
You were right randisking haha.
My logic at that point was most likely not working properly, I am assuming. I had accounted the rules of array's which starts from "0" but for some reason I thought adding it was the correct way. Who knows what I was thinking honestly, thank you very much!
Topic archived. No new replies allowed.