C++ Code for simple age calculator

rajesanthu -4 Tallied Votes 43K Views Share

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

Nick Evan commented: yuk. -3
#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();
}
ravenous 266 Posting Pro in Training

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 */
}
Hayzam_#include -3 Newbie Poster

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();
}
frogboy77 73 Posting Pro in Training

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

jin_yeugh 0 Newbie Poster

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.

Member Avatar for KumarUtkarsh
KumarUtkarsh

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

vinod insan 0 Newbie Poster

thanks to give calculator code but it shows error

thesoma300 0 Newbie Poster

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");
}
Lucaci Andrew 140 Za s|n

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);
}
mlesniak 22 Light Poster

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)

markwiering 22 Light Poster

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.

markwiering 22 Light Poster
WaltP commented: Why did you have to resurrect this bad thread just to post this about an equally bad piece of code? -3
Lucaci Andrew commented: Troll post. No need to post your own thread here, to "advertise" it saying how marvelous your source code is comparing to this one, because it's not! -1
takuya17 0 Newbie Poster

Good day..i am requesting if you can make a code of a simple registration system please.which indicates ..name.address.birthdate.age.and etc..thank you

NathanOliver 429 Veteran Poster Featured Poster

@takuya17 - Please don't resurrect dead threads with something completely unrelated. If you want to contract a coding job you need to do so under the Jobs and resumes section in business exchange.

Vishal patil78 0 Newbie Poster

ur uploaded program is great......

Youssef Faisal 31 Newbie Poster
#include <iostream>
using namespace std;


int main()
{ 
    int day , month, year , day_c,month_c,year_c,dd=0,md=0,yd=0;
    cout << "please enter your birthday date : day/month/year \n";
    cin >> day;
    cin >> month;
    cin >> year;
    cout << "please enter the current date : day/month/year \n";
    cin >> day_c;
    cin >> month_c;
    cin >> year_c;

    if (month_c > month && day_c > day)
    {
        yd = year_c - year;
        md = month_c - month;
        dd = day_c - day;**Bold Text Here**
    }

    else if (month_c > month && day_c < day)
    {
        yd = year_c - year;
        md = month_c - month - 1;
        dd = (30 - day) + day_c;
    }

    else if (month_c < month && day_c > day)
    {
        yd = year_c - year - 1;
        md = month_c;
        dd = day_c - day;
    }

    else if (month_c < month && day_c < day)
    {
        yd = year_c - year - 1;
        md = month_c;
        dd = (30 - day) + day_c;
    }

    else if (month_c == month && day_c > day)
    {
        yd = year_c - year;
        md = 0;
        dd = day_c - day;

    }

    else if (month_c == month && day_c < day)
    {
        yd = year_c - year - 1;
        md = month_c;
        dd = (30 - day) + day_c;

    }

    else if (month_c > month && day_c == day)
    {
        yd = year_c - year;
        md = month_c - month;
        dd = 0;
    }

    else if (month_c < month && day_c == day)
    {
        yd = year_c - year - 1;
        md = month_c;
        dd = 0;
    }

    else
        cout << "ERROR";


    cout << "your age exaclly is " << yd << " years, " << md << " months and " << dd << " days \n \n";

}
Schol-R-LEA commented: Please check the date of a post before replying to it. -3
Dani commented: Thanks for contributing a better way +34
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.