In the LMS Project, after inserting an entry into the Book class, if I am updating the count by moving the offset to the beginning, reading the current count, and then overwriting the updated value of the count, the count value is not displaying properly as 0, 1, or 2. Instead, it shows some garbage values as shown below:
Count :32512 Count :32513
Furthermore, when incorporating this count in the project, my displayBooks() function does not show any details, it only prints begin and end line of the function.
This problem is raised only when trying to incorporate the count of Books in the project.
What might be the possible reasons? Any suggestions are welcome.
The solution to the above problem is found as follows:
1. First, open the file in append mode to insert fresh details. After that, close its instance.
2. Then, open the same file again, but this time, NOT in append mode. Instead, use ios::out | ios::in mode. Move the offset to the beginning, read the count, and update it.
3. Once again, move the offset to the beginning and overwrite the count.
The above issue with the compilation error is resolved by avoiding an additional local instance of the state variable. There should be only one instance of the class Book, and we should directly access and modify the existing state variable within the checkOutBook() function.