2

Hi I have this structure, but when I call the function joinLottery, the mapping for gamblers[0] is not updated. Need help to understand why is this happening.

struct Gambler {
    uint token;
    uint encRandomNumber;
    bool exist;
    address gamblerAdd;
    address issuer;
}

mapping (int =>  Gambler) public gamblers;
uint public currentNumofGamblers=0;

function joinLottery (uint _token, uint _RandomNumber,
    address _issuer){
    Gambler newGambler = gamblers[int(currentNumofGamblers)]; 
    newGambler.token = _token;
    newGambler.encRandomNumber = _encRandomNumber;
    newGambler.exist = true;
    newGambler.gamblerAdd = msg.sender;
    newGambler.issuer = _issuer;
    currentNumofGamblers++;
    winningNumber = RandomNumber; 
}
1

This line is backwards:

Gambler newGambler = gamblers[int(currentNumofGamblers)]; 

You can't assign something to newGambler from gamblers if you haven't put anything in gamblers yet.

Try something like:

gamblers[int(currentNumofGamblers)] = Gambler(
   _token,
   _encRandomNumber,
   true,
   msg.sender,
   _issuer
); 

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.