Need help with age finding program.

The problem is that I need to figure out the user's birthday in months, days, hours, and minutes from today's date.

Here's what I have so far.

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
#include<iostream>
#include<cmath>
#include<ctime>

using namespace std;

int main()
{
	int todayDay, todayMonth, todayYear;
	int birthDay, birthMonth, birthYear;
	int birthHour, birthMin, birthSec;
	int hour, min, sec;
	int ageDay, ageMonth, ageYear;
	int ageHour, ageMin, ageSec;

	time_t result;
	result = time(NULL); 
	struct tm* tp = localtime(&result);

	todayMonth = tp->tm_mon + 1; 
	todayDay  = tp->tm_mday;
	todayYear  = tp->tm_year + 1900;
	hour = tp->tm_hour;
	min  = tp->tm_min;
	sec  = tp->tm_sec;

	cout << "Today's Date is: "<< todayMonth <<" " << todayDay <<" "<< todayYear <<endl;
	cout << "And the time is: "<< hour <<":"<< min << ":" << sec << endl;

	cout << "Enter month of your birth: "<<endl;
	cin  >> birthMonth;

	 cout << "Enter the date of birth: "<<endl;
	 cin  >> birthDay;

	 cout << "Enter the birth year: "<<endl;
	 cin  >> birthYear;

	 cout << "Enter the time you were born. ";
	 cout << "\nHour, minute, and second seperated by spaces." <<endl;
	 cin >> birthHour >> birthMin >> birthSec

	if(birthYear <= 1900)
	{
		ageYear = todayYear  - birthYear;

	}

	if(birthDay <= 31)
	{
		ageDay = todayDay  - birthDay;
	}

	if(birthMonth <= 12)
	{
		ageMonth = todayMonth - birthMonth;
	}

	cout << "Your age is: "
         << ageMonth << " month(s) and "
         << ageDay << " day(s)." << endl
         << hour << " hour(s) "
         << min   << " min(s) "
         << sec   << " second(s)."<< endl;

	system("pause");
    return 0;
}


I haven't initialized all the variables, yet. And it's very unfinished.
I need to figure out how to convert years from the age into months only.
And I need to figure out how to calculate the hours, seconds, and minutes.
I feel like there should be a shorter, simpler way to figuring this out, but I'm having a rough day and just can't think. Could you maybe give me some pointers in the right direction?
Last edited on
Okay. So I figured out the time part.
It was pretty easy. I think I got it right.
And here's an update of what I have.

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
79
80
81
82
#include<iostream>
#include<cmath>
#include<ctime>

using namespace std;

int main()
{
	int todayDay, todayMonth, todayYear;
	int birthDay, birthMonth, birthYear;
	int birthHour, birthMin, birthSec;
	int hour, min, sec;
	int ageDay, ageMonth, ageYear;
	int ageHour, ageMin, ageSec;

	time_t result;
	result = time(NULL); 
	struct tm* tp = localtime(&result);

	todayMonth = tp->tm_mon + 1; 
	todayDay  = tp->tm_mday;
	todayYear  = tp->tm_year + 1900;
	hour = tp->tm_hour;
	min  = tp->tm_min;
	sec  = tp->tm_sec;

	cout << "Today's Date is: "<< todayMonth <<" " << todayDay <<" "<< todayYear <<endl;
	cout << "And the time is: "<< hour <<":"<< min << ":" << sec << endl;

	cout << "Enter month of your birth: "<<endl;
	cin  >> birthMonth;

	 cout << "Enter the date of birth: "<<endl;
	 cin  >> birthDay;

	 cout << "Enter the birth year: "<<endl;
	 cin  >> birthYear;

	 cout << "Enter the time you were born. ";
	 cout << "\nHour, minute, and second seperated by spaces." <<endl;
	 cin >> birthHour >> birthMin >> birthSec;

	if(birthYear <= 1900)
	{
		ageYear = todayYear  - birthYear;
	}

	if(birthDay <= 31)
	{
		ageDay = todayDay  - birthDay;
	}

	if(birthMonth <= 12)
	{
		ageMonth = todayMonth - birthMonth;
	}

	if (birthHour <= 24)
	{
		ageHour = hour - birthHour;
	}

	if (birthMin < 60)
	{
		ageMin = min - birthMin;
	}

	if(birthSec < 60)
	{
		ageSec = sec - birthSec;
	}

	cout << "Your age is: "
         << ageMonth << " month(s) and "
         << ageDay << " day(s)." << endl
         << ageHour << " hour(s) "
         << ageMin << " min(s) "
         << ageSec << " second(s)."<< endl;

	system("pause");
    return 0;
}


I feel like converting the years into months should be really easy, and I still feel like I'm using a really long way of going about this. But I'm just not sure.
Alright this is in java but you should be able to easily convert it to c++ the concept remains the same. Also I only calculated out the days but you can also easily convert it to minutes/seconds.
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
package practicing;

import java.util.Calendar;
import java.util.Date;
import java.util.Scanner;

public class FindRelativeAge {

	private static Calendar calendar = Calendar.getInstance();
	private static String birthDate = "";
	private static String[] myArray = null;
	private static int birthDay = 0;
	private static int birthMonth = 0;
	private static int birthYear = 0;
	private static Scanner wiz = new Scanner(System.in);

	public static void enterYourDumbAssBirthday() {
		System.out.print("What is your birthday (e.g. 4/15/1994) -> ");
		birthDate = wiz.nextLine();
		myArray = birthDate.split("/");
		birthDay = Integer.parseInt(myArray[1].trim());
		birthMonth = Integer.parseInt(myArray[0].trim());
		birthYear = Integer.parseInt(myArray[2].trim());
		
	}

	public static void findWhenYourNextDumbAssBirthdayIs() {
		int currentMonth =  calendar.get(Calendar.MONTH) + 1;
		int currentDate = calendar.get(Calendar.DATE);
		//int currentYear =  calendar.get(Calendar.YEAR);
		int temp = 12 - currentMonth + birthMonth;
		int temp2 = currentDate -birthDay;
		
		System.out.println("Your birthday is " + temp + " months & " + temp2 + " days away");
//		System.out.println("You were born -> " + birthDate);
//		System.out.println("Your next birthday is -> " + monthDifference + " months away");
//		System.out.println("                      -> " + dayDifference + " days away");
//		System.out.println("                      -> " + yearDifference + " years away");
//		
	}

	public static void main(String[] args) {
		enterYourDumbAssBirthday();
		findWhenYourNextDumbAssBirthdayIs();
	}

}
Oh, okay.
Wow. That's way different than what I had.
I'll try working with that. Thanks for the reply.

Here was my final product:

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
79
80
81
82
#include<iostream>
#include<cmath>
#include<ctime>

using namespace std;

int main()
{
	int todayDay, todayMonth, todayYear;
	int birthDay, birthMonth, birthYear;
	int birthHour, birthMin, birthSec;
	int hour, min, sec;
	int ageDay, ageMonth, ageYear;
	int ageHour, ageMin, ageSec;

	time_t result;
	result = time(NULL); 
	struct tm* tp = localtime(&result);

	todayMonth = tp->tm_mon + 1; 
	todayDay  = tp->tm_mday;
	todayYear  = tp->tm_year + 1900;
	hour = tp->tm_hour;
	min  = tp->tm_min;
	sec  = tp->tm_sec;

	cout << "Today's Date is: "<< todayMonth <<" " << todayDay <<" "<< todayYear <<endl;
	cout << "And the time is: "<< hour <<":"<< min << ":" << sec << endl;

	cout << "\nEnter month of your birth: "<<endl;
	cin  >> birthMonth;

	 cout << "\nEnter the date of birth: "<<endl;
	 cin  >> birthDay;

	 cout << "\nEnter the birth year: "<<endl;
	 cin  >> birthYear;

	 cout << "\nEnter the time you were born. ";
	 cout << "\nHour, minute, and second (in 24 hour format) seperated by spaces: " <<endl;
	 cin >> birthHour >> birthMin >> birthSec;

    if(birthYear > 1900)
	{
		ageYear = todayYear - birthYear;
	}

	if(birthMonth <= 12)
	{
		ageMonth = (todayMonth - birthMonth + (ageYear*12));
	}

	if(birthDay <= 31)
	{
		ageDay = todayDay - birthDay;
	}

	if (birthHour < 24)
	{
		ageHour = hour - birthHour;
	}

	if (birthMin < 60)
	{
		ageMin = min - birthMin;
	}

	if(birthSec < 60)
	{
		ageSec = sec - birthSec;
	}

	cout << "Your age is: " << ageMonth
         << " month(s) and "
         << ageDay << " day(s)." << endl
         << ageHour << " hour(s) "
         << ageMin << " min(s) "
         << ageSec << " second(s)."<< endl;

	system("pause");
    return 0;
}


The math is wrong, though. If I put a date past today (for example, if I say my birthday is on the 30th) it gives me a negative number in days.
But I'll try to convert that to C++. I have like no experience with Java.
Last edited on
Topic archived. No new replies allowed.