date validation if statment not working
Apr 5, 2014 at 11:04am UTC
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?
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
int validateYear( int year)
int u = 0;
int returnValue = 1;
for ( int i = strlen(systemDate)-5; i < 11; i++)
computedYear[u] = systemDate[i];
int convertedYear = atoi(computedYear);
if ((year<1900) || (year>convertedYear));
printf_s( "Input Error: Please insert a year between 1900 and today.\n");
returnValue = 0;
Apr 5, 2014 at 1:11pm UTC
The main problem is the
statement at line 14 should not end with a semicolon.
Another issue is there is no terminating null character at the end of the
. 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
, you could instead just use something like
int convertedYear = atoi(__DATE__ + 7);
Note also that the
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:
2 3 4 5
struct tm * timeinfo;
timeinfo = localtime(&rawtime);
int convertedYear = timeinfo->tm_year + 1900;
Last edited on
Apr 5, 2014 at 2:33pm UTC
Apr 20, 2014 at 1:01pm UTC
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.