-5

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? :(

  • Could you provide code, not a photo of code ? :) – BartekPL Feb 16 '18 at 07:37
  • Have you tried stepping through the code using a debugger? What happened? (As a side note: it is more convenient for us, if you post code as text instead of images) – Jerome Reinländer Feb 16 '18 at 07:37
  • Please don't use images to show your code. Use text. It'd also help if you provided us with a [mcve](https://stackoverflow.com/help/mcve). Now, this is unrelated to your issue but, in the first line of the main, avoid `new` when not necessary. This is not really that harmful here, but `GameMapDriver gamemapdriver;` is simpler and works just as fine. – Caninonos Feb 16 '18 at 07:38
  • What are `TWO` `THREE` ... in your code ? Macros set to 2, 3, ... ? – Caninonos Feb 16 '18 at 07:40
  • 1
    Note that in `createGameMapForNumberOfPlayers`, you don't cout `std::endl`. Now, `std::endl` also has the effect of flushing the output. Without it (or without flushing explicitly), it's possible (probable?) that "Creating map" just sits in a buffer somewhere, waiting for more characters to be couted before being printed on screen. If you have something terminating your program abnormally, I guess it might not even get written at all. – Caninonos Feb 16 '18 at 07:44
  • 1
    Possible duplicate of [What is a debugger and how can it help me diagnose problems?](https://stackoverflow.com/questions/25385173/what-is-a-debugger-and-how-can-it-help-me-diagnose-problems) – Raedwald Feb 16 '18 at 07:58
  • @BartekPL sorry for the pictures, I thought it'd be easier for everyone! But now I included the formatted code :) – Fabian Vergara Feb 16 '18 at 08:37
  • @Caninonos I am developing in my MacBook and setting the debugger for Eclipse is a pain in the ass.I already spent like 2hrs trying to make gdb work with no chance, so I'll leave that for another day. And yes, TWO, THREE, etc are enums to their respective integers (2, 3, etc) – Fabian Vergara Feb 16 '18 at 08:38
  • @FabianVergara I didn't suggest to debug (although I should have !). If you name numerical constants, don't name them with what their numerical value is (unless it's some long and complex one like pi, but for 2, what's the point of writing "TWO" instead of 2 ?), but with what they represent (i.e. instead of writing "800" in some window creation function, write WINDOW_WIDTH, now I immediately know what this constant means). Also, you may have better luck with lldb. apple's compiler is actually llvm clang in disguise – Caninonos Feb 16 '18 at 08:46

1 Answers1

2

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.

john
  • 85,011
  • 4
  • 57
  • 81
  • Thank you! This really solved the problem and now I learned for future cases... My program still exists with value -1, but now I am sure it's for other reasons, at other part of the code lol – Fabian Vergara Feb 16 '18 at 08:44