Age Calculator in C++

This is my first program in C++ that truly uses object oriented programming capabilities. Some of the code is a little redundant. I wrote it this way to help me understand what is really going on when your passing data around.

I thought I’d throw the code up here for anyone who would like to use it. Here you go.

If you find an error/bug, please leave me a comment!

Age Calculator.txt



#include <iostream>
#include <stdio .h>
#include <time .h>
#include <string>

using namespace std;

class date
{
public:
int day;
int month;
int year;
int daysold;
int monthsold;
int yearsold;

//I defined two methods. The "birth" method and "age" method both accept three integer variables.
void birth(int, int, int);
void age(int, int, int);
};

void date::birth(int b_day, int b_month, int b_year)
{
// This mearly takes the birthday (DD MM YYYY) entered and writes it to the class's variables for the other method to use.

day = b_day;
month = b_month;
year = b_year;

//cout < < day << "/" << month << "/" << year << "\n";           // This was for me to test what data is really reaching those three variables.

}

void date::age(int c_day, int c_month, int c_year)
{
//This method actually does something useful! Useing both the current date and the birthdate, it will calculate "monthsold", "daysold", and "yearsold."

//NOTE: THESE ALGORYTHMS DO NOT CURRENTLY RETURN ACURATE VALUES AS THEY DO NOE ACCOUNT FOR LEAP YEARS. Hmm... mabe something to change when it's not 11:53 pm

monthsold = (((c_year*12) + c_month) - ((year*12) + month));

daysold = (monthsold*30) + day;

yearsold = monthsold/12;
}

int main()
{
// -----------------   VARIABLES   -------------------
int b_day;
int b_month;
int b_year;
int c_day;
int c_month;
int c_year;

//------------------ GET SYS TIME  -------------------

time_t tim;  //create variable of time_t
time(&tim); //pass variable tim to time function
cout << "Program initiated on " << ctime(&tim) << "\n\n"; // this translates what was returned from time() into a readable format

time (&tim);
struct tm * ptm= localtime(&tim);
c_day = ptm->tm_mday;
c_month = (ptm->tm_mon)+1;
c_year = ptm->tm_year + 1900;

//------------------ START ASKING QUESTIONS -------------------

cout < < "In what year was the subject born? (i.e. 1982)\n";
cin >> b_year;
cout < < "\nIn what month was the subject born? (i.e. 09)\n";
cin >> b_month;
cout < < "\nOn what day was the subject born? (i.e. 27)\n";
cin >> b_day;
cout < < "\n";

//------------------ VERIFY INFORMATION ------------------
//Define an object from the "date" class. I'll call my object "human."
date human;

// Send the collected data to the "birth" method
human.birth(b_day, b_month, b_year);

//Put a confirmation message up.
cout << "You said that the subject was born on " << human.month << "/" << human.day << "/" << human.year << " if I'm not mistaken.\n";

//Send current date to AGE method to run calculations
human.age(c_day, c_month, c_year);

//------------------ DISPLAY CALCULATIONS --------------------

cout << "\nOk. In case you were wondering, the subject is somewhere around\n\n";
cout << human.yearsold << " years \n\n";
cout << "...or...\n\n";
cout << human.monthsold << " months \n\n";
cout << "...or...\n\n";
cout << "aprox. " << human.daysold << " days old\n\n\n";
cout << " - - - | - - -\n\n";
cout << "Sorry, that's my best extimate.\n";

//---------------------- QUIT MAIN PROGRAM ----------------------cout << "\n Press any key and hit ENETER to close." << endl;

cin >> c_day;
//int getchar();    // For some reason or another, this command has never worked for me. That's why I wrote the above line so that the user can actually read the data output. It's not as nice, but it works.
return (0);

}

Rate this Post/Page