small problem . please help me ..

Hello every one ,
Please check this code and help me ..
The calculation of the age is WRONG .
How could I fix it ..?!
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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
/******SQU*******C++******SQU******C++*****SQU*******C++*****SQU*****

Name:   sanalbarq.net 

		This Program is going to :
		1.  Read the birth date & another date.
		2.	Check & validation & Find the next date of the entered date ( NOT for the birthdate) .
		3.  Calculate The age up to the given date .
		3.  Check if the same date DD MM has passed or not in the current year & 
		    calculate how many days still reamaining .


 ******SQU*******C++******SQU******C++*****SQU*******C++*****SQU******/


# include <iostream>
# include <cmath>
# include <iomanip>

using namespace std ;

// ***** ( PROTYPES ) ****** 

//1. CHECKING the validation of Birth Date 
void check_birthdate(int day_birth , int month_birth , int year_birth );

//2. CHECKING the validation of CURRENT Date
void check_currentdate(int day_date , int month_date , int year_date ,int day_birth ,int  month_birth ,int year_birth ) ;

//3. Finding the Next Day of Day Entered
void nxt_date(int day_date,int month_date,int year_date);


int main () //start main
{
	//Declarations
	int day_birth , month_birth , year_birth ;
	int day_date , month_date , year_date ;
	int day_age , month_age , year_age ;
//	int nxt_day , nxt_month , nxt_year ;

	// reading from user {BIRTH_DATE}
	cout << "Type your Birth_date as (dd mm yyyy) : " ; 
	cin >> day_birth >> month_birth >> year_birth ;
	cout<<endl;


	//-----------
	//<>**** CHECKING the validation of Birth Date ****
	check_birthdate(day_birth , month_birth ,year_birth );

	cout<<"***************************" << endl ;
	cout<<"The BIRTH Date : "<< day_birth <<"/"<< month_birth<<"/"<<year_birth<<endl<<endl;
	
	
	//--> reading from user {CURRENT_DATE}		
	cout << "Type The Date  (dd mm yyyy) : " ; 
	cin >> day_date >> month_date >> year_date ;


	//------------
	//<>**** CHECKING the validation of CURRENT Date 
	check_currentdate(day_date , month_date ,year_date,day_birth , month_birth ,year_birth );


	cout << "*************************************" << endl ;
	cout<<"The BIRTH  : "<< day_birth <<"/"<< month_birth<<"/"<<year_birth << endl;
	cout<<"The CURRENT: "<< day_date <<"/"<< month_date<<"/"<<year_date << endl;
	cout << "*************************************" << endl ;



	//-------------
	// CallinG NeXt _ DaTe Finding Function .
	nxt_date(day_date,month_date,year_date);


	// Finding the age ...
	year_age = day_date - day_birth ;
	month_age = month_date - month_birth ;
	day_age = day_date - month_birth ;

	cout<<endl<<"The Age is : " << year_age ;
	

	

	system("PAUSE");

	return 0;
} // end main

//*****************************************************



// CHECKING the validation of Birth Date 

void check_birthdate(int day_birth , int month_birth , int year_birth)
{

	// 1. while Month + Day is INVALID ..

	while (( month_birth > 12)&&(day_birth > 30 )) 
	{
		cout << "The Month & Day Entered are WRONG !! " << endl ;
		cout << "Please Type your Birth_Date again ONLY Day & Month as (dd mm ) : " ; 
		cin >> day_birth >> month_birth  ;
		cout<<endl;
	}



	// 2. while Day is INVALID ..

	while ( day_birth > 30) 
	{
		cout << "The DAY Entered is WRONG !! " << endl ;
		cout << "Please Type your Birth_date again ONLY the DAY : " ; 
		cin >> day_birth  ;
		cout<<endl;
	}
	

	// 3. while Month is INVALID ..
	while ( month_birth > 12) 
	{
		cout << "The Month Entered is WRONG !! " << endl ;
		cout << "Please Type your Birth_Date again ONLY Month : " ; 
		cin >> month_birth ;
		cout<<endl;
	}



} // End of Birth date validation




//******************************************************
// CHECKING the validation of CURRENT Date 

void check_currentdate(int day_date , int month_date , int year_date ,int day_birth ,int  month_birth ,int year_birth )
{

	// 1. while Month + Day is INVALID ..
		while (( month_date > 12)&&(day_date > 30 )) 
	{
		cout << "The Month and day Entered are WRONG !! " << endl ;
		cout << "Please Type the CURRENT_Date again as (dd mm yyyy) : " ; 
		cin >> day_date >> month_date>> year_date ;
		cout<<endl;
	}


	// 2. while Day is INVALID ..
	while ( day_date > 30) 
	{
		cout << "The DAY Entered is WRONG !! " << endl ;
		cout << "Please Type CURRENT_date again as (dd mm yyyy) : " ; 
		cin >> day_date >> month_date >> year_date ;
		cout<<endl;
	}
	


	// 3. while Month is INVALID ..
	while ( month_date > 12) 
	{
		cout << "The Month Entered is WRONG !! " << endl ;
		cout << "Please Type the CURRENT_Date again as (dd mm yyyy) : " ; 
		cin >> day_date >> month_date >> year_date ;
		cout<<endl;
	}



	// 4. while a confliction between BIRTH & CURRENT 
	while (( year_date <= year_birth) && ( month_date <= month_birth ) && ( day_date <= day_birth ))
	{
		cout << "The DATE Entered conflicts with the Birth_Date !! " << endl ;
		cout << "Please Type the Date again as (dd mm yyyy) : " ; 
		cin >> day_date >> month_date >> year_date ;
		cout<<endl;
	}
	


} // End of CURRENT Date validation





//******************************************************
// Finding NEXT Date ..................

void nxt_date(int day_date,int month_date,int year_date)
{
	if(day_date < 30 )
	{
	//	nxt_day =day_date + 1 ;
		cout << " NEXT Date is : " << day_date + 1 <<"/"<<month_date<<"/"<<year_date<<endl;
	}

	
	if(day_date == 30 )
	{
	//	nxt_day = 1 ;
	//	nxt_month = month_date + 1 ;
		cout << " NEXT Date is : " <<"1"<<"/"<< month_date+1 <<"/"<<year_date<<endl;
	}


	if(( day_date == 30 ) && (month_date == 12 ) )
	{
	//	nxt_day = 1 ;
	//	nxt_month = 1 ;
	//	nxt_year = year_date+1 ;
		cout << " NEXT Date is : " <<"1"<<"/"<<"1"<<"/"<<year_date+1 <<endl;
	}

} // End next Date Function ...


//*********************************************************** 
Last edited on
closed account (zb0S216C)
Where's your code?

Wazzak
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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
/******SQU*******C++******SQU******C++*****SQU*******C++*****SQU*****

Name:   sanalbarq.net 

		This Program is going to :
		1.  Read the birth date & another date.
		2.	Check & validation & Find the next date of the entered date ( NOT for the birthdate) .
		3.  Calculate The age up to the given date .
		3.  Check if the same date DD MM has passed or not in the current year & 
		    calculate how many days still reamaining .


 ******SQU*******C++******SQU******C++*****SQU*******C++*****SQU******/


# include <iostream>
# include <cmath>
# include <iomanip>

using namespace std ;

// ***** ( PROTYPES ) ****** 

//1. CHECKING the validation of Birth Date 
void check_birthdate(int day_birth , int month_birth , int year_birth );

//2. CHECKING the validation of CURRENT Date
void check_currentdate(int day_date , int month_date , int year_date ,int day_birth ,int  month_birth ,int year_birth ) ;

//3. Finding the Next Day of Day Entered
void nxt_date(int day_date,int month_date,int year_date);


int main () //start main
{
	//Declarations
	int day_birth , month_birth , year_birth ;
	int day_date , month_date , year_date ;
	int day_age , month_age , year_age ;
//	int nxt_day , nxt_month , nxt_year ;

	// reading from user {BIRTH_DATE}
	cout << "Type your Birth_date as (dd mm yyyy) : " ; 
	cin >> day_birth >> month_birth >> year_birth ;
	cout<<endl;


	//-----------
	//<>**** CHECKING the validation of Birth Date ****
	check_birthdate(day_birth , month_birth ,year_birth );

	cout<<"***************************" << endl ;
	cout<<"The BIRTH Date : "<< day_birth <<"/"<< month_birth<<"/"<<year_birth<<endl<<endl;
	
	
	//--> reading from user {CURRENT_DATE}		
	cout << "Type The Date  (dd mm yyyy) : " ; 
	cin >> day_date >> month_date >> year_date ;


	//------------
	//<>**** CHECKING the validation of CURRENT Date 
	check_currentdate(day_date , month_date ,year_date,day_birth , month_birth ,year_birth );


	cout << "*************************************" << endl ;
	cout<<"The BIRTH  : "<< day_birth <<"/"<< month_birth<<"/"<<year_birth << endl;
	cout<<"The CURRENT: "<< day_date <<"/"<< month_date<<"/"<<year_date << endl;
	cout << "*************************************" << endl ;



	//-------------
	// CallinG NeXt _ DaTe Finding Function .
	nxt_date(day_date,month_date,year_date);


	// Finding the age ...
	year_age = day_date - day_birth ;
	month_age = month_date - month_birth ;
	day_age = day_date - month_birth ;

	cout<<endl<<"The Age is : " << year_age ;
	

	

	system("PAUSE");

	return 0;
} // end main

//*****************************************************



// CHECKING the validation of Birth Date 

void check_birthdate(int day_birth , int month_birth , int year_birth)
{

	// 1. while Month + Day is INVALID ..

	while (( month_birth > 12)&&(day_birth > 30 )) 
	{
		cout << "The Month & Day Entered are WRONG !! " << endl ;
		cout << "Please Type your Birth_Date again ONLY Day & Month as (dd mm ) : " ; 
		cin >> day_birth >> month_birth  ;
		cout<<endl;
	}



	// 2. while Day is INVALID ..

	while ( day_birth > 30) 
	{
		cout << "The DAY Entered is WRONG !! " << endl ;
		cout << "Please Type your Birth_date again ONLY the DAY : " ; 
		cin >> day_birth  ;
		cout<<endl;
	}
	

	// 3. while Month is INVALID ..
	while ( month_birth > 12) 
	{
		cout << "The Month Entered is WRONG !! " << endl ;
		cout << "Please Type your Birth_Date again ONLY Month : " ; 
		cin >> month_birth ;
		cout<<endl;
	}



} // End of Birth date validation




//******************************************************
// CHECKING the validation of CURRENT Date 

void check_currentdate(int day_date , int month_date , int year_date ,int day_birth ,int  month_birth ,int year_birth )
{

	// 1. while Month + Day is INVALID ..
		while (( month_date > 12)&&(day_date > 30 )) 
	{
		cout << "The Month and day Entered are WRONG !! " << endl ;
		cout << "Please Type the CURRENT_Date again as (dd mm yyyy) : " ; 
		cin >> day_date >> month_date>> year_date ;
		cout<<endl;
	}


	// 2. while Day is INVALID ..
	while ( day_date > 30) 
	{
		cout << "The DAY Entered is WRONG !! " << endl ;
		cout << "Please Type CURRENT_date again as (dd mm yyyy) : " ; 
		cin >> day_date >> month_date >> year_date ;
		cout<<endl;
	}
	


	// 3. while Month is INVALID ..
	while ( month_date > 12) 
	{
		cout << "The Month Entered is WRONG !! " << endl ;
		cout << "Please Type the CURRENT_Date again as (dd mm yyyy) : " ; 
		cin >> day_date >> month_date >> year_date ;
		cout<<endl;
	}



	// 4. while a confliction between BIRTH & CURRENT 
	while (( year_date <= year_birth) && ( month_date <= month_birth ) && ( day_date <= day_birth ))
	{
		cout << "The DATE Entered conflicts with the Birth_Date !! " << endl ;
		cout << "Please Type the Date again as (dd mm yyyy) : " ; 
		cin >> day_date >> month_date >> year_date ;
		cout<<endl;
	}
	


} // End of CURRENT Date validation





//******************************************************
// Finding NEXT Date ..................

void nxt_date(int day_date,int month_date,int year_date)
{
	if(day_date < 30 )
	{
	//	nxt_day =day_date + 1 ;
		cout << " NEXT Date is : " << day_date + 1 <<"/"<<month_date<<"/"<<year_date<<endl;
	}

	
	if(day_date == 30 )
	{
	//	nxt_day = 1 ;
	//	nxt_month = month_date + 1 ;
		cout << " NEXT Date is : " <<"1"<<"/"<< month_date+1 <<"/"<<year_date<<endl;
	}


	if(( day_date == 30 ) && (month_date == 12 ) )
	{
	//	nxt_day = 1 ;
	//	nxt_month = 1 ;
	//	nxt_year = year_date+1 ;
		cout << " NEXT Date is : " <<"1"<<"/"<<"1"<<"/"<<year_date+1 <<endl;
	}

} // End next Date Function ...


//*********************************************************** 
Recheck your 'finding the age' section.
1
2
3
4
5
6
// Finding the age ...
	year_age = day_date - day_birth ; // Not checking Years
	month_age = month_date - month_birth ;
	day_age = day_date - month_birth ; // Subtracting from day_date, the month_date

	cout<<endl<<"The Age is : " << year_age ;


When I ran the program, I get not get my age, but at my next birthday in this year. To get a more accurate age, you'll have to check if the birth month and day are before or after, the current day and month.

whitenite1
BUT we know the difference between the current date and the birth date is equal to the AGE ..! OR NOT ??!

Then, I will ccomplete the programme ..
Not really, sanalbarq. If your birthday was before the current month and day, then yes, age is current year minus birth year, But, if your month of birth is later than current month, you need to subtract 1 year from age, since you have not yet aged this year.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
// Finding the age ...
	year_age = year_date - year_birth ;
	month_age = month_date - month_birth ;
	day_age = day_date - day_birth ;

	if(month_birth>month_date)
		year_age--;
	if(month_birth>month_date)
		month_age = 12-(month_birth - month_date);
	if(month_birth<month_date)
		month_age = month_date - month_birth;

	cout << endl << "The Age is : " << year_age << " years";
	if (month_age - month_date != 0)
	{
		cout << " and " << month_age << " month";
	if (month_age >1)
		cout << "s";
	}
	cout << " of age." << endl << endl;
ThanQ . ummmm could you plesae see to that result :
---------------------------------------


Type your Birth_date as (dd mm yyyy) : 20 3 1992

***************************
The BIRTH Date : 20/3/1992

Type The Date (dd mm yyyy) : 21 5 2012
*************************************
The BIRTH : 20/3/1992
The CURRENT: 21/5/2012
*************************************
NEXT Date is : 22/5/2012

The Age is : 0 years and 2 months of age.

Press any key to continue . . .
Sorry about that. I changed the order of input from day, month year to month, day, year, and forgot to change the check_birthdate function to the same sequence.
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
// Birthdates.cpp : main project file.

/******SQU*******C++******SQU******C++*****SQU*******C++*****SQU*****

Name:   sanalbarq.net 

		This Program is going to :
		1.  Read the birth date & another date.
		2.	Check & validation & Find the next date of the entered date ( NOT for the birthdate) .
		3.  Calculate The age up to the given date .
		3.  Check if the same date DD MM has passed or not in the current year & 
		    calculate how many days still reamaining .

 ******SQU*******C++******SQU******C++*****SQU*******C++*****SQU******/

#include <iostream>
#include <string>
#include <cmath>
#include <iomanip>

using namespace std ;

// ***** ( PROTYPES ) ****** 

//1. CHECKING the validation of Birth Date 
void check_birthdate(int month_birth , int day_birth , int year_birth );

//2. CHECKING the validation of CURRENT Date
void check_currentdate(int day_date , int month_date , int year_date ,int day_birth ,int  month_birth ,int year_birth ) ;

//3. Finding the Next Day of Day Entered
void nxt_date(int day_date,int month_date,int year_date);

int main () //start main
{
	
	string months[] = {"January","February","March","April","May","June","July","August","September","October","November","December"};
	//Declarations
	int day_birth , month_birth , year_birth ;
	int day_date , month_date , year_date ;
	int day_age , month_age , year_age ;
//	int nxt_day , nxt_month , nxt_year ;

	// reading from user {BIRTH_DATE}
	cout << "Type your Birth_date as (mm dd yyyy) : " ; 
	cin >> month_birth >> day_birth >> year_birth ;
	cout<<endl;

	//-----------
	//<>**** CHECKING the validation of Birth Date ****
	check_birthdate(month_birth , day_birth ,year_birth );

	cout<<"***************************" << endl ;
	cout<<"The BIRTH Date : "<< months[month_birth-1] <<" "<< day_birth<<", "<<year_birth<<endl<<endl;
	
	//--> reading from user {CURRENT_DATE}		
	cout << "Type The Date  (mm dd yyyy) : " ; 
	cin >> month_date >> day_date >> year_date ;

	//------------
	//<>**** CHECKING the validation of CURRENT Date 
	check_currentdate(day_date , month_date ,year_date,day_birth , month_birth ,year_birth );

	cout << "*************************************" << endl ;
	cout<<"The CURRENT DATE : "<<  months[month_date-1] <<" "<< day_date<<", "<<year_date << endl;
	cout<<"  The BIRTH DATE : "<<  months[month_birth-1] <<" "<< day_birth<<", "<<year_birth << endl;
	cout << "*************************************" << endl ;

	//-------------
	// CallinG NeXt _ DaTe Finding Function .
	nxt_date(day_date,month_date,year_date);

	// Finding the age ...
	year_age = year_date - year_birth ;
	month_age = month_date - month_birth ;
	day_age = day_date - day_birth ;

	if(month_birth>month_date)
	{
		year_age--;
		month_age = 12-(month_birth - month_date);
	}
	if(month_birth<month_date)
		month_age = month_date - month_birth;

	cout << endl << "The Age is : " << year_age << " years";
	if (month_age)
	{
		cout << " and " << month_age << " month";
	if (month_age >1)
		cout << "s";
	}
	cout << " of age." << endl << endl;

	system("PAUSE");

	return 0;
} // end main

//*****************************************************

// CHECKING the validation of Birth Date 

void check_birthdate(int month_birth , int day_birth , int year_birth)
{
	// 1. while Month + Day is INVALID ..
	while (( month_birth > 12)&&(day_birth > 30 )) 
	{
		cout << "The Month & Day Entered are WRONG !! " << endl ;
		cout << "Please Type your Birth_Date again ONLY Month & Day as (mm dd ) : " ; 
		cin >> day_birth >> month_birth  ;
		cout<<endl;
	}
	// 2. while Day is INVALID ..

	while ( day_birth > 30) 
	{
		cout << "The DAY Entered is WRONG !! " << endl ;
		cout << "Please Type your Birth_date again ONLY the DAY : " ; 
		cin >> day_birth  ;
		cout<<endl;
	}
	
	// 3. while Month is INVALID ..
	while ( month_birth > 12) 
	{
		cout << "The Month Entered is WRONG !! " << endl ;
		cout << "Please Type your Birth_Date again ONLY Month : " ; 
		cin >> month_birth ;
		cout<<endl;
	}
} // End of Birth date validation

//******************************************************
// CHECKING the validation of CURRENT Date 

void check_currentdate(int day_date , int month_date , int year_date ,int day_birth ,int  month_birth ,int year_birth )
{
	// 1. while Month + Day is INVALID ..
		while (( month_date > 12)&&(day_date > 30 )) 
	{
		cout << "The Month and day Entered are WRONG !! " << endl ;
		cout << "Please Type the CURRENT_Date again as (dd mm yyyy) : " ; 
		cin >> day_date >> month_date>> year_date ;
		cout<<endl;
	}

	// 2. while Day is INVALID ..
	while ( day_date > 30) 
	{
		cout << "The DAY Entered is WRONG !! " << endl ;
		cout << "Please Type CURRENT_date again as (mm dd yyyy) : " ; 
		cin >> month_date >> day_date >> year_date ;
		cout<<endl;
	}
	
	// 3. while Month is INVALID ..
	while ( month_date > 12) 
	{
		cout << "The Month Entered is WRONG !! " << endl ;
		cout << "Please Type the CURRENT_Date again as (mm dd yyyy) : " ; 
		cin >> month_date >> day_date >> year_date ;
		cout<<endl;
	}

	// 4. while a confliction between BIRTH & CURRENT 
	while (( year_date <= year_birth) && ( month_date <= month_birth ) && ( day_date <= day_birth ))
	{
		cout << "The DATE Entered conflicts with the Birth_Date !! " << endl ;
		cout << "Please Type the Date again as (mm dd yyyy) : " ; 
		cin >> month_date >> day_date >> year_date ;
		cout<<endl;
	}

} // End of CURRENT Date validation

//******************************************************
// Finding NEXT Date ..................

void nxt_date(int day_date,int month_date,int year_date)
{
	if(day_date < 30 )
	{
	//	nxt_day =day_date + 1 ;
		cout << " NEXT Date is : " << day_date + 1 <<"/"<<month_date<<"/"<<year_date<<endl;
	}
	
	if(day_date == 30 )
	{
	//	nxt_day = 1 ;
	//	nxt_month = month_date + 1 ;
		cout << " NEXT Date is : " <<"1"<<"/"<< month_date+1 <<"/"<<year_date<<endl;
	}

	if(( day_date == 30 ) && (month_date == 12 ) )
	{
	//	nxt_day = 1 ;
	//	nxt_month = 1 ;
	//	nxt_year = year_date+1 ;
		cout << " NEXT Date is : " <<"1"<<"/"<<"1"<<"/"<<year_date+1 <<endl;
	}

} // End next Date Function ...


//***********************************************************  
Last edited on
Thanks ..
BUT I am a beginner in C++ language , so I have not understood what you exactly did ..!
Please could you explain more ?!

Why it does not work in my code if we use the order (dd mm yyyy) ??!!

And also months[month_birth-1] How could the above mentioned code converts the Months from numbers 1,2,3,...etc to their letters words as January , Febrauary , ...etc ??!
string months[] = {"January","February","March","April","May","June","July","August","September","October","November","December"};
months[] is an array of strings (basically like a bunch of strings that you can use the same name to find). Inside of the [] goes a number that tells the program which string to use (in this case, which month to use). month_birth is an integer that stores the number of the month to use, but because counting in c++ starts at 0 (always start at zero when you count) you have to subtract 1 from the integer (because 1 is january in normal counting, but we need 0 in the list).
@sanalbarq
It does work in your code. I am just used to entering dates, with month first, then day, then year. It's just preference, that's all. You can change the input order back to what you had, if you prefer and it'll still work as you intended.
I added a string array of the months. Arrays start a zero, so when the user types, let's say 01 for month of January, a 1 gets subtracted to keep it in sync with the array, otherwise it would say the month was February.
months[0] = "January";
months[1] = "February";
...
etc.
...
months[11] = "December";

If you need more clarification, just ask. I'll be happy to add a bit more explanation.

Thank you alot NerdTastic and whitenite1 ..

Now it is so clear ..

We have taken in the study only light background about arrays .. I want to understand more and more to then be able to create complicated codes ..
Could anyone , please, provide me more explanation and examples in using arrays ??!


Best regards ,
Thanks for the information Guys! :)
Think of arrays as a group of related items. As in the array, months[], you can access a certain name of a month by just supplying its number in the sequence. If you didn't want to have to subtract one from the list, you could create the array as string months[13]={"", "January","February",(ETC. to end)};, then if you enter 1 as a month number, you do get "January", but that way is a waste of memory, since the array is larger, etc. Not to bad for very small programs, but bad with a lot of arrays. Or, if you're making a program concerning hotel rooms, and cost. double hotel_cost[5]={100,0,125.50,200.0,225.0,500.0} You access the cost by its place in the array. So if a person is staying 3 nights in hotel room 3, it would be float room_cost=hotel_cost[2]*3 It's hotel_cost[2] for #3, because you need to start count at 0. So, its, 0, 1, 2, for the 3rd slot in the array.
Topic archived. No new replies allowed.