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!

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

Leave a Reply :)