0
struct userdata
{
    string task;
    bool isdone;
}

userdata[] public userinfo; 

    address public user;

   // mapping (uint[]=>string) public tasksno;
    mapping (address=>userdata[]) public usertask;

    constructor (address _user)
    {
        user=_user;

    }

    function createtask(address _user1, string memory _task)public
    {
        if(user!=_user1)
        {
            user=_user1;
        }
        userinfo.task.push(_task);
        userinfo.isdone.push(false);

        usertask[user]=userinfo;

    }

2 Answers 2

0

I'm not sure what are you trying to do, in any case this version at least should compile:

    function createtask(address _user1, string memory _task) public {
        if (user != _user1) {
            user=_user1;
        }

        userdata memory _data = userdata({
            task: _task,
            isdone: false
        });
        userinfo.push(_data);

        usertask[user] = _data;
    }

You can't do something like userinfo.task.push(_task) since userinfo is an array, not userinfo.task. So first you create an element (_data), which is a struct, and you push that.

Some examples to learn: https://solidity-by-example.org/structs/

0
0

Here is the correct code:

I wasn't properly assigning values to the struct elements.

    struct userdata
    {
      string task;
      bool isdone;
    }

    userdata[] userinfo; 

    address user;

    mapping (address=>userdata[]) usertask;

    function createtask(address _user1, string memory _task)public
    {
        if(user!=_user1)
        {
            user=_user1;
        }
        userinfo.push(userdata(_task,false));
        usertask[user]=userinfo;
    }

    function readtask(address _user1, uint index) public view returns (userdata memory)
    {
        userdata storage uses=usertask[_user1][index];
        return uses;
    }

    function updatetask(address _user1, uint index) public
    {
        usertask[_user1][index].isdone=true;
    }

    function deletetask(address _user1, uint index) public 
    {
        delete usertask[_user1][index].isdone;
    }

    function taskcount() public view returns (uint)
    {
       return usertask[msg.sender].length;
    }

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.