Search code examples
c++eclipsemacosdebuggingexit-code

Why is my C++ app terminated with exit value -1 but some code logic isn't being executed?


Well basically I have this main method

int main() {
GameMapDriver* gameMapDriver = new GameMapDriver();
int players = 0;
std::cout << "How many players are going to play the game? Insert a numeric value from 2 to 5  : ";
std::cin >> players;

if ( players == 2 )
    gameMapDriver->createGameMapForNumberOfPlayers(2);
switch (players)  {
case 2 : {
    gameMapDriver->createGameMapForNumberOfPlayers(2);
    break;
}
case 3: {
    gameMapDriver->createGameMapForNumberOfPlayers(3);
    break;
}
case 4: {
    gameMapDriver->createGameMapForNumberOfPlayers(4);
    break;
}
case 5: {
    gameMapDriver->createGameMapForNumberOfPlayers(5);
    break;
}
default: std::cout << "Invalid number of players";
break;
}
return 0;
}

and for the gameMapDriver->createGameMapForNumberOfPlayers(2) method (please note that the first if statement is for debugging purposes, it shouldn't affect anything), I have this (I cropped the picture, the switch is coded correctly with default case at the end):

GameMap * GameMapDriver::createGameMapForNumberOfPlayers(int players){
std::cout << "Creating map";
    switch (players){
    case (TWO):{
        this->gameMap = NULL;
        delete this->gameMap;
        this->gameMap = new GameMap(TWO);
        std::cout << "Game map for two players created";
        return this->gameMap;
        break;
    }
    case (THREE) : {
        this->gameMap = NULL;
        delete this->gameMap;
        this->gameMap = new GameMap(THREE);
        std::cout << "Game map for three players created";
        return this->gameMap;
        break;
    }
    case (FOUR): {
        this->gameMap = NULL;
        delete this->gameMap;
        this->gameMap = new GameMap(FOUR);
        std::cout << "Game map for four players created";
        return this->gameMap;
        break;
    }
    case (FIVE):{
        this->gameMap = NULL;
        delete this->gameMap;
        this->gameMap = new GameMap(FIVE);
        std::cout << "Game map for five players created";
        return this->gameMap;
        break;
    }
    default: {
        std::cout << "Invalid number of players";

        return this->gameMap;
        break;
    }
    }
    }

The problem is that from createGameMapForNumberOfPlayers, line 21 std::cout << "Creating map"; it doesn't even print on console.

I run my app, I enter "2", I read it, I should be passing it to my method but it just says (exit value -1).It doesn't even print "Creating Map" at all.

What's happening? :(


Solution

  • You should use a debugger, but if you can't then at least you should flush cout to get a better idea of where your program is going wrong.

    std::cout << "Creating map" << std::endl;
    

    std::endl will add a newline to the output stream and more importantly force output to happen immediately. I would guess that your program is crashing while the output is still waiting to happen.