RSS Forums RSS
Please support our C++ advertiser: Programming Forums
Views: 1622 | Replies: 2
Reply
Join Date: Jul 2005
Posts: 15
Reputation: cpp noob is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 0
cpp noob cpp noob is offline Offline
Newbie Poster

Your exact age

  #1  
Jul 1st, 2005
this program contains everything even leap year:
i am a beginner in cpp (we have this in school so i am learning this age 15 )

# include<iostream.h>
# include<conio.h>

void main (void)

{


	clrscr();

	int years1, months1, days1;
	int monthdays1[12] = {31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365};
	int startday;
	int years2, months2, days2;
	int endday;
	int leapdays1, leapdays2;
	int oldyears, agebalance, balancedays, oldmonths, olddays, i;

	int const BASEYEAR=1900;
	int const BASEMONTH=12;
	int const BASEDAY=31;

	//Input birth date

	cout<<"Enter the date you were born(dd mm yyyy):\t";
	cin>>days1>>months1>>years1;
	cout<<endl;

	//error
	if (years1<BASEYEAR)
	 {
	 cout<<"The value of year is below 1900 Press any key to continue";
	 getch();
	 return;
	 }
		if (months1>BASEMONTH)
	 {
	 cout<<"The value of months is above 12 Press any key to continue";
	 getch();
	 return;
	 }

		if (days1>BASEDAY)
	 {
	 cout<<"The value of days is above 31 Press any key to continue";
	 getch();
	 return;
	 }

	//input todays date

	cout<<"Enter the date today(dd mm yyyy):\t";
	cin>>days2>>months2>>years2;
	cout<<endl;

	//error
	if (years2<BASEYEAR)
	 {
	 cout<<"The value of years is below 1900 Press any key to continue";
	 getch();
	 return;
	 }
		if (months2>BASEMONTH)
	 {
	 cout<<"The value of months is above 12 Press any key to continue";
	 getch();
	 return;
	 }

		if (days2>BASEDAY)
	 {
	 cout<<"The value of days is above 31 Press any key to continue";
	 getch();
	 return;
	 }

	//calculation
	years1=years1-1;
	months1=months1-1;
	days1=days1;

	leapdays1=years1/4;  //leapyear is a multiple for 4
	if((months1>=2) && ((years1+1)%4==0)) leapdays1++;
	if(years1>1999) leapdays1--;      //2000 is no leapyear


	startday=(years1*365)+monthdays1[months1-1]+days1+leapdays1;

	years2=years2-1;
	months2=months2-1;
	days2=days2;

	leapdays2=years2/4; //leapyear is a multiple for 4

	if((months2>=2) && ((years2+1)%4==0)) leapdays2++;
	if(years2>1999) leapdays2--; //2000 is no leapyear


	endday=(years2*365)+monthdays1[months2-1]+days2+leapdays2;

	agebalance=endday-startday;
	oldyears=agebalance/365;
	balancedays=agebalance%365;
	i=0;
	while (balancedays> monthdays1[i])
		{
		i++;
		}
	oldmonths=i;

	olddays=balancedays-monthdays1[oldmonths-1];

	//output
	cout<<"Your age is "<<oldyears<<" years, "<<oldmonths<<" months, ";
	cout<<olddays<<" days"<<endl;
	getch();
}
<< moderator edit: added [code][/code] tags >>
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Jun 2005
Location: Cambridge, MA
Posts: 1,317
Reputation: Rashakil Fol has a spectacular aura about Rashakil Fol has a spectacular aura about 
Rep Power: 7
Solved Threads: 42
Colleague
Rashakil Fol's Avatar
Rashakil Fol Rashakil Fol is offline Offline
Salamander Man

Re: Your exact age

  #2  
Jul 1st, 2005
Originally Posted by cpp noob
if(years1>1999) leapdays1--;      //2000 is no leapyear

2000 was a leap year.
Reply With Quote  
Join Date: Jul 2005
Posts: 15
Reputation: cpp noob is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 0
cpp noob cpp noob is offline Offline
Newbie Poster

Re: Your exact age

  #3  
Jul 2nd, 2005
hmmmm...
thanks
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes
Forums | Blogs | Tutorials | Code Snippets | Whitepapers | RSS Feeds | Advertising
All times are GMT -4. The time now is 11:02 am.
Newsletter Archive - Sitemap - Privacy Statement - Contact Us
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC