1.11M Members

C++ Code for simple age calculator

 
-5
 

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();
}
 
1
 

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 */
}
 
1
 

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();
}
 
0
 

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

 
0
 

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.

KumarUtkarsh
Deleted Member
 
0
 

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

Thanks Man

 
0
 

thanks to give calculator code but it shows error

 
0
 

these codes are wrong!!!!
My birthday is on 1997 9, and it is 2012 7 now
it says i am 15 year and 3 month old but I'm not
I have a solution for this

#include<iostream>
using namespace std;
int main()
{
    system("TITLE how old are you?");
    system("color f3");
    int yearnow,yearthen,monthnow,monththen,age1,age2;
    cout<<"\t\t\tEnter the current year and month \n\t\t\t(eg. 1997, enter, 7, enter):\n ";
    cin>>yearnow;
    cin>>monthnow;
    cout<<"Enter your birthyear and month: \n";
    cin>>yearthen;
    cin>>monththen;
    if(monththen >12 || monththen<1)return 1;
    if(monththen > monthnow){
                   age1=yearnow-yearthen-1;
                   age2=(12-monththen) + monthnow;
                   }else{
                         age1=yearnow-yearthen;
                         age2=12-monththen;
                         }
    cout<<"\n\n\t\t\tYou are "<<age1<<" year and "<<age2<<" moth old";
    system("pause>>void");
}
 
0
 

Assuming your system time is ok, this will calculate an aproximate of your years:

#include <ctime>
#include <iostream>
using namespace std;

int main(){
    struct tm date = {0};
    int day, month, year;
    cout<<"Year: ";
    cin>>year;
    cout<<"Month: ";
    cin>>month;
    cout<<"Day: ";
    cin>>day;
    date.tm_year = year-1900;
    date.tm_mon  = month-1;
    date.tm_mday = day;
    time_t normal = mktime(&date);
    time_t current;
    time(&current);
    long d = (difftime(current, normal) + 86400L/2) / 86400L;
    cout<<"You have~: "<<d/365.0<<" years.\n";
    return (0);
}
 
0
 

The age calculator is not quite right because it does not calculate the months correctly.
If the current month is less than the birth month then the years are shown as one too many. The months are calculated as 12 minus the birth months, which is incorrect in most instances.
The calculation should be
If (current month minus birth month) > 0 then
years = current year minus birth year
months = current month minus birth month
else
years = (current year minus birth year) - 1
months = 12 - (birth month minus current month)

 
0
 

Rajeesh.N.Santhu, you made too many mistakes. He is not showing the months correctly. I am going to rewrite this program in Dutch, and whitout mistakes of course. :-D

Thanks for the idea. I already made a program to calculate temperature scales, I made a program which calculates the average and the sum, I made a program which is showing you random zeros and ones, I made a program which is giving random numbers, I made a mini-game where you can wed coins (beginning with 200 coins), and now I am going to rewrite a program which calculates your age.

You
Post:
Start New Discussion
Tags Related to this Article