[Solved] Update a struct in Arduino


You can use an array of type record to store multiple books. Sold being your inbuilt function, you can try this:

struct record
{
   int bookId;
   int qtyInStock;

};
typedef struct record Record;
void sold(int id, Record* records) {
  int i;
  for(i=0;i<3;i++) {
    if(records[i].bookId == id) {
      records[i].qtyInStock--;
    }
  }
}
void updateId(int id, int new_id, Record* records) {
  int i;
  for(i=0;i<3;i++) {
    if(records[i].bookId == id) {
        records[i].bookid = new_id;
    }
  }
}
void updateQty(int id, int new_qty, Record* records) {
  int i;
  for(i=0;i<3;i++) {
    if(records[i].bookId == id) {
        records[i].qtyInStock = new_qty;
    }
  }
}
void main() {
  Record records[3];
  records[0].bookId = 100;
  records[0].qtyInStock = 12;
  records[1].bookId = 101;
  records[1].qtyInStock = 10;
  records[2].bookId = 102;
  records[2].qtyInStock = 100;
  int i;
  sold(101, records);
  updateId(100, 99, records);
  updateQty(102, 15, records);
  for(i=0;i<3;i++) {
    printf("%d\n", records[i].bookId);
    printf("%d\n\n", records[i].qtyInStock);
  }
}

1

solved Update a struct in Arduino