0

I was wondering if anyone could help. I'm trying to check to see if an address has been stored in a struct, but the results I'm getting are not what I'm expecting.

So basically I have two functions....one is to store a value, and the other is to check if an address has already used the first function and stored a value.

so if I call storeValue() and then I input a value, the value gets stored correctly, but if I then call checkForAddress() and input my address, it still shows "false". I'm expecting it to show true because it was the same address I called storeValue() with to begin with.

I need to be able to confirm if addresses have already stored values....ultimately it will mean that if they have stored a vlue, then they won't be able to call storeValue() any more....they only get one chance to store a value from a single address.

Can anyone help, as I'm not really sure what I've done wrong :-(

thanks

pragma solidity 0.4.24;

import "github.com/OpenZeppelin/openzeppelin-solidity/contracts/math/SafeMath.sol";



// MAIN CONTRACT

contract DoesAddressExist {
        using SafeMath for uint256;


constructor() public {
}


function () private payable {}



struct ValueRecorded {
    uint valueEntered;
    uint8 flag;
}

ValueRecorded[] public valueRecorded;

mapping(address => ValueRecorded) user;




function storeValue(uint _value) public {
        ValueRecorded memory newValueRecorded = ValueRecorded ({
                valueEntered: _value,
                flag: 1
                });
        valueRecorded.push(newValueRecorded);

}


function checkForAddress(address _address) public view returns (bool) {
    if(user[_address].flag == 1) {
    return true; }
    else return false;
    }


}
  • your contract might work perfectly but you may have problems in the way you are calling your contract, specify that in an edit. – Kaki Master Of Time Jun 27 '18 at 11:41
  • I appreciate the response but not sure what you mean by the way that I'm calling it? I'm deploying the contract, then calling storeValue() and inputting a number (e.g.10) as my value.....then calling checkForAddress() and the response still shows false, even though if I were to call valueRecorded() it shows my address and my flag = 1 – user9498339 Jun 27 '18 at 11:57
  • what I meant is that to store value you need to send a transaction. to read value you need to call the EMV. if you are doing that right it is probably a problem with your contract code. – Kaki Master Of Time Jun 27 '18 at 12:38
0

The problem with your current code is that you're never actually storing something in your user mapping, and you're not actually checking it in storeValue.

Also, you don't need to keep track of a whole VoteRecorded. To accomplish your goal, a bool is sufficient:

mapping(address => bool) stored;

function storeValue(uint _value) public {
    require(!stored[msg.sender], "Sender already stored a value.");
    stored[msg.sender] = true;

    // ...
}

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.