0

This question already has an answer here:

I have a problem with my code.

contract Registration {
address public owner;
uint conceptPrice;
uint conceptValue;

function Registration(uint _conceptPrice) public {
    owner = msg.sender;
    conceptPrice = _conceptPrice;
    conceptValue = conceptPrice;
}
/////////////////////////////////////
/* Others code*/
/////////////////////////////////////
struct Request {
    uint reqId;
    uint amount;
    uint percentage;
    address modAdd;
    //0 for pending, 1 accept, 2 decline
    uint state;
}
//
Request[] public requests;
//mapping (uint => Request) requests;
function request(uint _amount, uint _percentage) public returns (uint numRequests) {
    bool present = false;
    for (uint i = 0; i <= participants.length; i++) {
        if (participants[i] == msg.sender) {
            present = true;
            break;
        }
    }
    //
    if (msg.sender == owner || _amount < conceptPrice || _amount < balances[msg.sender] || _percentage > 5 || present == false) return;
    Request memory req = Request({reqId : requests.length + 1, amount : _amount, percentage : _percentage, modAdd : msg.sender, state : 0});
    //bissogna togliere i soldi a chi fa l'offerta
    requests.push(req);
    numRequests = requests.length;
}

uint[] ident;
uint[] per;
uint[] amountV;

function pendingRequest() public {

    for (uint i = 0; i == requests.length; i++) {
        if (requests[i].state == 0) {
            ident.push(requests[i].reqId);
            per.push(requests[i].percentage);
            amountV.push(requests[i].amount);
        }
    }
}
function printReq() public constant returns (uint[] ident_){
    ident_ = ident;
}
}

I created two different request (numRequests =2) but when i try to take value from array of object Request through the function pendingRequest i have an error: status 0x0 Transaction mined but execution failed. Array requests have values inside for example requests at position one is:requests[1] Why?

marked as duplicate by Ismael, Achala Dissanayake, Richard Horrocks, shane, Rosco Kalis Mar 11 at 14:23

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

0

There is an error with your for loop:

for (uint i = 0; i == requests.length; i++) { ...  <-- this code is wrong

You probably are trying to iterate over the values of requests, so your code should look like:

function pendingRequest() public {
    for (uint i = 0; i < requests.length; i++) {
        ... do stuff
    }
}
  • No, i have same problem with "==" – Davide Dec 9 '18 at 20:30
  • == is not correct. You need to check if i is less than requests.length (<). That first code block was me just copying the incorrect part of your code. – Shawn Tabrizi Dec 9 '18 at 20:31
  • There are numerous other errors with your code though (as it is written in your post), it will certainly not work even if you fix this one issue. I think you need to take a step back and iterate on smaller pieces, getting each one to work on its own. – Shawn Tabrizi Dec 9 '18 at 20:34
  • What do you mean? If variable requests is public i can know output and it has value inside. For example: link – Davide Dec 9 '18 at 21:01
  • I am sorry, I am not sure what you are asking me. The output you are getting looks right to me for an index of requests – Shawn Tabrizi Dec 9 '18 at 22:00

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