0
//SPDX-Licence-Identifier: GNU AGPL
pragma solidity >= 0.5.0 < 0.9.0;

contract Election{
    struct Candidate{
        string name;
        uint numVotes;
}
    struct Voter{
        string name;
        bool authorised;
        uint whom;
        bool voted;

    }

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

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

    function startElection(string memory _electionName) public{
        owner = msg.sender;
        electionName = _electionName;
    }

    function addCandidate(string memory _candidateName) ownerOnly public{
        candidates.push(Candidate(_candidateName, 0));

    }

    function authorizeVoter(address _voterAdress) ownerOnly public{
        voters[_voterAdress] authorised = true;
    }

    function getNumCandidates() public view return(uint){
        return candidates.length;
    }

    function vote(uint candidateIndex) public{
        require(!voters[msg.sender].voted);
        require(voters[msg.sender].authorised);
        voters[msg.sender].whom = candidateIndex;
    }
 
}//SPDX-Licence-Identifier: GNU AGPL
pragma solidity >= 0.5.0 < 0.9.0;

contract Election{
    struct Candidate{
        string name;
        uint numVotes;
}
    struct Voter{
        string name;
        bool authorised;
        uint whom;
        bool voted;

    }

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

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

    function startElection(string memory _electionName) public{
        owner = msg.sender;
        electionName = _electionName;
    }

    function addCandidate(string memory _candidateName) ownerOnly public{
        candidates.push(Candidate(_candidateName, 0));

    }

    function authorizeVoter(address _voterAdress) ownerOnly public{
        voters[_voterAdress] authorised = true;
    }

    function getNumCandidates() public view return(uint){
        return candidates.length;
    }

    function vote(uint candidateIndex) public{
        require(!voters[msg.sender].voted);
        require(voters[msg.sender].authorised);
        voters[msg.sender].whom = candidateIndex;
    }
 
}//SPDX-Licence-Identifier: GNU AGPL
pragma solidity >= 0.5.0 < 0.9.0;

contract Election{
    struct Candidate{
        string name;
        uint numVotes;
}
    struct Voter{
        string name;
        bool authorised;
        uint whom;
        bool voted;

    }

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

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

    function startElection(string memory _electionName) public{
        owner = msg.sender;
        electionName = _electionName;
    }

    function addCandidate(string memory _candidateName) ownerOnly public{
        candidates.push(Candidate(_candidateName, 0));

    }

    function authorizeVoter(address _voterAdress) ownerOnly public{
        voters[_voterAdress] authorised = true;
    }

    function getNumCandidates() public view return(uint){
        return candidates.length;
    }

    function vote(uint candidateIndex) public{
        require(!voters[msg.sender].voted);
        require(voters[msg.sender].authorised);
        voters[msg.sender].whom = candidateIndex;
    }
 
}

error text :Election.sol:43:45: ParserError: Expected '{' but got 'return' function getNumCandidates() public view return(uint){ ^----^

2 Answers 2

1

Simple syntax error. You are using the wrong type of brackets to define a struct

Instead of what you currently have :

struct Candidate (
  string name;
  uint numVoters;
)

Change it to this :

struct Candidate {
  string name;
  uint numVoters;
}
6
  • now the error has changed to : Election.sol:8:2: ParserError: Expected pragma, import directive or contract/interface/library definition. }; ^ Dec 25, 2022 at 8:49
  • can you paste in all the code that you have currently?
    – Anon.eth
    Dec 25, 2022 at 8:49
  • I have changed the question please check Dec 25, 2022 at 8:55
  • Please format the code better. It’s hard to read it like this
    – Anon.eth
    Dec 25, 2022 at 8:57
  • Delete } after Candidate {
    – Anon.eth
    Dec 25, 2022 at 9:01
1

Your function is using wrong word for return keywords in the function.

Instead of looking like this.

function getNumCandidates() public view return(uint){
        return candidates.length;
    }

It should be written like this.

function getNumCandidates() public view returns(uint){
    return candidates.length;
}
1
  • when i change return to returns the error is moved higher Dec 25, 2022 at 9:34

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.