0

I'm writing a program, in which I have some competitors, which are defined by structure. I also have a date structure.

struct date{ int d; int m; int y;};

I must sort competitor by their age, and I have no idea how to get age of competitors, to compare it.

I also tried this:

void age(tekmovalec comp){
#define _CRT_SECURE_NO_WARNINGS
time_t theTime = time(NULL);
struct tm *aTime = localtime(&theTime);

int day = aTime->tm_mday;
int month = aTime->tm_mon + 1; // Month is 0 - 11, add 1 to get a jan-dec 1-12 concept
int year = aTime->tm_year + 1900;

int age1, age2;
if(comp.rojDat.m > month){
          age1=year-comp.rojDat.y-1;
          age2=(12-month) + comp.rojDat.m;
    }else{
         age1=year-comp.rojDat.y;
         age2=12-comp.rojDat.m;
    }

 int age3 = age1*12 + age2;
 comp.age=age3;

}

But it returns error that localtime is unsafe.

PeeHaa
  • 71,436
  • 58
  • 190
  • 262
n32303
  • 831
  • 1
  • 10
  • 25

4 Answers4

3

As you want to sort by date, you may want to take a look at std::sort. For comparison, you need to write a function comp that compares 2 competitors (you need just to compare their ages, using basic comparations between days, months and years).

bool comp(competitor a, competitor b){
    if(a.age.y < b.age.y)
        return true;
    else if(a.age.y == b.age.y && a.age.m < b.age.m)
        return true;
    else if(a.age.y == b.age.y && a.age.m == b.age.m && a.age.d < b.age.d)
        return true;
    else 
        return false;
}

Note that this is not the best implementation (actually it is pretty messy), but it's purpose is to give you a hint, not to be used as it is.

Another way to achive this is to overload comparison operators for competitors.

Paul92
  • 8,827
  • 1
  • 23
  • 37
  • 1
    In C++11, you can reduce that to `std::tie(a.age.y, a.age.m, a.age.d) < std::tie(b.age.y, b.age.m, b.age.d)` – Mike Seymour Dec 02 '13 at 19:10
  • If C++11 isn't available, you can use `boost` to do what @MikeSeymour said with `boost::tie`. – mpark Jan 05 '14 at 21:42
1

I presume you are using Visual studio. _CRT_SECURE_NO_WARNINGS needs to be a preprocessor definition (i.e. Seen before any #includes or code)
Right click on the project, get the 'Properties' menu and find the C/C++ preprocessor definitions and put it thre. To actually compare times, it might be easier if you use mktime after transforming your dat struct into a tm, but you'll have trouble if anyone was born before 1970. See here

Community
  • 1
  • 1
doctorlove
  • 18,872
  • 2
  • 46
  • 62
1

There is no need for localtime. You do not need age; what you need is a way to compare age. To determine which of us is older, we do not need to know what year this is, all we need to know is which of us was born earlier.

Write a function that compares two birthdates:

bool operator<(const date &A, const date &B)
{
  ...
}

Then use that to build a function that compares two competitors, then use that to build a function that sorts competitors.

Beta
  • 96,650
  • 16
  • 149
  • 150
1

instead of localtime() which can cause issues (not thread safe), you can use localtime_r() which should not have warnings :

struct tm *localtime_r(const time_t *restrict timer, struct tm *restrict result);

struct tm mytime; // use this instead of aTime

 localtime_r(&theTime, &myTime)
 int day = myTime.tm_mday;
 int month = myTime.tm_mon + 1; // Month is 0 - 11, add 1 to get a jan-dec 1-12 concept
 int year = myTime.tm_year + 1900;

This might also be localtime_s()

Glenn Teitelbaum
  • 10,108
  • 3
  • 36
  • 80