How would you go about creating an exact age calculator

Pages: 1234
I've been wondering this, since there is so many things which needs to be taken care of. I created one, but it wasn't precise.
Uhh.. subtract the birth year from the current year? And subtract 1 if the current date is before the birth date.
I would like to be able to calculate months, weeks, days too.
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.
Last edited on
Time calculations are very hard to get correct. Use the library.
http://www.cplusplus.com/reference/ctime/localtime/
Disch, i get your idea. But i am wondering since leap years do exist how i would be able to calculate the days, since the value isn't static.
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?
Last edited on
foo = -foo; will negate 'foo' (make it positive if it's negative, or vice versa)

foo = std::abs(foo); (#include <complex>) will do an absolute value on foo to ensure it is positive.


also which sort of variable should i use when the variable goes to minus, double?


A normal 'int' can handle negative numbers just fine. You only need to use a double if you need floating points (ie: 0.123)
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";
Last edited on
The number of months doesn't show up


It must be. From the code you posted you are clearly outputting it. You either are not running that code, or you are misreading the output.

The number of months doesn't show up, can you help me?


I'd need to see more code to see what you're doing wrong.
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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
#include <iostream>
#include <cstdlib>
#include <time.h>
#include <complex>


using namespace std;

int main()
{
  
  int cDay; // Current values 
  int cMonth;
  int cYear;

  int bDay; // Birth values
  int bMonth;
  int bYear;








 // Current year, month, day
  cout << "Type in the current year: ";
  cin >> cYear;

  cout << "Type in the current month number: " ;
  cin >> cMonth;

  cout << "Type in the current day: ";
  cin >> cDay;

  // Birth year, month, day

  cout << "Type in your birth year: ";
  cin >> bYear;

  cout << "Type in the birth month: ";
  cin >> bMonth;

  cout << "Type in the birth day: ";
  cin >> bDay;

  int Alder = cYear - bYear;
  int ferdigAlder;


  if (bMonth > cMonth)
  {
      cout << "You are ";
cout << Alder - 1 ;
cout << " years old";
cin >> ferdigAlder;

  }
  else
  {
      cout << "You are ";
      cout << Alder;
      cout << " years old";
      cin >> ferdigAlder;

  }
  int foo = -foo;
  int ferdigManeder = cMonth - bMonth;
  cout << "You are";
  cout << ferdigManeder;
  cout << " months old";    




  return 0;
}
Line 57 and 65... you are trying to get another number from the user. The month does not get printed until the user inputs another number.
So i typed in

Current Year: 2014
Current Month : 8
Current Day: 6

Birth Year: 1990
Current Month 9
Current Day: 6


it says im -1 month old, i thought the int foo = -foo;
was suppose to turn any negative number into positive?
Last edited on
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.
Oh sorry, you're right helios.

Wont int foo = -foo;
Make the number negative? I want it to go positive
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;
Last edited on
int foo = -foo; does nothing. It creates a new variable with an undefined value and then negates that undefined value, leaving it still undefined.

For example,
1
2
3
4
5
6
int myvar = 42;
//Prints "42"
std::cout <<myvar<<std::endl;
myvar = -myvar;
//Prints "-42"
std::cout <<myvar<<std::endl;
Alright guys, thanks for the help so far. I'd like to know if any of you guys have a smart way of calculating the days. So something like this.

You are 20 years old 6 months and x days.
Hmm....

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.

Like I said, this is harder than it appears.

Dave
Pages: 1234