Help with my bool logic for date validation

Sep 17, 2013 at 3:27am
Hey guys,
I have a problem I need some help with. I pretty much have the problem complete. My issue is regardless of which date I input its always defaulting to the values I have set in my constructor in my implementation file in the else statement. So the values always default to 3/15/2006 I think its something to do with the logic in my bool function but I may be incorrect. If someone can assist me with this that would be appreciated. Thank you guys

header
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
//date.h header
#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			// Class body and declartions
{
private: 
	int month;
	int day;
	int year;
	bool isDateValid (int m, int d, int y);
public:
	Date (int m, int d, int y); // 3 argument constructor
	void getValues();
	
}; // End of class declaration 


Last edited on Sep 19, 2013 at 9:21pm
Sep 17, 2013 at 4:32am
closed account (D80DSL3A)
At line 9 in your constructor: if (isDateValid(month, day, year)) month, day and year have not yet been initialized. You wantif (isDateValid(m, d, y)) instead.

Some issues with your isDateValid() function:
I think you want >= and <= in place of > and < everywhere.
Also, if the year is valid it will return true without checking month or day.
Last edited on Sep 17, 2013 at 4:32am
Sep 17, 2013 at 4:46am
Hey fun2code. Thanks for your response. I tried your suggestion switched month, day, year to m,d,y and the output for my code is still the same. Am I doing something else wrong?


1
2
3
4
5
6
if (isDateValid(m, d, y))
	{
	month = m;
	day = d;
	year = y;
	}
Sep 17, 2013 at 5:24am
I built the solution again and its doing opposite to my last post. Now all the the input gets past the validation regardless of what is entered.
Sep 17, 2013 at 7:26am
Try with doing if (isDateValid (month, day, year))Then after have a look at this.


1
2
3
4
5
6
if (isDataValid (m, d, y))
{
month = m; // add these for the key of the program.
day= d;
year = y;
}


Try that.
Topic archived. No new replies allowed.