Not sure what you are asking here. Aer you asking how you can extract it or how you can figure it out? One thing you should remember is that the month and year are also very important because depending on what time of the year it is, you could be off a year in your calculations. So If it is 01/02/2001 as a birth and the date is 11/17/2011, then the person is 10 years old but if the birthday is 12/23/2001, then you person is only 9 and still looking to turn 10 this year.
You can either do a simple check of the month and day for this or use a simple formula. Like maybe month * 35 + day (as long as the constant is greater then the amount of days in the longest month, the formula will work. So when you compare 2 days, say 1/2 and 12/23, the first would be 37 while the second would be 443, since 443 is greater then 37, it is later in the year.
There are also other applications when dealing with the date like the Doomsday Formula which will calculate what day of the week any given date will fall under. I used this in a calendar program I wrote so that the month would always have the proper look and it was faster then counting days since the formula is based on the concept of a repeating calendar every 400 years. It won't help you in this situation but it is interesting and good to know for dealing with dates in the future.
As for extracting. If you extract it as a string, you can first detect if there are two / to make sure it was entered correctly and then use istringstream to convert it to three separate numbers. Pretty easy.
1 2 3 4 5 6 7 8 9 10
|
string s = "1/1/2010";
int y = s.find('/');
if(y >= 0)
s[y] = ' ';
y = s.find('/', y+1);
if (y >= 0)
s[y] = ' ';
int x1, x2, x3;
istringstream i(s);
i >> x1 >> x2 >> x3; // x1 = month, x2 is day, x3 is year
|
Your if statement if fine, just make sure you cover every possible age and also account for someone trying to enter a future date. People will do stupid things, always try to input anything to make sure the crazies are taken care of.
If I didn't answer what it is you need to know, I am sorry but I tried to cover everything I thought you were asking. Repost here and I will be glad to try again.