0
pragma solidity >=0.4.0 <0.7.0;

contract NewHello{

    mapping(address => string) public users; // contains a name for every address

    function updateUser(string memory name) public {
        if(string name != users[name]){
            users[msg.sender] = name; // set name for address
        }

    }
    function getUser(address userAddress) public view returns(string memory) {
        return users[userAddress]; // return address name
    }
}

well I have 2 func fist one set the name second one get the name.

my goal is : I wanna check the users . if user isn't registered , register this user than return the user name use the getUser function.

when I compile my contract I give a error like this: Expected ')' but got identifier if(string name != users[name]){ ^--^

my getUser func. works very well. I can get results which I want but I can't fixed the userUpdate func.

what should I do ? anyone can help me please :)

1

1 Answer 1

1

About error handling in Solidity you can read here.

Use require instead of if: The require function should be used to ensure valid conditions that cannot be detected until execution time. These conditions include inputs, or contract state variables are met, or to validate return values from calls to external contracts.

You don't need to pass the user's address as a parameter if you use msg.sender, which is the sender of the message.

About string comparison you can get the hash of the two strings and compare them.

With your actual implementation, users can change their name:

pragma solidity >=0.4.0 <0.7.0;

contract NewHello {

    mapping(address => string) public users; // contains a name for every address

    function updateUser(string memory name) public {
        require(keccak256(abi.encodePacked(name)) != keccak256(abi.encodePacked(users[msg.sender])));
        users[msg.sender] = name; // set name for address
    }

    function getUser(address userAddress) public view returns(string memory) {
        return users[userAddress]; // return address name
    }
}

Alternative solution: If you want only to check if the user is registered, you need to check if there is a name associated with the user's address. You can do that converting the string into a type bytes and then checking if the length is 0.

pragma solidity >=0.4.0 <0.7.0;

contract NewHello {

    mapping(address => string) public users; // contains a name for every address

    function updateUser(string memory name) public {
        require(bytes(users[msg.sender]).length == 0);
        users[msg.sender] = name; // set name for address
    }

    function getUser(address userAddress) public view returns(string memory) {
        return users[userAddress]; // return address name
    }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.