0

I have this code for a voting system but when I compile it give me error in here ( boolean if_voted; ) can any one help me please. thank you

this is the code :

pragma solidity >=0.4.22 <0.7.0;

contract Ballot {
    struct Voter {
        uint weight;
        boolean if_voted;
        address delegated_to;
        uint vote;
    }
    struct Proposal {
        bytes32 name;
        uint voteCount;
    }

    address public chairperson;
    mapping(address => Voter) public voters;
    Proposal[] public proposals;
    constructor(bytes32[] memory proposalNames) public {
        chairperson = msg.sender;
        voters[chairperson].weight = 1;

        for (uint i = 0; i < proposalNames.length; i++) {
            proposals.push(Proposal({
                name: proposalNames[i],
                voteCount: 0
            }));
        }
    }
    function giveRightToVote(address voter) public {
        require(
            msg.sender == chairperson,
            "Only the chairperson can assign voting rights."
        );
        require(
            !voters[voter].voted,
            "The voter has used their ballot."
        );
        require(voters[voter].weight == 0);
        voters[voter].weight = 1;
    }
    function delegate(address to) public {
        Voter storage sender = voters[msg.sender];
        require(!sender.voted, "You have already voted.");

        require(to != msg.sender, "You can’t delegate to yourself.");

        while (voters[to].delegate != address(0)) {
            to = voters[to].delegate;
            require(to != msg.sender, "Found loop in delegation!");
        }
        sender.voted = true;
        sender.delegate = to;
        Voter storage delegate_ = voters[to];
        if (delegate_.voted) {
            proposals[delegate_.vote].voteCount += sender.weight;
        } else {
            delegate_.weight += sender.weight;
        }
    }
    function vote(uint proposal) public {
        Voter storage sender = voters[msg.sender];
        require(sender.weight != 0, "Cannot vote");
        require(!sender.voted, "Has voted.");
        sender.voted = true;
        sender.vote = proposal;
        proposals[proposal].voteCount += sender.weight;
    }
    function winningProposal() public view
            returns (uint winningProposal_)
    {
        uint winningVoteCount = 0;
        for (uint p = 0; p < proposals.length; p++) {
            if (proposals[p].voteCount > winningVoteCount) {
                winningVoteCount = proposals[p].voteCount;
                winningProposal_ = p;
            }
        }
    }

    function winnerName() public view
            returns (bytes32 winnerName_)
    {
        winnerName_ = proposals[winningProposal()].name;
    }
}
0

Change this:

boolean if_voted;
address delegated_to;

To this:

bool voted;
address delegate;
| improve this answer | |
  • Thank you so much – Atheer S H Mar 21 at 10:31
  • @AtheerSH: You're welcome. Feel free to accept the answer by clicking on the V next to it (if it has answered your question of course). – goodvibration Mar 21 at 12:32
0
        bool if_voted;

try using 'bool' instead of 'boolean

you should declare your struct 'Voter' as shown below,

    struct Voter 
{ 
    uint weight;
    bool voted;
    address delegate;
    uint vote;
}
| improve this answer | |
  • thank you so much – Atheer S H Mar 20 at 22:13
  • I fix it as you said then it gave me this error : member "voted" not found or not visible – Atheer S H Mar 20 at 22:14
  • your variable 'voted' is not declared inside your struct, rename it as 'voted'. – Adam Boudjemaa Mar 21 at 0:23

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.