Same idea. To get the months... subtract the birth month from the current month. Add 12 if negative.
etc
etc
EDIT:
To solve these kind of problems.... just think about how you would solve the problem if you were doing this by hand. IE... if you had to figure out someone's exact birthday... how would you do it? Figure out each step-by-step thing you have to do to get the answer.
Once you have all the steps... just write code to do those same steps.
Count how many leap years there are between dates and add 1 day for each leap year.
Like I say... the logic for this is pretty simple. This is something you could do by hand. If you can do it by hand... then you understand the logic.
Think about how you'd do it if you had to do this with just a pen and paper. Figure out the steps that you'd do... and then just translate those steps into code.
What element of code you you have to use to convert a -1 into a 1 and so on, i don't really wanna write if statements all the way until -11. So basically just a code which removes the - from the number, and also which sort of variable should i use when the variable goes to minus, double?
Thank you, so the reason i asked about double is cause, i can't get my months to show up. E.g You are 45 years old and 5 months. I can't get it to work. The number of months doesn't show up, can you help me?
1 2 3 4 5
int foo = -foo;
int ferdigManeder = cMonth - bMonth;
cout << "You are";
cout << ferdigManeder; // The result of months
cout << " months old";
Err... 'foo' is a placeholder name you're supposed to replace with whatever makes sense in your case. Just writing int foo = -foo; won't magically negate the particular number you want to negate.
If the number is negative to start it will be positive otherwise it will become negative. You will need to do an if statement something like if(foo < 0) foo = -foo; Or you could use a ternary (though I would use first method) foo = foo > 0 ? foo : -foo;
What if you enter 2014, 8, 7 and current year/month/day and 2013, 8, 15 as the birth year/month/day. The current code will say that you are 1 year old.
You'll have the same problem with the number of months old.