date validation if statment not working

I've gone in and with debug, checked each of the values, everything seems right, but no matter what I put in, the if statement kicks the entry out as being invalid. What am I missing?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
 int validateYear(int year)
{
	char systemDate[MAX_CHARACTER_COUNT];
	char computedYear[5];
	strcpy_s(systemDate,__DATE__);
	int u = 0;
	int returnValue = 1;
	for (int i = strlen(systemDate)-5; i < 11; i++)
	{
		computedYear[u] = systemDate[i];
		u++;
	}
	int convertedYear = atoi(computedYear);
	if ((year<1900) || (year>convertedYear));
	{
		printf_s("Input Error: Please insert a year between 1900 and today.\n");
		returnValue = 0;
	}
	return(returnValue);
}
The main problem is the if statement at line 14 should not end with a semicolon.

Another issue is there is no terminating null character at the end of the computedYear. You should add
 
    computedYear[u] = 0; // add null terminator 
immediately after the closing brace at line 12.

However, you don't actually need the strings computedYear and systemDate, you could instead just use something like
 
    int convertedYear = atoi(__DATE__ + 7);


Note also that the __DATE__ macro doesn't give today's date. It gives the date when the program was compiled, which isn't necessarily the same as the date when the program is run.

In order to get the current date or time, use the facilities in the <ctime> header. Example:
1
2
3
4
5
	time_t rawtime;
	struct tm * timeinfo;
	time (&rawtime);
	timeinfo = localtime(&rawtime);
	int convertedYear = timeinfo->tm_year + 1900;

See http://www.cplusplus.com/reference/ctime/tm/ etc.
Last edited on
doesn't give the time the program is run? wow... good thing I was able to fool my instructor on that one. Thanks for the info!
Topic archived. No new replies allowed.