0

I am trying to make a voting system and I have come up with something but at the moment the vote count is not increasing. Can you check what is wrong? I am beginner and my code is below.

Contract Code

struct Candidate {
    uint64 votes;
    string name;
    mapping(address => uint) voteMapping;
    bool flag;
    address add;
  }
  struct Team {
    Candidate[] candidates;
    mapping(uint => Candidate) candidateMapping;
    mapping(address => bool) addressMap;
    string name;
    uint[] cidArr;
    uint token;
    address creator;
    bool flag;
  }
//More code in between
function vote(uint tCid, uint cid) external {
    Team storage t = getTeam(tCid);
    if (t.flag) {
      Candidate storage c = t.candidateMapping[cid];
      if (
        c.flag && c.voteMapping[msg.sender] == 0 && t.addressMap[msg.sender]
      ) {
        c.voteMapping[msg.sender] = 1;
        c.votes++;
        emit VoteDone(true);
      }
    }
    emit VoteDone(false);
  }
  function getCandidateVotes(
    uint tCid,
    uint cid
  ) external view returns (uint256) {
    Team storage t = getTeam(tCid);
    Candidate storage c = t.candidateMapping[cid];
    return c.votes;
  }

Whenever I call the getCandidateVotes method, I have always received 1 even after voting multiple times. Can you help me fix this?

2

1 Answer 1

0

this error is arising from this line of code

c.flag && c.voteMapping[msg.sender] == 0

in the conditional statement here

if (t.flag) {
  Candidate storage c = t.candidateMapping[cid];
  if (
    c.flag && c.voteMapping[msg.sender] == 0 && t.addressMap[msg.sender]
  ) {
    c.voteMapping[msg.sender] = 1;
    c.votes++;
    emit VoteDone(true);
  }
}

...once you have voted once, c.voteMapping[msg.sender] != 0, and subsequently c.votes++; isn't executed

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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