0

Hi I am trying to run this code in remix. It deploys successfully but whenever I add a Customer using the addCustomer function, it shows this message "transact to MyContract.addCustomer pending ...". Afterwards if i try to interact with any other function it just prints for example "call to MyContract.totalRooms" and later the browser window hangs. May I know what's the issue.

pragma solidity ^0.6.0;

contract MyContract {

    enum Statuses {Vacant,Occupied}
    uint internal counter = 0;
    event occupied (string cname, address raddr);
    uint public constant totalRooms = 5;
    struct Room{
        address roomAddress;
        Statuses roomStatus;
    }

    mapping(uint => Room) public rooms;

    function roomInfo() public {
        rooms[1] = Room(0x33c5E8069F3F5b653fEDdE8c628B1e0DE48382a1,Statuses.Vacant);
        rooms[2] = Room(0x88789bB3d6B1eF931d3DF5aB008A38F86EA3da8d,Statuses.Vacant);
        rooms[3] = Room(0xDaEf09dE138de7C27f1a43cA6c544c1a6A50BAf9,Statuses.Vacant);
        rooms[4] = Room(0xC72C577e4450E4AAF9eaf2FF3ff6b3eC597F894b,Statuses.Vacant);
        rooms[5] = Room(0xF68af2bAC4c9Db4a27186C6382eAD0c0BA04638F,Statuses.Vacant);
    }


    // Mappings
    mapping(uint => Customer) public customers;


    struct Customer {
        address _address;
        string name;
        address room;
    }


    constructor() public {

    }


    function addCustomer(address _add, string memory _name) public {
        address _room;
        while(counter<=totalRooms){
            for(uint i =0; i< totalRooms;i++){
                if(rooms[i].roomStatus==Statuses.Vacant)
                  _room = rooms[i].roomAddress;
                else 
                i++;
            }
            customers[counter+1] = Customer(_add, _name,_room);
        }

        emit occupied(_name,_room);
    }


}
1

The addCustomer function has an infinite loop

Inside the while loop counter is never modified

    while(counter<=totalRooms){
        for(uint i =0; i< totalRooms;i++){
            if(rooms[i].roomStatus==Statuses.Vacant)
              _room = rooms[i].roomAddress;
            else 
            i++;
        }
        customers[counter+1] = Customer(_add, _name,_room);
    }

It appears that an if should be used there

    if (counter < totalRooms) {
        for (uint i = 0; i< totalRooms; i++){
            if (rooms[i].roomStatus == Statuses.Vacant) {
                _room = rooms[i].roomAddress;
                break;
            }
        }
        counter += 1;
        customers[counter] = Customer(_add, _name,_room);
    }
| 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.