0
pragma solidity ^0.4.11;
contract simpleVotingDapp {
/*
The key of the mapping is candidate name stored as type bytes32 and value is an unsigned integer to store the vote count
*/
mapping (bytes32 => uint8) public votesReceived;
/*
We use an array of bytes32 instead to store the list of candidates
*/
bytes32[] public candidateList;
/* This is the constructor which will be called once when we deploy the contract to the blockchain.
When we deploy the contract, we will pass an array of candidates who will be contesting in the election
e.g.["Tom","Dick","Harry"]
*/

function simpleVotingDapp(bytes32[] candidateNames) {
    candidateList = candidateNames;
}

// This function returns the total votes a candidate has received so far
function totalVotesFor(bytes32 candidate) returns (uint8) {
    if (validCandidate(candidate) == false) revert();
    return votesReceived[candidate];
}

// This function increments the vote count for the specified candidate. This
// is equivalent to casting a vote
function voteForCandidate(bytes32 candidate) {
    if (validCandidate(candidate) == false) revert();
    votesReceived[candidate] += 1;
}

function validCandidate(bytes32 candidate) returns (bool) {
    for(uint i = 0; i < candidateList.length; i++ ) {
        if (candidateList[i] == candidate) {
            return true;
        }
    }
    return false;
}

}

I copied this code on a book by by Mukhopadyay, M. “Ethereum smart contracts development”;
function simplevotingDapp says Warning: Defining constructors as functions with the same name as the contract is deprecated. Use "constructor(...) { ... }" instead. function simpleVotingDapp(bytes32[] candidateNames) { ^ (Relevant source part starts here and spans across multiple lines).
Every function says Warning: No visibility specified. Defaulting to "public". function validCandidate(bytes32 candidate) returns (bool) { ^ (Relevant source part starts here and spans across multiple lines).
Lastly function validCandidate says Warning: Function state mutability can be restricted to view function validCandidate(bytes32 candidate) returns (bool) { ^ (Relevant source part starts here and spans across multiple lines). What is wrong with it?

  • Because the Solidity language has changed since the book was written (even on v0.4.x, which your coding example is designated for). I suggest that you get yourself a more recently-published book. – goodvibration Aug 9 at 12:33
  • @goodvibration thanks sir. i thought i had some problems in encoding kekeke – dummy dummy Aug 9 at 12:38
  • @goodvibration I managed to update some of the codes. Now the return statement says TypeError: Different number of arguments in return statement than in returns declaration. i changed to code to – dummy dummy Aug 10 at 14:55
  • function totalVotesFor(bytes32 candidate) public { require (validCandidate(candidate) == false); return votesReceived[candidate]; – dummy dummy Aug 10 at 14:56
  • Return statement says TypeError: Different number of arguments in return statement than in returns declaration. While require statement says TypeError: Operator == not compatible with types tuple() and bool can i u tell me what im doing wrong? – dummy dummy Aug 10 at 14:57

Your Answer

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

Browse other questions tagged or ask your own question.