tags:

views:

139

answers:

3

Hello everyone,

Here's a block of code I'm having trouble with.

string Game::tradeRandomPieces(Player & player)
{
  string hand = player.getHand();
  string piecesRemoved;
  size_t index;
  //Program crashes while calculating numberOfPiecesToTrade...
  size_t numberOfPiecesToTrade = rand() % hand.size() + 1
  for (; numberOfPiecesToTrade != 0; --numberOfPiecesToTrade)
  {
    index = rand() % hand.size();
    piecesRemoved += hand[index];
    hand.erase(index,1);
  }

  player.removePiecesFromHand(piecesRemoved);
  player.fillHand(_deck);

  return piecesRemoved;
}

I believe the code is pretty self explanatory.

fillhand and removepiecesfromhand are working fine, so that's not it. I really can't get what's wrong with this :(

Thanks for your time

EDIT OK, I found out where the program crashes. Added a comment to the above source code.

+3  A: 

If the hand is empty, then this operation:

rand() % hand.size() 

In the initializer of the for loop, will be attempting to perform a modulus by 0, which is essentially division by zero. That is your crash.

Add a test to make sure the hand is not empty before proceeding with the rest of the method.

Tyler McHenry
This would not cause 'This program stopped responding'
BlueRaja - Danny Pflughoeft
It causes a floating point exception, and is definitely a crash bug with this code. I have no idea what message Windows puts up when an FPE is encountered. There may be a second error, but I don't see it. Edit: Or, if for some reason the div-by-zero resulted in a negative number, instead of a crash, it would make the loop infinite. But I don't know why a system would handle an FPE like that.
Tyler McHenry
Yes, I considered the div by 0 case, and I already had a .empty() test. But thanks anyway.
Francisco P.
A: 

Maybe you want to use

 for (size_t numberOfPiecesToTrade = rand() % hand.size() + 1; numberOfPiecesToTrade > 0; --numberOfPiecesToTrade)

to make things clearer.

Edit: If you run in debug mode, than wo don't you just debug? :) This "not responding" message is afaik often caused by an infinite loop.

Edit2: I don't know if i am correct, but could it be that in some cases you will miss the numberOfPiecesToTrade != 0 condition if the initialvalue of numberOfPiecesToTrade is 1? I'm not familiar with size_t though.

InsertNickHere
your edit: Because I don't know how. :( I know, I should learn..your edit no. 2: No, that's not it.
Francisco P.
A: 

stick a breakpoint into your for loop to give you a better idea of what's going on. I would bet that the for loop is going infinite and causing the program to hang.

On hitting the breakpoint, check your iterator variables and see if you can see anything out of the ordinary

Lerxst
Thanks for the check, glad that I could help. Could you share what the problem was, so that others may benefit from your discovery?
Lerxst
It was quite odd, actually. I started debugging, first using printf then the actual "debug" function. It only stopped crashing after I took the numberOfPiecesToTrade out of the for loop declaration, as shown in my question.
Francisco P.