Find the oldest person by Lithuanian National Identification Number
Headers

Prefer &lt;cmath&gt; to &lt;math.h&gt; when writing new C++ code.  It seems it's not required in this program, so prefer to omit it instead!

&lt;fstream&gt; is only required for the test program - I'll move it to after the function.  (In fact, I'll use a string stream instead, to make the test program self-contained).

Avoid using namespace std;, especially in headers - there's many answers here on Code Review that explain why it's harmful.

Types

Enumeration types are normally named as singular, unless they are intended to be combined as bitmask values.  That's a useful convention to maintain, so let's rename Genders to Gender.  We never want Gender to implicitly convert to and from integer values, so we can make it a strong enumeration, using enum class.

We can improve the cohesion of Person by observing that all its properties are derived from the ID number.  Instead of requiring client code to populate all fields, we can supply a constructor which accepts the ID number and populates the whole structure from it.

We might even choose to store only the number (to reduce the size of Person) and compute the derived properties when needed.  That would be an appropriate strategy if we had a much larger number of input lines.

We can present a public interface that doesn't depend on our implementation choice:

class Person
{
public:
    Person(long kodas);
    Gender gender() const;
    int dobYear() const;
    int dobMonth() const;
    int dobDay() const;
    long code() const;
};


I also suggest making the comparison and formatting functions into members:

class Person
{
public:
    Person(long kodas);
    Gender gender() const;
    bool older_than(const Person&amp; other) const;
    std::ostream&amp; operator&lt;&lt;(std::ostream&amp;) const;
};


(Note that the birth date no longer needs to be public).

Implementation

We don't need to split the code into year, month and day to compare - we can just sort the numbers directly (because 10000*year + 100*month + day will sort the same way).  If we did need to sort based on multiple fields, we could use std::make_tuple() or std::tie() to simplify the code.

When printing month and day, it's usual to zero-fill single-digit numbers.

We should check that we reached end-of-stream - if not, that indicates a read error, and we can't claim to have found the answer.



Modified code

#include &lt;iosfwd&gt;

enum class Gender {
    Man, Woman
};

class Person
{
    long kodas;
public:
    Person(long kodas);
    Gender gender() const;
    bool older_than(const Person&amp; other) const;

    friend std::ostream&amp; operator&lt;&lt;(std::ostream&amp;, const Person&amp;);

private:
    long remove_gender() const;
};


    

#include &lt;iomanip&gt;
#include &lt;ostream&gt;
#include &lt;stdexcept&gt;
Person::Person(long kodas)
    : kodas{kodas}
{
    if (kodas &lt; 0)
        throw std::domain_error("negative ID");
}

Gender Person::gender() const
{
    auto firstDigit = kodas / 10000000000;
    return firstDigit % 2 ? Gender::Man : Gender::Woman;
}

bool Person::older_than(const Person&amp; other) const
{
    return remove_gender() &lt; other.remove_gender();
}

std::ostream&amp; operator&lt;&lt;(std::ostream&amp; os, const Person&amp; p)
{
    auto n = p.remove_gender() / 10000;
    int century = 18 + n/2000000;
    n %= 1000000;

    int year = n / 10000;
    n %= 10000;

    int month = n / 100;
    int day = n % 100;

    // save stream settings
    auto fill = os.fill();

    return os &lt;&lt; std::setfill('0')
              &lt;&lt; century
              &lt;&lt; std::setw(2) &lt;&lt; year
              &lt;&lt; '-'
              &lt;&lt; std::setw(2) &lt;&lt; month
              &lt;&lt; '-'
              &lt;&lt; std::setw(2) &lt;&lt; day
              &lt;&lt; std::setfill(fill);
}

long Person::remove_gender() const
{
    return gender() == Gender::Woman ? kodas - 10000000000 : kodas;
}




#include &lt;sstream&gt;
#include &lt;iostream&gt;
int main () {
    std::istringstream kodai(
                        "33309240064\n"
                        "40212220064\n"
                        "50211020064\n"
                        "10001010064\n"
                        "20001010064\n"
                        );

    Person oldestMan(99999999999);
    Person oldestWoman(89999999999);

    long code;
    while (kodai &gt;&gt; code) {
        Person person{code};
        switch (person.gender()) {
        case Gender::Woman:
            if (person.older_than(oldestWoman))
                oldestWoman = person;
            break;
        case Gender::Man:
            if (person.older_than(oldestMan))
                oldestMan = person;
            break;
        }
    }
    if (!kodai.eof())
        throw std::runtime_error("Failed to read whole file");

    std::cout &lt;&lt; "Oldest man: " &lt;&lt; oldestMan &lt;&lt; std::endl;
    std::cout &lt;&lt; "Oldest woman: " &lt;&lt; oldestWoman &lt;&lt; std::endl;
}