954,483 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?

C++ Code for simple age calculator

By Rajeesh.N.Santhu on Dec 13th, 2010 12:00 pm

Simple C++ program for age calculation
NB:Excuse me experts,,this is only for beginners.........

Instructions////
1.Copy the code
2.paste it in a text file
3.save the text file as agecalc.cpp and select file type all with in the text file.
4.paste this file inside your bin folder where the Turbo C resides.
5.Open turbo C and do File->Open->Agecalc.cpp
6.press F2 to save and F9 to compile.
7.press ctrl+F9 to run the program
8.once you have compiled the program search for agecal.exe in your bin folder

#include<iostream.h>
#include<conio.h>
void main()
{
int birthmonth,birthyear;
int currentmonth,currentyear;
int agey,agem;
cout<<"\n\n\t\t\tRaje's Age Calculator\n\n";
cout<<"Enter Your Birth Year(Eg:1989):";
cin>>birthyear;
cout<<"\n\nEnter Your Birth Month(Eg:7):";
cin>>birthmonth;
cout<<"\nEnter The Current Month(Eg:7):";
cin>>currentmonth;
cout<<"\nEnter The Current Year(Eg:2010):";
cin>>currentyear;
agey=currentyear-birthyear;
agem=12-birthmonth;
cout<<"\n\n\t\tYour Age is "<<agey<<" Years And "<<agem<<" Months ";
getch();
}

I think that you should probably think about indenting your code a little, so that the various blocks are a bit easier to pick out.

Also, I think that it's generally best to have main() return int rather than void , that way you can check the return value of the program and see if everything went as expected.

So, your code properly formatted, with some other little bits corrected:

#include<iostream>      /* Removed the ".h" from the header name, it's deprecated in C++ */
#include<conio>         /* Same for this one */

/* Added a line to use the std namespace, otherwise you'd have to use std::cout & std::cin instead of cout & cin */
using namespace std;

int main()   /* Changed the return-type to int */
{
   /* this code is now indented, so that we know it's in main() */
   int birthmonth,birthyear;
   int currentmonth,currentyear;
   int agey,agem;
   
   /* Adding carriage returns between distinct blocks of code can aid readability */
   cout << "\n\n\t\t\tRaje\'s Age Calculator\n\n";   /* Used "\'" instead of just "'", since this will cause an error */
   cout << "Enter Your Birth Year(Eg:1989):";
   cin >> birthyear;

   /* Added some white space between the elements of these statements to aid readability */
   cout << "\n\nEnter Your Birth Month(Eg:7):";
   cin >> birthmonth;

   /* Added this bit to demonstrate return values from main */
   if(birthmonth > 12 || birthmont < 1)
      return 1;   /* return 1 when something's gone wrong */
      
   cout << "\nEnter The Current Month(Eg:7):";
   cin >> currentmonth;

   cout << "\nEnter The Current Year(Eg:2010):";
   cin >> currentyear;

   agey = currentyear - birthyear;
   agem = 12 - birthmonth;

   cout << "\n\n\t\tYour Age is " << agey << " Years And " << agem << " Months ";
   
   getch();
   return 0;    /* We now return "0", so that we can check from another program if this one finished OK */
}
ravenous
Posting Pro
516 posts since Jul 2005
Reputation Points: 269
Solved Threads: 92
 

iam using Dev-C++ Its Working Like a Charm

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

using namespace std;
int main()
{
int birthmonth,birthyear;
int currentmonth,currentyear;
int agey,agem;
cout<<"\n\n\t\t\tRaje's Age Calculator\n\n";
cout<<"Enter Your Birth Year(Eg:1989):";
cin>>birthyear;
cout<<"\n\nEnter Your Birth Month(Eg:7):";
cin>>birthmonth;
cout<<"\nEnter The Current Month(Eg:7):";
cin>>currentmonth;
cout<<"\nEnter The Current Year(Eg:2010):";
cin>>currentyear;
agey=currentyear-birthyear;
agem=12-birthmonth;
cout<<"\n\n\t\tYour Age is "<<agey<<" Years And "<<agem<<" Months ";
getch();
}
Hayzam_#include
Newbie Poster
16 posts since Nov 2010
Reputation Points: 7
Solved Threads: 2
 

@hayzam take some time and read the post from ravenous. there is plenty to be learned there.

frogboy77
Posting Pro in Training
481 posts since Aug 2010
Reputation Points: 100
Solved Threads: 39
 

I definitely agree with ravenous in that an organized source is always best for you or anyone else working with the code. Otherwise a very good, precise age calculator.

jin_yeugh
Newbie Poster
1 post since Dec 2010
Reputation Points: 10
Solved Threads: 0
 

I think you should taken current year and current month and same day using Computer time with help of header file because most user want program is made in such way which only gives output without more or unnessary input.

Thanks Man

KumarUtkarsh
Newbie Poster
12 posts since Mar 2011
Reputation Points: 10
Solved Threads: 0
 

thanks to give calculator code but it shows error

vinod insan
Newbie Poster
1 post since Mar 2012
Reputation Points: 10
Solved Threads: 0
 

Post: Markdown Syntax: Formatting Help
You