C++ age calculator

Introduction:

C++ age calculator in Turbo C \C++. On this page, you'll learn how we can create an age calculator using the C++ programming language. The age calculator is used to calculate the age of a living or non-leaving thing.

    Now on this page, we'll create an age calculator. This age calculator has the following features:

  • Beautiful design
  • Give accurate results
  • Having graphics
  • It has an age bar (it's an extra feature)
  • Show statements (for example, you are a child, a young, an adult, or an old)
  • You can recalculate an age unlimitedly

Program Info:

 

Project name

Age calculator in C++ using class (C++ age calculator)

Details

C++ age calculator. This program has a very easy and simple code. This program is made with the graphics of C++. It is a good-looking and simple program. It gives accurate results. If you are a beginner and you want to make an awesome C++ program, then this is perfect for you.

Type

C++

Published by

Codes Gallery

Developer

Chetan Bedakihale



To create an age calculator, follow the following procedures:

1. First, open the Turbo C\C++ IDE on your desktop.

2. Now, create a new file named "AGE_CALCULATOR" and save it with an extension". CPP" (AGE_CALCULATOR. CPP)

3. Now, in this file we include few a important header files as follows.

#include"iostream.h"
#include"graphics.h"
#include"stdio.h"
#include"conio.h"
#include"dos.h"
#include"time.h" 


4. Now, we print the title of the program on all the pages by the following code.

void title(){
   settextstyle(0,HORIZ_DIR,3);
      setcolor(GREEN);
   outtextxy(getmaxx()/4,getmaxy()/6,"AGE CALCULATOR");
}


5. Now, we will create a function named input_box(), to create small boxes. In these boxes, we take inputs from the user. We create 4 boxes and 1 border. The first box contains the current date. The current date will be taken from your computer system. The second box contains the date, the third box has the month, and the fourth box contains the year of birth.



void input_box(){

  setcolor(15);
    rectangle(200,150,450,300); // border
    rectangle(220,190,414,210); //it carry current date

    settextstyle(0,HORIZ_DIR,0);
    setcolor(GREEN);
    outtextxy(220,178,"Current date");

      // these shapes carry user date of birth
      setcolor(15);
      rectangle(220,237,265,257);
      rectangle(270,237,315,257);
      rectangle(320,237,414,257);

      settextstyle(0,HORIZ_DIR,0);
      setcolor(GREEN);
      outtextxy(220,225,"Enter DOB");
      outtextxy(235,262,"DD");
      outtextxy(285,262,"MM");
      outtextxy(350,262,"YYYY");
}


6. Now, we call both the title() function and the input_box() function in the main() function to execute the code.


void main(){
   int gd=DETECT,gm;
   initgraph(&gd,&gm,"C:\\TC\\BGI");
   title();
   input_box();
   
   getch();
   closegraph();
   }


code for age calculator in C++  | codes gallery
Fig 1


7. Now, we create a class named "UserData", in this class, we create a function named getdata(). This function is used to obtain input from the user to calculate an age. Before receiving the input, we print the current date in the first box. Here we use the 1st box to print the current date.

       Below is the code to get the date, month, and year which is used to calculate the age. We take the date in the second box, then next we take a month in the third box, and then we take the year in the fourth box.



class UserData{
   public:

   int day,month;
   int year;

   void getdata(){
	time_t now=time(0);
	tm *ltm=localtime(&now);
	   gotoxy(33,13);
	cout< tm_mday<<" - "<<1+ltm -> tm_mon<<" - "<<1900+ltm -> tm_year;
     setcolor(15);
      rectangle(235,272,250,274);

      gotoxy(30,16);  //It changes the position of a cursor
      cin>>day;

	rectangle(285,272,300,274);

      gotoxy(36,16);
      cin>>month;

      rectangle(350,272,383,274);
      gotoxy(43,16);
      cin>>year;
   }

};


code for age calculator in C++  | codes gallery
Fig 2


8. Next, we create a loading page. which indicates that age is calculated at a given date of birth. Below is a code.



void loading(){
       title();
	     setcolor(14);
	     settextstyle(0,HORIZ_DIR,0);
       outtextxy(getmaxx()/2.4,200,"Calculating....");
	     delay(15000);
	     cleardevice();

	     title();
}

code for age calculator in C++  | codes gallery
Fig 3


9. Now, we create a class named 'agecalc', which is a subclass of the parent class UserData. In this subclass, we create a function named calc(). This function is used to calculate the age of a given date of birth.



class agecalc : public UserData{
     public :

	 void calc(){
	   int one_month=31;
	   int one_year=12;
	   char y[5], m[5], d[5];

	time_t now=time(0);
	   tm *ltm=localtime(&now);
	 int c_date=ltm -> tm_mday;      //carry current date
	   int c_month=1+ltm -> tm_mon;  //carry current month
	   int c_year=1900+ltm -> tm_year;  //carry current year

	   //printing error message
	   if(year>c_year || month>12 || day>31){
	     settextstyle(0,HORIZ_DIR,1);
      setcolor(GREEN);
      outtextxy(200,getmaxy()/2,"Please enter a valid date of birth.");

	   }
	  else{
	    //calculating age according to the conditions
	   if(c_date>=day&&c_month>=month&&c_year>=year){
	       year=c_year-year;
	       month=c_month-month;
	       day=c_date-day;
	   }
	   if(c_date>=day&&c_month=year){
	       c_year=c_year-1;
	       c_month=c_month+one_year;

	       year=c_year-year;
	       month=c_month-month;
	       day=c_date-day;
	   }
	   if(c_date=month&&c_year>=year){
	     c_month=c_month-1;
	     c_date=c_date+one_month;
	     if(c_month=year){
	      c_year=c_year-1;
	      c_month=c_month+one_year;
	      c_month=c_month-1;
	      c_date=c_date+one_month;

	      year=c_year-year;
	       month=c_month-month;
	       day=c_date-day;
	   }

	    setcolor(15);

	setcolor(RED);
	settextstyle(0,HORIZ_DIR,1);
	setcolor(15);
	outtextxy(275,150,"-------------");
	setcolor(GREEN);
	outtextxy(280,160,"your age is");
	setcolor(15);
	outtextxy(275,170,"-------------");

	    setcolor(12);
	settextstyle(0,HORIZ_DIR,1);

	     //converting int to char using sprintf()
	     sprintf(y,"%d",year);
	     sprintf(m,"%d",month);
	     sprintf(d,"%d",day);

	     //printing converted values
	     outtextxy(250,200,y);
	     outtextxy(315,200,m);
	     outtextxy(380,200,d);
	    setcolor(15);
	     outtextxy(240,210,"years");
	    outtextxy(305,210,"months");
	     outtextxy(372.5,210,"days");

	    

	   }

	       }
     };


  After calculating an age, we print the calculated age on the screen as shown in Fig 4.


void main(){
   int gd=DETECT,gm;
   initgraph(&gd,&gm,"C:\\TC\\BGI");
   title();
   input_box();
   agecalc ud;    //creating an object of class agecalc
   ud.getdata();
   cleardevice();
   loading();
   ud.calc();
   
   getch();
   closegraph(); 


code for age calculator in C++  | codes gallery
Fig 4


10. Now, it's time to do something different in this program. We create an age bar and print the statements. The statement means, prints what you are. For example, you are young, a child(kid), an adult, or an old.


                  The age bar is used to show age by a bar (progress bar). The age bar has values from 0 to 100, which means that the bar only shows up to the age of 100 years.  For example, if your age is 30 then the bar will only be filled with a value of 30, and print the age at the end of the bar. If your age is 100, the bar will be filled up to 100. If your age is over 100, the bar will only be filled up to a maximum value which is 100, but whatever your age is that age will be printed at the end of the bar.


                 Therefore, to perform this activity we create two functions. The first function is called the age_bar() function and the second is called the statement() function. Now, in these two functions, we write their code.



void age_bar(int Year){
  char y[5];
  int cl;
	float incr=1.62;
  sprintf(y,"%d",Year);
  rectangle(240,230,402.5,240);
  for(int i=0;i396){
	 break;
       }

    }
      settextstyle(2,HORIZ_DIR,4);
      setcolor(15);
      outtextxy(cl,243,y);
}

void statement(int Year){
  settextstyle(0,HORIZ_DIR,1);
      if(Year<=14){
	 setcolor(GREEN);
	 outtextxy(270,265,"You are kid");
      }
      if(Year>=15 && Year<=24){
	 setcolor(14);
	 outtextxy(270,265,"You are young");
      }
       if(Year>=25 && Year<=64){
	 setcolor(13);
	 outtextxy(270,265,"You are adult");
      }
      if(Year>=65){
	 setcolor(4);
	 outtextxy(270,265,"You are old");
      }
}


After writing down their code we call them in a function named calc(), which exists in the subclass agecalc.


code for age calculator in C++  | codes gallery
Fig 5


code for age calculator in C++  | codes gallery
Fig 6


11. Now, we do some changes in the main() function which is a very important function of the program. Without the main function, we cannot run this program. We call all the functions in this main() function to perform their tasks. We repeatedly call all the functions in the do-while loop to calculate age unlimited.



void main(){
  char key;
  do{
   int gd=DETECT,gm;
   initgraph(&gd,&gm,"C:\\TC\\BGI");

   title();
   input_box();
   agecalc ud;    //creating an object of class agecalc
   ud.getdata();
   cleardevice();
   loading();
   ud.calc();
	  //Getting input from user to recalculate age
	  setcolor(11);
	  outtextxy(10,390,"You want to recalculate age? (Y/N) : ");
	  gotoxy(39,25);
	  cin>>key;

     }while(key=='Y'||key=='y');
      cleardevice();
      settextstyle(0,HORIZ_DIR,1);
      setcolor(GREEN);
      //Printing thanking statement
      outtextxy(200,getmaxy()/2,"Thank you for used this software.");
       getch();
   closegraph();
}


code for age calculator in C++  | codes gallery
Fig 7


code for age calculator in C++  | codes gallery
Fig 8

That's all friends! We completed our C++ age calculator program in a very successful way. I hope you understood the code very well. If you liked this program, please comment in the comments section, about how much you liked this program.


Full source code:


 





Post a Comment

You are welcome to share your ideas with me in the comments!

Previous Post Next Post