operator overloading, and some other basic stuff

i am doing an exercise - a library software, which takes book names, ISBN in form n-n-n-x where n is integer and x is integer or letter and other details.
1.here i am required to use operator overloading with == and != to check whether two ISBN's are same are not. i have done operator overloading before but cant find a way to use them in this code. and same for operators<< to show the book details.
2. also i am required to use constructors which i haven't coz i didnt find a way and any other ways to assign genres to books(genre has to be enumerated type.

i am learning from book Principles & Practice Using C++ stroustrup.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
  /*  exercise - library software, which takes book names,ISBN in form n-n-n-x where n is integer and x is integer or letter and other details.

 */


enum class Genre
{
    fiction=1,nonfiction,periodical,biography,children
};

class Book
{
public:
    void take_ISBN_details(Book& b);
    void show_details();
    void rent();
    vector<Book> ret_rack(){return rack;}
    string ret_ISBN() {return ISBN;}


private:
    string ISBN,author,title,cpydate;
    Genre gen;
    bool checkedout{false};
    vector<Book>rack;
};

bool verify_ISBN(Book& b,string ISBN)
{
    int a{0};
    //check ISBN for format error n-n-n-x
    for(int i=0; i<ISBN.size(); ++i)
    {
        if(ISBN[i]=='-') ++a;
        if(a==0 || a==1 || a==2)
        {
            if(isalpha(ISBN[i]))
            {
                cout<<"\nInvalid ISBN"<<endl;
                return false;
                break;
            }
        }
        else if(a>3)
        {
            cout<<"\nInvalid ISBN"<<endl;
            return false;
            break;
        }
    }
    if(a==0)
    {
        cout<<"\nInvalid ISBN"<<endl;
        return false;
    }


    // check ISBN if duplicate
    for(auto& a:b.ret_rack())
    {
        if(a.ret_ISBN() == ISBN)
        {
            cout<<"\nDuplicate ISBN";
            cout<<"Press any key to continue";
            return false;
            getch();
            system("CLS");
        }
    }
    return true;
}

void Book::take_ISBN_details(Book& b)
{
    char c;
    do
    {

        bool flag{true};
        do
        {

            cout<<"\nEnter ISBN: ";
            getline(cin,ISBN);
            flag=verify_ISBN(b,ISBN);
        }
        while(!flag);

        if(flag)
        {
            string str;
            cout<<"\nISBN valid "<<endl;
            cout<<"Enter author name: ";
            getline(cin,author);
            cout<<"Enter Title: ";
            getline(cin,title);
            cout<<"Enter copyright date: (dd/mm/yy)";
            getline(cin,cpydate);
            cout<<"Enter Genre(fiction,nonfiction,periodical,biography,children): ";
            cin>>str;
            if(str=="fiction") gen=Genre::fiction;
            else if(str=="nonfiction") gen=Genre::nonfiction;
            else if(str=="periodical") gen=Genre::periodical;
            else if(str=="biography") gen=Genre::biography;
            else if(str=="children") gen=Genre::children;
            rack.emplace_back(b);
        }
        cout<<"\nRecord updated. Enter another record?(y/n): ";
        cin>>c;
        cin.ignore();
    }
    while(c=='y');


    cout<<"Press any key to continue";
    getch();
    system("CLS");

}

void Book::show_details()
{

    for(const auto& a:rack)
    {
        cout<<"\n"<<"ISBN: "<<a.ISBN<<"   "<<"Author: "<<a.author<<"     "<<"Title: "<<a.title<<"   "<<"Copyright date: "<<a.cpydate<<endl;
    }
    cout<<"Press any key to continue";
    getch();
    system("CLS");

}

void Book::rent()
{
    string name; bool flag{false};
    cout<<"\nEnter the book name: "; getline(cin,name);
    for(auto& a:rack)
    {
        if(name==a.title && a.checkedout==true)
        {
            cout<<"Book already rented out "<<endl; flag=true;
            break;
        }
        if(name==a.title && a.checkedout==false )
        {
            a.checkedout=true;
            cout<<"\nBook rented"<<endl; flag=true;
            break;
        }

    }
    if(!flag) cout<<"\nBook not found"<<endl;
    cout<<"Press any key to continue"; getch();
    system("CLS");

}

int main()
{
    Book b; int choice;
    while(true)
    {


        cout<<"Welcome to the library. "<<"\n"<<"Enter your choice(1,2,3)"<<"\n1. Enter a new record"<<"\n2. Show all records"<<"\n3. Rent a book"<<endl;
        cin>>choice;
        cin.ignore();
        switch(choice)
        {
        case 1:
            b.take_ISBN_details(b);
            break;

        case 2:
            b.show_details();
            break;

        case 3:
            b.rent();
            break;
        }
    }
}
1
2
3
class Book{
    vector<Book>rack;
};
a Book should not be a collection of books, you are modelling a Library there
so go back to design phase.

¿do you know about the this pointer?
yes i created another class library, where i placed collection of books.
yes i know about this pointer , but right now in book , i haven't reached that chapter.
here is the entire new design i created , its unfinished -> https://pastebin.com/PFya2SEs
problem is in rent(). if i re-rent a book, it doesnot show book already rented out .
a.set_checkedout(b) is not changing the flag in original rack whereas i have passed the book and library objects as reference.
Line numbers are approximate based on your pastebin page.

Line 2: <conio.h> Is an obsolete non-standard header. You shouldn't be using it.

Line 3: You're missing:
1
2
3
4
#include <string>
#include <vector>
#include <iostream>
using namespace std;

Presumably these are in std_lib_facilities.h. You should get in the practice of including the std library headers you need.

Line 38: As ne555 pointed out. A Book is not a collection of books. This line is self-referential and makes no sense.

Line 79: A Library is correctly a collection of Books.

Lines 172, 215, 243, 269: getch is non-standard and should be avoided. Use cin.get() instead.

Line 181: You have a sigened/unsigned mismatch. i is an int. ISBN.size() returns an unsigned int.

if i re-rent a book, it doesnot show book already rented out .

Line 252: When you call l.ret_rack(), you're returning a copy of the library. Any changes you make to the copy of the library are lost when the the copy of the library goes out of scope (for loop exits). l.ret_rack() should return the rack by reference.



Last edited on
@AbstractionAnon thanks . I fixed the points u mentioned except line 181. compiler shows warning which u mentioned. How to fix it!..
Topic archived. No new replies allowed.