-5

I have one structure like below in Arduino, I want to update it

struct record
{
   int bookId;
   int qtyInStock;

};

typedef struct record Record;

Record aRec;
aRec.bookId = 100;
aRec.qtyInStock = 12;

aRec.bookId = 101;
aRec.qtyInStock = 10;

aRec.bookId = 102;
aRec.qtyInStock = 100;

If bookId 101 is sold then how can I update qtyInStock? So, qtyInStock for bookId 101 should be 9 now.

Thanks

ckruczek
  • 2,361
  • 2
  • 20
  • 23
newbeedeveloper
  • 759
  • 2
  • 6
  • 10
  • The code makes no sense. It is modifying value of variable aRec in a row with constants. You perhaps want to have some container of these Records, not single variable. – Öö Tiib Nov 14 '17 at 07:27
  • what do you mean by container? Can you please advise more so I can try that? – newbeedeveloper Nov 14 '17 at 07:38
  • 1
    Consider skimming through a good book on C++ first. Check out an excellent list of books assembled by the community of StackOverfllow C++ programmers here: [The Definitive C++ Book Guide and List](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) – Ivan Aksamentov - Drop Nov 14 '17 at 07:42

2 Answers2

0

I would keep all records in a linked list, and iterate over them, find the book id, decrease it's available count.

You know that you are editing a single record (aRec) in your example code right? You will need some sort of container for that:

struct node {
   Record* value;
   Node* next;
}

sruct recordList {
    Node* head;
}
/* ... */
sorush-r
  • 10,490
  • 17
  • 89
  • 173
  • Would a for loop good approach if I loop through all records and get aRec.bookId with 101, once I have correct record then I can update it in for loop? – newbeedeveloper Nov 14 '17 at 07:37
  • @newbeedeveloper well, I'm afraid you have no other choice (: Having something more sophisticated like `std::map` (a red-black balanced tree) or a hash table, will be too expensive for an AVR. So just keep it simple: Chain all your records together, keep the head in chain, iterate over the chain checking ids. – sorush-r Nov 14 '17 at 07:40
0

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);
  }
}
Trishant Pahwa
  • 2,559
  • 2
  • 14
  • 31