Convert YYMMDD to YYYYMMDD and sort them

advertisements
#include <iostream>     // std::cout
#include <cstdlib>
#include <climits>
#include <algorithm>
#include <cmath>
#include <fstream>

using namespace std;

struct student{
    int ID;           // ID
    string firstname; // first name
    string lastname;  // last name
    int date;         // YYMMDD

    static bool sort_date(student a, student b){
        if(a.date < 150000){
            a.date += 20000000;
        }
        else{
            a.date += 19000000;
        }
        if(b.date < 150000){
            b.date += 20000000;
        }
        else{
            b.date += 19000000;
        }
        return data1 < data2;
    }

};

int main(){

    student students[5];

    students[0].date = 000101;
    students[1].date = 951230;
    students[2].date = 570509;
    students[3].date = 120915;
    students[4].date = 020324;

    stable_sort(students, students + 5, student::sort_date);

    ofstream file;
    file.open("sort_date.txt");
    for(int i = 0; i < 5; i++){
        file << students[i].date << endl;
    }

    return 0;
}

This is my program and I have dates in format YYMMDD and I want to convert them in YYYYMMDD and sort them, but there is a problem in my algorithm which I can't find.

For some reason the sorted output in file looks like this:

570509
951230
65
8404
120915

I want my program, first, to take the dates:

students[0].date = 000101;
students[1].date = 951230;
students[2].date = 570509;
students[3].date = 120915;
students[4].date = 020324;

Then convert them to YYYYMMDD and sort them and output them in YYYYMMDD format. But the output is somehow wrong and convert 101 into 65 for some reason as well.

The correct output should be:

19570509
19951230
20000101
20020324
20120915


The integer literal 000101 is interpreted as an octal representation of the decimal 65, because of the leader zero.

A literal is a number written out in your source code. This does not include characters in a file, or characters inputted by a user into an I/O stream.

Any integer literal beginning 0 is an octal integer literal.

To stick to decimal literals, do not use leading zeroes.

students[0].date =    101;
students[1].date = 951230;
students[2].date = 570509;
students[3].date = 120915;
students[4].date =  20324;

As discussed above, this is kind of a strange way to do human-readable dates (typically you'd use a string for this, and leave the integer storage to some internal representation such as a UNIX timestamp).