Creating an Age Calculator

I am a beginner in C++ programming.

I was trying to make an Age Calculator.

It works on the basis that the user will enter the date, his birth year, birth month and birth date.
I wanted to know that is there a method that will automatically take only the System Date and Month (i.e. if today is 10/05/2011, it will take the value of 10 and put it in place of current date [variable] and put 5 in place of the current month[variable])?

I am sorry if this has been asked before, though I couldn't find it anywhere.

Thanks in advance.
Have you tried yet? People will be much more willing to help if you post some code. For now, check out Parsing strings and regular expressions.
If you want to parse a string such as "10/05/2011", look up the StringTokenizer in the string header file and use the delimiter "/".
I made a basic structure, but the code was too long. I decided to not post it until someone asked for it.
Here is the whole code:
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
//Variables:
	unsigned int month;			//The User's month input
	unsigned int year;			//The User's year input
	unsigned int date;			//Today's Date - User Input
	
	unsigned int cur_month = 4;	//The Calculative month
	unsigned int cur_year;		//The Calculative year
	unsigned int cur_date;		//User's Input -  Today's date

	unsigned int dates;			//User Date (for Week)

	unsigned int year_age;		//The User's Age in Years
	unsigned int month_age;		//The Month's passed since user's last birthday
	unsigned int date_age;		//User Age Days
	unsigned int week_age;		//User Ag - Week

	unsigned int days = 0;	//Days to User's next Bithday

	int months [12] = {31 , 28  , 31  , 30  , 31  , 30  , 31  , 31  , 30  , 31  , 30  , 31};

	char e = 'N';			//To disable Auto Terminate
	char h = 'N';			//Also to disable Auto Terminate

	//Asking for input:
		
	
		//Current Date
		do
		{
		cout << "Please Enter TODAY's Date and press ENTER/RETURN" << endl;
		cin >> cur_date;
		cout << endl << endl;
		
			//If Flaw:
			if (0 >= cur_date, cur_date >= 31)
			{
				cout << "The Value you enterred was Invalid. Please Enter the correct Value" << endl << endl;
			}
		} while (0 >= cur_date, cur_date >= 31);

		//User Year
		do
		{
		cout << "Please enter the YEAR you were born in and press ENTER/RETURN:" << endl;
		cin >> year;
		cout << endl << endl;
		
			//If flaw	
			if (0 >= year, year >=2011)
			{ 
				cout << "The Value you enterred was Invalid. Please Enter the correct Value" << endl << endl;
			}
		} while (0 >= year, year >= 2011);

		//User Month
		do
		{
		cout << "Please enter the MONTH you were born in NUMERIC FORM and press ENTER/RETURN" <<endl << "[i.e.January is 1, February is 2 and so on]:" << endl;
		cin >> month;
		cout << endl << endl;
		
			//If flaw	
			if (0 >= month, month >= 12 )
			{ 
				cout << "The Value you enterred was Invalid. Please Enter the correct Value" << endl << endl;
			}
		} while (0 >= month, month >= 2010);	

		//User Date
		do
		{
		cout << "Please enter the DATE you were born in and press ENTER/RETURN:" << endl;
		cin >> date;
		cout << endl << endl;
		
			//If flaw	
			if (0 >= date, date >= 31 )
			{ 
				cout << "The Value you enterred was Invalid. Please Enter the correct Value" << endl << endl;
			}
		} while (0 >= date, date >= 2010);

	//Putting up the conditions:
	
	if (month >= 4)
	{
		cur_year = 2010;
	}

	else
		cur_year = 2011;

	
	//Making Equations:
		//Current Age - Years:
		year_age = cur_year - year;
		
		//Current Age - Months:
		if (cur_year == 2010)
		{
			month_age = ((12 - month) + cur_month - 1);
		}

		else 
		{
			month_age = cur_month - month - 1;
		}
		
		//Current Age - Dates:
			
				//Dates Calculation
				dates = ((31 - date) + cur_date);

		//Current Age - Weeks:
				week_age = (dates)/7;

		//Current Date_Age:
				date_age = dates % 7;
				
		//Next Birthday:
			
			//Defining and Calculating Days:
			switch (month)
			{
			case 1: 
				days = (months[0] - month) + months[11] + months[10] + months[9] + months[8] + months[7] + months[6] +  
							months[5] + months[4] + (months[3] - cur_date);
				break;
			case 2: 
				days = (months[1] - month) + months[11] + months[10] + months[9] + months[8] + months[7] + months[6] +  
							months[5] + months[4] + (months[3] - cur_date) + months [2];
				break;
			case 3: 
				days = (months[2] - month) + months[11] + months[10] + months[9] + months[8] + months[7] + months[6] +  
							months[5] + months[4] + months[3]  + months[2];
				break;
			case 4: 
				days = (months[3] - month) + months[11] + months[10] + months[9] + months[8] + months[7] + months[6] +  
							months[5] + months[4] + (months[3] - cur_date);
				break;
			case 5: 
				days = (months[3] - cur_date) +  date;
				break;
			case 6: 
				days = (months[3] - cur_date) +  date + months[4];
				break;
			case 7: 
				days = (months[3] - cur_date) +  date + months[5] + months[4];
				break;
			case 8: 
				days = (months[3] - cur_date) +  date + months[4] + months[5] + months[6];
				break;
			case 9: 
				days = (months[3] - cur_date) +  date + months[4] + months[5] + months[6] + months[7];
				break;
			case 10:
				days = (months[3] - cur_date) + date + months[4] + months[5] + months[6] + months[7] + months[8];
				break;
			case 11:
				days = (months[3] - cur_date) + date + months[4] + months[5] + months[6] + months[7] + months[8] + months[9];
				break;
			case 12: 
				days = (months[3] - cur_date) + date + months[4] + months[5] + months[6] + months[7] + months[8] + months[10];
				break;
			}

//Giving the Age
	cout << "You are "<< year_age << " years, " << month_age << " months, "<< week_age << " weeks and " << date_age << " days old." << endl << endl;
	
	//Next Birthday
	cout << "Your next BIRTHDAY is after " << days << " days." << endl << endl;

	//Thanking Words
	cout << "--------------------------------------------------------------------------------";

	cout << "Thank You for using Age Calculator" << endl << endl;
	cout << "Made by:" << endl << "Nisheeth Pandey" << endl << endl ;
	
	cout << "--------------------------------------------------------------------------------";
	
	// Preventing Auto Terminate with Option to Exit or Not
	
	do
	{	
		cout <<endl << "EXIT (Y or N):";
		cin >> e;
				
				//Declaring h
				if (e == 'Y')
					h = 'Y';
				else if (e == 'y')
					h = 'y';
				//Definfing possibilities
			
				switch(e)
			{
			case 'Y': cout << endl << "You chose to EXIT the program" << endl;
				break;
			case 'y': cout << endl << "You chose to EXIT the program" << endl;
				break;
			case 'N': cout << endl << "You chose not to exit the program. If you want to calculate another age, please restart the AGE CALCULATOR" << endl;
				break;
			case 'n': cout << endl << "You chose not to exit the program. If you want to calculate another age, please restart the AGE CALCULATOR" << endl;
				break;
			default : cout << endl << "The value you enterred was neither ‘Y’ nor ‘N’. Please chose a value between ‘Y’ and ‘N’." << endl;
				break;						
			};
	} 	while (e != h);
It might just be better to not over think it, and ask for the user to input the year and date...

EDIT: Although if you just want to gain some experiance than string parsing might be your solution
Last edited on
Well, O.K. I will look up String parser. Thanks anyway.
too long
You could do this more simply by converting the calender dates to Julian days....then subtracting to get age in days (or years by taking 365.24 days per year.)
Topic archived. No new replies allowed.