Saturday, February 19, 2011

Job question and why I think you should use MingW

I had a job interview and they asked me to write a program to read 10 registers with the following fields:

Name, address, phone and age.

And I should print out the two eldest in the list. I could use any language I wanted, then I thought if I chose pure ANSI C would be really painful because I would need to sort these registers using qsort. Then I thought about using C++ and STL, that is really the easiest way to do that fast, and to be honest the shortest and more concise way. The STL supplies me the sort method that can take, a pointer iterator to the first item and one pointer to the last.

When I was there I just forget two important things, when I sort a container I sort it from low to high, as I would override the < operator. And I forgot to implement the constructor.

Anyway I came to home and tried to implement it using DevCpp, which has MingW built in, which is a gcc port for Windows. I think that DevCpp uses a real old version of MingW, but that is no excuse for not compiling STL based code properly. If I use MingW the code below won't compile. I just did what is better for C++ programmers on Windows, I used Visual C++ Express 2008. And it compiles. The copy constructor is a bonus.

Edit: New versions of MingW are quite decent and will compile this code.  Use Qt Creator with MingW bundled instead of Visual C++.

#include "stdafx.h"
#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
using namespace std;

class Registro {
     Registro(string& Nome, string& Endereco, string& telefone, int idade );
     Registro(const Registro& reg);
     bool operator<(const Registro& reg);
     string Nome;
     string Endereco;
     string telefone;
     int idade ;

Registro::Registro(string& Nome, string& Endereco, string& telefone, int idade )
     this->Nome = Nome;
     this->Endereco = Endereco;
     this->telefone = telefone;
     this->idade = idade;                    

Registro::Registro(const Registro& reg)
    this->Endereco = reg.Endereco;
    this->Nome = reg.Nome;
    this->telefone = reg.telefone;
    this->idade = reg.idade;

bool Registro::operator<(const Registro& reg)
    if ( this->idade < reg.idade)
        return true;
    return false;

int _tmain(int argc, _TCHAR* argv[])
    int cnt = 0;
    vector<Registro> regs;
    while ( cnt < 4 )
          string Nome;
          string telefone;
          string end;
          int idade = 0;
          cout << "Digite o Nome: " ;
          cin >> Nome;
          cout << endl;

          cout << "Digite o Endereco: " ;
          cin >> end;
          cout << endl;
          cout << "Digite o telefone: " ;
          cin >> end;
          cout << endl;         
          cout << "Digite a idade: " ;
          cin >> idade;
          cout << endl;
          Registro reg(Nome,end,telefone,idade);
    sort(regs.begin(), regs.end());
    cout << "O Mais idoso: " << regs[regs.size()-1].Nome  << endl;
    cout << "O segundo mais idoso: " << regs[ regs.size()-2].Nome << endl;
    return 0;

No comments: