0

thank you so much for your help I really appreciated this contract give me error in the line with (length) "ParserError: Expected primary expression. returns candidate.length"

pragma solidity ^0.4.21;

contract Election {

    struct Candidate {
        string name;
        uint voteCount;
    }

    struct Voter {
    bool authorized;
    bool voted;
    uint vote;
    } 
    address public owner;
    string public electionName;

    mapping(address => Voter) public voters;
    Candidate[] public candidates;
    uint public totalVotes;

    modifier ownerOnly() {
        require(msg.sender == owner);
        _;
    }

    function Election(string _name) public {
        owner = msg.sender;
        electionName = _name;
    }

    function addCandidate(string _name) ownerOnly public {
        candidates.push(Candidate(_name,0));  
    }

    function getNumCandidate() public view returns(uint){
        returns candidates.length;
    }  

    function authorize(address _person) ownerOnly public{
        voters[_person].authorized = true;
    }

    function vote(uint _voteIndex) public {
        require(!voters[msg.sender].voted);
        require(voters[msg.sender].authorized);

        voters[msg.sender].vote = _voteIndex;
        voters[msg.sender].voted = true;

        candidates[_voteIndex].voteCount +=1;
        totalVotes +=1;
    }

    function end() ownerOnly public {
        selfdestruct(owner);
    }
}
1

When you define a function's returns values, you have to use returns.

functions getNumCandidates() public view returns(uint)

However when you actually return the values, you have to use return.

{
    return candidates.length;
}

Hence your code should look like:

function getNumCandidate() public view returns(uint){
    return candidates.length;
}
| improve this answer | |

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.