Home > front end >  Updating value of class variables - C
Updating value of class variables - C

Time:04-15

I'm just beginning to learn how to code and I've come across a problem I can't seem to solve. More specifically the problem occurs in the "borrows" function.

In the following program I am somehow unable to update the value of the public class variable "stock" even though I used getters and setters. It seems to be updated correctly on the cout right after but not "saved permanently". My guess is that it's modifying a copy of the variable rather than the variable itself.

I have attached my code to the post! Please let me know if I should upload the whole file!

Thanks in advance!

void Book::borrows() {
    int searchid;
    bool isfound=false;
    cout<<"Please enter the unique ID of the book:\t\t";
    cin>>searchid;
    for(auto i:myBooks){
        if (i.id==searchid){
            cout<<"This book matches your search:\t"; print(i);
            if (i.stock==0) {
                cout<<"Book is out of stock!"<<endl;
            } else {
                setStock((i.stock-1));
                cout<<"Successfully borrowed!! Now there are only "<<getStock()<<" copies left in stock!"<<endl;
            }
            isfound=true;
        }
    }
    if (isfound== false){
        cout<<"    \t\tBook not found    \t\t"<<endl;
    }
    system("pause");
}

int Book::getStock() const {
    return stock;
}

void Book::setStock(int newstock) {
    Book::stock = newstock;
}

Edit 1:

Here is my Class structure and my vector:

class Book{
public:
    int id;
    string title;
    int stock;

    void add();
    void displayall();
    void displayspecific();
    void print(Book);
    void borrows();
    void returns();

    int getStock() const;

    void setStock(int stock);
};

vector<Book> myBooks;

CodePudding user response:

Your actual problem is that you are operating on a copy of the Book object, not the setters and getters of the members of a book.

for(auto i:myBooks){

You need

for(auto &i:myBooks){

But as other have pointed out, you need 2 classes, Library and Book.

CodePudding user response:

I agree with Thomas Matthews' comment. Your current Book structure does not make sense because most of the methods are for doing work with a collection of Books rather than a particular Book

If you were to refer to a collection of books as a Library then the code below would make more sense for delegating where work is done among Book and Library

#include <string>
#include <vector>

using namespace std;

class Book {
public:
    int id;
    string title;
    int stock;

    // These methods operate on this particular Book instance
    int getStock() const;
    void setStock(int);
};

int Book::getStock() const {
    return stock;
}

void Book::setStock(int newstock) {
    stock = newstock;
}

class Library {
public:
    vector<Book> myBooks;

    // These methods operate on the vector of books
    void add();
    void displayall();
    void displayspecific();
    //void print(Book); // This method probably belongs inside Book
    void borrows();
    void returns();
};

int main() {
    return 0;
}

Rewriting borrows in terms of a Library method would mean operating on that Library's myBooks vector. When you find the particular Book you want inside the vector then you can call its getters and setters which should work fine

Addressing your question, in your current code this is iterating over your Books and giving you a copy of each one:

for(auto i:myBooks)

You want this to get a reference instead of a copy:

for(auto& i:myBooks)
  • Related