Skip to content
Advertisement

Strange Output Difference Between GCC and MinGW (C++)

I’m working on a project for a computer science class. I wrote the code and tested it using MinGW, and it works fine. I then copied the code over to the university’s Linux server and tested it there, since that’s what my professor grades assignments on. The output is very different – like it printed out a carriage return in the middle of an output.

The interesting thing is, the issue also appears when compiled and run with Cygwin GCC (32-bit, 4.7.2). Does anyone have any insight as to why this is happening and how to fix it?

Code and a sample input file (to be named lifepath.txt and placed in the same directory as the executable).

#include <iostream>
#include <fstream>
#include <map>
#include <set>
#include <string>
#include <cstdlib>

using namespace std;

bool younger(string s1, string s2) {
    string y1 = s1.substr(s1.rfind('-')+1);
    string y2 = s2.substr(s2.rfind('-')+1);

    if(y1 < y2) return true;
    else if(y1 > y2) return false;

    string m1 = s1.substr(s1.find('-')+1, s1.rfind('-')-s1.find('-')-1);
    string m2 = s2.substr(s2.find('-')+1, s2.rfind('-')-s2.find('-')-1);

    if(m1 < m2) return true;
    else if(m1 > m2) return false;

    string d1 = s1.substr(0, s1.find('-'));
    string d2 = s2.substr(0, s2.find('-'));

    return d1 < d2;
}

int main() {
    ifstream in("lifepath.txt");
    map<int, int> pathcounts;
    set<string> bdays;
    string oldest, youngest;

    map<string, int> years;

    string line;
    while(getline(in, line)) {
        string monthday = line.substr(0, line.rfind('-'));
        bdays.insert(monthday);
        string num = line;
        while(num.find('-') != string::npos) num.erase(num.find('-'));
        int path = atoi(num.c_str()) % 9 + 1;
        pathcounts[path]++;

        if(youngest == "" || younger(line, youngest)) youngest = line;
        if(oldest == "" || younger(oldest, line)) oldest = line;

        years[line.substr(line.rfind('-')+1)]++;

    }

    for(int i = 1; i <= 9; i++) {
        cout << i << ": " << pathcounts[i] << endl;
    }

    cout << endl;

    cout << "The oldest birthday is " << oldest << endl;
    cout << "The youngest birthday is " << youngest << endl;

    cout << endl;

    cout << "There are " << bdays.size() << " unique birthdays" << endl;

    cout << endl;

    string modeyear = "";
    int modeyearcount = 0;
    for(map<string, int>::iterator it = years.begin(); it != years.end(); it++) {
        if(it->second > modeyearcount) {
            modeyear = it->first;
            modeyearcount = it->second;
        }
        else if(it->second == modeyearcount) modeyear += " " + it->first;
    }

    cout << "The mode of the birthyears is " << modeyear;
    cout << ", appearing " << modeyearcount << " times" << endl;


    return 0;
}

I will post a link to an image of the output as a reply, since I don’t have enough rep to have more than two links in a post.

Advertisement

Answer

This is because different line endings, r in Windows’ case means just go to the beginning of the line, when you’re reading data from a file and set the years array. You use keys, not just #### but ####r, and this last symbol is displayed during cout.

So you need to change your code:

years[line.substr(line.rfind('-')+1, 4)]++;
Advertisement