Find the oldest person by Lithuanian National Identification Number I made a program which parses the NIDs from kodai.txt file and calculates the oldest woman and man and outputs their DOB.

#include &lt;iostream&gt;
#include &lt;fstream&gt;
#include "math.h"
using namespace std;

enum Genders {
    Man, Woman      
};

struct Person {
    Genders gender;
    int dobYear;
    int dobMonth;
    int dobDay;
    long code;  
};

Person parsePerson(long kodas) {
    Person person;

    kodas = kodas / 10000;
    int firstNumber = kodas / 1000000;      

    switch(firstNumber) {
        case 1:
            person.gender = Genders::Man;
            person.dobYear = 1800;
            break;
        case 2:
            person.gender = Genders::Woman;
            person.dobYear = 1800;
            break;
        case 3:
            person.gender = Genders::Man;
            person.dobYear = 1900;
            break;
        case 4:
            person.gender = Genders::Woman;
            person.dobYear = 1900;
            break;
        case 5:
            person.gender = Genders::Man;
            person.dobYear = 2000;
            break;
        case 6:
            person.gender = Genders::Woman;
            person.dobYear = 2000;
            break;
    }

    kodas = kodas % 1000000;
    int years = kodas / 10000;

    person.dobYear += years;

    kodas = kodas % 10000;

    person.dobMonth = kodas / 100;

    kodas = kodas % 100;

    person.dobDay = kodas;

    cout &lt;&lt; person.dobDay &lt;&lt; endl;
    return person;
}

bool comparePerson (Person p1, Person p2) {
    if (p1.dobYear &lt; p2.dobYear) {
        return true;
    }

    if (p1.dobYear == p2.dobYear) {
        if (p1.dobMonth &lt; p2.dobMonth) {
            return true;
        }

        if (p1.dobMonth == p2.dobMonth) {
            if (p1.dobDay &lt; p2.dobDay) {
                return true;
            }
        }
    }

    return false;
}

int main () {
    ifstream kodai("kodai.txt");

    Person oldestMan;
    Person oldestWoman;

    oldestMan.dobYear = 9999;
    oldestMan.dobMonth = 9999;
    oldestMan.dobDay = 9999;

    oldestWoman.dobYear = 9999;
    oldestWoman.dobMonth = 9999;
    oldestWoman.dobDay = 9999;

    long code; 

    while (kodai &gt;&gt; code) {
        Person person = parsePerson(code);          

        if (person.gender == Genders::Woman) {
            if (comparePerson(person, oldestWoman)) {
                oldestWoman = person;
            }   
        } else if (person.gender == Genders::Man) {
            if (comparePerson(person, oldestMan)) {
                oldestMan = person;
            }
        }   

    }


    cout &lt;&lt; "Oldest man: " &lt;&lt; oldestMan.dobYear &lt;&lt; "-" &lt;&lt; oldestMan.dobMonth &lt;&lt; "-" &lt;&lt; oldestMan.dobDay &lt;&lt; endl;
    cout &lt;&lt; "Oldest woman: " &lt;&lt; oldestWoman.dobYear &lt;&lt; "-" &lt;&lt; oldestWoman.dobMonth &lt;&lt; "-" &lt;&lt; oldestWoman.dobDay &lt;&lt; endl;
}


Example kodai.txt:

33309240064
40212220064
50211020064
10001010064
20001010064


Can you give me any suggestions or problems that you might see?
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;
}