I am writing below a simple contract which stores all results of questionnaires for each ID.
contract answer{
mapping(address => mapping(string => bool)) voters;
struct qList {
uint count; //The number of respondents
mapping(address => mapping(uint => uint)) answer;
}
mapping(string => qList) questionnaires;
function vote(string ID, uint qNum, uint ans) returns (bool) {
if(voters[msg.sender][ID]) throw;
voters[msg.sender][ID] = true;
questionnaires[ID].count += 1;
questionnaires[ID].answer[msg.sender][qNum] = ans;
return true;
}
function getNumResult(string ID) constant returns (uint res) {
return questionnaires[ID].count;
}
}
The function vote
can be called and mined successfully, however I cannot get the return value of vote
.
Appreciate it if someone would advise the cause of this and solution to get the return value of function with arguments.
Best Answer
Summary
Your code runs as expected when I execute it through the
geth
console. If it does not work for you, try increase the gas you send with your transactions.As
@Taylor Gerring
has stated in his answer, you may not be able to get the results from yourvote()
function, but your code seems to work OK.If you want the result from your
vote()
function, which in your example is a check as to whether a person has voted before, you already have this data in yourvoters
data.The Details
I've taken your source code and just changed the class name from
answer
toAnswer
, and converted your comment from//
to/*...*/
:I've stripped out the CR-LF from the code and collapsed the spaces, and executed the following statement in
geth
:I then compiled your code and inserted it into my dev blockchain:
I waited for the contract to be mined and got the following message:
Checked
getNumResult()
:Voted:
Waited for the transaction to be mined and checked the transaction:
Checked
getNumResult()
:Sent another vote with a different ID:
Checked the results:
Sent another vote from the same account with the same ID:
And I noticed the following error:
The error above must have been generated from the
throw
statement in your code if the same ID is voted from the same account:I then sent another vote from my second account:
And the results were updated as expected: