My first program in C++ (Age Calcuator)

Hello, I’ve started recently to learn C++ programming. And this is the first program I write in this awesome language:

// my first program in C++
#include <iostream>
using namespace std;

int main()
{
    time_t theTime = time(NULL);
    struct tm *aTime = localtime(&theTime);
    int currentday = aTime->tm_mday;
    int currentmonth = aTime->tm_mon + 1;
    int currentyear = aTime->tm_year + 1900;
    int day;
    int month;
    int year;
    cout << "Please enter your year of born: ";
    cin >> year;
    cout << "Please enter your month of born: ";
    cin >> month;
    cout <<"Please enter your day of born: ";
    cin >> day;
    cout << "You are: ";
    if (currentday < day) {
        currentmonth=currentmonth-1;
        currentday=currentday+30;
        cout << currentday-day << " days and ";
    }
    else {
        cout << currentday-day << " days and ";
    }
    if (currentmonth < month) {
        currentyear=currentyear-1;
        currentmonth=currentmonth+12;
        cout << currentmonth-month << " months and ";
    }
    else {
        cout << currentmonth-month << " months and ";
    }
    cout << currentyear-year << " years old\n";
}

Very simple I know, but it should be a good start for me to look forward in coding something better in C++. Maybe starting a project who knows? 😀

See ya!

Advertisements

5 thoughts on “My first program in C++ (Age Calcuator)

Leave a Reply :)

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s