0
pragma solidity ^0.4.24;

contract online_shopping{

    struct item {
        uint price;
        string name;
        //string seller;

    }
    struct soldItems{
        address customer;
        uint tprice;
    }

    mapping (address=>uint) sold;
    uint public number;
    uint public totalPrice;
    item  [] public items;
    //soldItems [] public si;
    uint public len;
    address public owner;
    constructor () public{
        owner=msg.sender;

    }
    modifier  onlyOwner() {
        require(msg.sender==owner);
        _;

    }

    function  addItems(uint _price,string _name) public onlyOwner  {

        items.push(item({
            price : _price,
            name : _name
           // seller : _seller
        }));

    }
    function () payable public{}

    function  sellItems (uint _number,string _name)public payable returns(bool)
    {

        number=_number;
        bool f;
        uint p;

        len=items.length;
        for(uint i=0;i<items.length;i++)
        {
            if (keccak256(abi.encodePacked(items[i].name))==keccak256(abi.encodePacked(_name)))
            { f= true;p=i;}
            else
            {f=false;}
        }
        if(f==true)
        {
          totalPrice= items[p].price*number; 
        }

        if(msg.value>totalPrice)
        {
            address(this).transfer(totalPrice-msg.value);
            return true;
            sold[msg.sender]=totalPrice;
        }
        else
        {
            return false;
        }
    }  

}

I tried to read the length separately, but it is showing zero length.

| improve this question | | | | |
  • 1
    items.length should tell you the length of the items array, but that's what your code is already doing. What specific problem are you having? – user19510 Aug 20 '19 at 22:33
  • I am not able to access the name feature from the item structure at the specific index. – Akp101 Aug 21 '19 at 11:28
  • I'm not sure what you mean. Are you seeing an error of some sort? Or is the result of running your code not what you expected? – user19510 Aug 21 '19 at 17:11
  • Yeah.. Actually when I run my code to display the total amount. It's showing zero. So I thought maybe the string equality is not working properly – Akp101 Aug 22 '19 at 3:50
  • I have no idea what "the total amount" means or why it being zero suggests a problem. I'm going to vote to close this question. – user19510 Aug 22 '19 at 3:55
2

You can only get the length of arrays.

It is not possible to get the length of structs.

| improve this answer | | | | |

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.