Question

#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
Was it helpful?

Solution

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).

OTHER TIPS

You can't modify the sorted values while sorting. It's evil. The sort algorithm may launch "format C:" instead of actually sorting.

Beside, values to sort are passed by value to the callback, so your modification are lost.

  1. Process your list of date for adjustments
  2. Sort with a non modifying callback

(also, initialize your dates without the octal prefix, as in the @Lightness Races in Orbit answer)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top