I can't figure out the error in my class program for a bookshop

So the program is a Bookshop
which is the Class Book

It should contain : ISBN, title, author, quantity as members;
set , get , print as methods.

and outside the class I should make functions for (borrowing - returning - searching for - printing all books)

The class should be a pointer in the main !

MY PROBLEMs are :
1. the program isn't giving me an error, but at the same time once I run it and call the first command, enter the info till the author name or quantity it crashes down.
2. I'm not sure if the way I'm using the pointer in the main is right, because it looks like I didn't fully understand the way it should be used in.

please if u could help me as soon as possible because the deadline is tomorrow :'O.
thanks in advance.

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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
#include <iostream>
#include <string>
using namespace std;

int size = 0;

class Book
{
private:
	string ISBN;
	string title;
	string author;
	int quantity;

public:
	Book(string serial, string tit, string auth, int qu)
	{
		ISBN = serial;
		title = tit;
		author = auth;
		quantity = qu;
	}
	Book()
	{}
	void set_ISBN(string serial)
	{
		ISBN = serial;
	}
	void set_title(string tit)
	{
		title = tit;
	}
	void set_author(string auth)
	{
		author = auth;
	}
	void set_quanitiy(int qu)
	{
		quantity = qu;
	}
	string get_ISBN()
	{
		return ISBN;
	}
	string get_title()
	{
		return title;
	}
	string get_author()
	{
		return author;
	}
	int get_quantity()
	{
		return quantity;
	}

	void print_book_info()
	{
		cout << "ISBN : " << ISBN << endl;
		cout << "Title : " << title << endl;
		cout << "Author : " << author << endl;
		cout << "Quantity : " << quantity << endl;
	}
	~Book()
	{}
};

void add_anew_book(Book *b, string IS, string title, string author, int qua)
{
	int i = 0;
	int q1;
	q1 = (b+i)->get_quantity();
	if (IS == b->get_ISBN())
	{
		cout << "The book is already in stock, we're gonna add one to it's quantity ! " << endl;
		q1++;
	}
	else
		size++;
	(b+i)->set_ISBN(IS);
	(b+i)->set_author(author);
	(b+i)->set_title(title);
	(b+i)->set_quanitiy(qua);
	i++;
}

bool borrow_abook(Book *b, string IS)
{
	int x;
	if (b->get_ISBN() == IS)
	{
		x = b->get_quantity();
		x--;
		return true;
	}
	else return false;
}

bool return_abook(Book *b, string IS)
{
	int x;
	if (b->get_ISBN() == IS)
	{
		x = b->get_quantity();
		x++;
		return true;
	}
	else return false;
}

bool search_for_abook(Book *b, string IS)
{
	if (b->get_ISBN() == IS)
	{
		return true;
	}
	else return false;
}

void print_all_books(Book *b)
{
	for (int i = 0; i < size; i++)
	{
		(b + i)->print_book_info();
	}
}

void main()
{
	Book *b;
	b = new Book [size];
	char c;
	cout << "Welcome to our BookStore, please choose one of the following : " << endl;
	cout << "1. To add a new book, please enter 'A': " << endl;
	cout << "2. To borrow a book, please enter 'B': " << endl;
	cout << "3. To return a borrowed book, please enter 'R': " << endl;
	cout << "4. To search for a book, please enter 'S': " << endl;
	cout << "5. to print a book's info, please enter 'P' :" << endl;
	cout << "6. to print the info of all the book avialable in stock, please enter 'T' :" << endl;
	cout << "7. To exit, please enter 'E' : " << endl;
	cin >> c;
	string IS , title , author;
	int qua;
	bool bor;
	bool ret;
	bool ser;
	while (c != 'E')
	{
		switch (c)
		{
		case 'A':	cout << "Enter the book ISBN to check if the book is already in stock or not to add it : " << endl;
			cin >> IS;
			b->set_ISBN(IS);
			cout << "Enter the book's title , author , quantity : " << endl;
			cin >> title >> author >> qua;
			b->set_title(title);
			b->set_author(author);
			b->set_quanitiy(qua);
			add_anew_book(b, IS, title , author, qua);break;
		case'B': cout << "Please enter the book's ISBN : " << endl;
			cin >> IS;
			bor = borrow_abook(b, IS);
			if (bor)
			{
				cout << "Enjoy reading the book, and please return the given book in a week." << endl;
			}
			else cout << "Sorry there is no avialabe copy right now, please check later ." << endl;  break;
		case'R':cout << "Please enter the book's ISBN : " << endl;
			cin >> IS;
			ret = return_abook(b, IS);
			if (ret)
			{
				cout << "Thanks for returning the book in time, hope you enjoyed it." << endl;
			}
			else cout << "Sorry, this book doesn't belong to our BookStore." << endl; break;
		case'S':cout << "Please enter the book's ISBN : " << endl;
			cin >> IS;
			ser = search_for_abook(b, IS);
			if (ser)
			{
				cout << "Book was found ." << endl;
			}
			else cout << "Book was not found" << endl;
			break;
		case'P':cout << "Please enter the book's position to print it's info :" << endl;
			int i;
			cin >> i;
			(b+i)->print_book_info();
			break;
		case'T':print_all_books(b);
			break;
		}
		cout << "1. To add a new book, please enter 'A': " << endl; 
		cout << "2. To borrow a book, please enter 'B': " << endl;
		cout << "3. To return a borrowed book, please enter 'R': " << endl; 
		cout << "4. To search for a book, please enter 'S': " << endl; 
		cout << "5. to print a book's info, please enter 'P' :" << endl; 
		cout << "6. to print the info of all the book available in stock, please enter 'T' :" << endl; 
		cout << "7. To exit, please enter 'E' : " << endl; 
		cin >> c;
	}

	system("pause");
}
void main() is illegal. main should return int;
b = new Book [size]; at this point size is 0; zero size arrays are illegal.
size++; What is this for. After that point nothing ever uses size
You must perform your program step by step - then you are understand why.
For exampl - I have run a program:
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
Book *b;
	b = new Book [size]; // allocate memory for zero, will crushed, but later when
                                          // when you are assigned a value to the variable 
	char c;
	cout << "Welcome to our BookStore, please choose one of the following : " << endl;
	cout << "1. To add a new book, please enter 'A': " << endl;
	cout << "2. To borrow a book, please enter 'B': " << endl;
	cout << "3. To return a borrowed book, please enter 'R': " << endl;
	cout << "4. To search for a book, please enter 'S': " << endl;
	cout << "5. to print a book's info, please enter 'P' :" << endl;
	cout << "6. to print the info of all the book avialable in stock, please enter 'T' :" << endl;
	cout << "7. To exit, please enter 'E' : " << endl;
	cin >> c;
	string IS , title , author;
	int qua;
	bool bor;
	bool ret;
	bool ser;
	while (c != 'E')
	{
		switch (c)
		{
		case 'A':	cout << "Enter the book ISBN to check if the book is already in stock or not to add it : " << endl;
			cin >> IS;
			b->set_ISBN(IS); // CRUSH 


source of your CRUSH: int size = 0 (out of main()); and b = new Book [size](in main());

As a variant u may assign size 10 for example, but taking into account that the cycle will take no more than 10 times( in main() )
Last edited on
Thanks alot for replying .
The problem is that I can't assign a specific value for the pointer's size . that's why I gave it an initial value "size = 0" , and the "size++" is to increase the array's size with every book added.

How can I move to the next book if I don't use the size++ ?
Thanks alot for helping out. Ok I kinda understand what you're saying.
the problem is that I declared "size" in the global scope so I can use it in (class - functions - main) .
I'll try moving it to the main ..
size++" is to increase the array's size
You cannot increase array size after it was created. What you can, is to create larger array, copy data from old array and to delete old one.

Also I skimmed though your code. It does not do what intended. At all. It needs to be rewritten.

Advice: for now do not use pointers at all. Create static array large enough to stoe all books you will need, make your program work with it and after that consider using dynamic arrays. And even then you should consider using standard containers with automatic memory management.
size++ - is wrong. size variable should be unchanged VALUE. b = new Book [size]; - So you allocate memory for the object b!!! Another saing - expression b = new Book [size]; denotes the number of objects b!!!! If size = 0 then number of objects b must be 0!!!
Last edited on
MiiNPaa Thanks I'll try that although I don't have much time left.


I made some changes to my code, as u all suggested. It works now but with alot of wrong values and mistakes.


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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
#include <iostream>
#include <string>
using namespace std;



class Book
{
private:
	string ISBN;
	string title;
	string author;
	int quantity;

public:
	int size = 0;
	Book(string serial, string tit, string auth, int qu)
	{
		ISBN = serial;
		title = tit;
		author = auth;
		quantity = qu;
	}
	Book()
	{}
	void set_ISBN(string serial)
	{
		ISBN = serial;
	}
	void set_title(string tit)
	{
		title = tit;
	}
	void set_author(string auth)
	{
		author = auth;
	}
	void set_quanitiy(int qu)
	{
		quantity = qu;
	}
	string get_ISBN()
	{
		return ISBN;
	}
	string get_title()
	{
		return title;
	}
	string get_author()
	{
		return author;
	}
	int get_quantity()
	{
		return quantity;
	}

	void print_book_info()
	{
		cout << "ISBN : " << ISBN << endl;
		cout << "Title : " << title << endl;
		cout << "Author : " << author << endl;
		cout << "Quantity : " << quantity << endl;
	}
	~Book()
	{}
};

void add_anew_book(Book *b, string IS, string title, string author, int qua)
{
	int size = 0;
	int i = 0;
	int q1;
	q1 = (b+i)->get_quantity();
	if (IS == b->get_ISBN())
	{
		cout << "The book is already in stock, we're gonna add one to it's quantity ! " << endl;
		q1++;
	}
	else
		size++;
	(b+i)->set_ISBN(IS);
	(b+i)->set_author(author);
	(b+i)->set_title(title);
	(b+i)->set_quanitiy(qua);
	i++;
}

bool borrow_abook(Book *b, string IS)
{
	int x;
	if (b->get_ISBN() == IS)
	{
		x = b->get_quantity();
		x--;
		return true;
	}
	else return false;
}

bool return_abook(Book *b, string IS)
{
	int x;
	if (b->get_ISBN() == IS)
	{
		x = b->get_quantity();
		x++;
		return true;
	}
	else return false;
}

bool search_for_abook(Book *b, string IS)
{
	if (b->get_ISBN() == IS)
	{
		return true;
	}
	else return false;
}

void print_all_books(Book *b)
{
	for (int i = 0; i < b->size; i++)
	{
		(b + i)->print_book_info();
	}
}

void main()
{
	Book *b;
	int size = 50;
	b->size;
	b = new Book [size];
	char c;
	cout << "Welcome to our BookStore, please choose one of the following : " << endl;
	cout << "1. To add a new book, please enter 'A': " << endl;
	cout << "2. To borrow a book, please enter 'B': " << endl;
	cout << "3. To return a borrowed book, please enter 'R': " << endl;
	cout << "4. To search for a book, please enter 'S': " << endl;
	cout << "5. to print a book's info, please enter 'P' :" << endl;
	cout << "6. to print the info of all the book avialable in stock, please enter 'T' :" << endl;
	cout << "7. To exit, please enter 'E' : " << endl;
	cin >> c;
	string IS , title , author;
	int qua;
	bool bor;
	bool ret;
	bool ser;
	while (c != 'E')
	{
		switch (c)
		{
		case 'A':	cout << "Enter the book ISBN to check if the book is already in stock or not to add it : " << endl;
			cin >> IS;
			b->set_ISBN(IS);
			cout << "Enter the book's title , author , quantity : " << endl;
			cin >> title >> author >> qua;
			b->set_title(title);
			b->set_author(author);
			b->set_quanitiy(qua);
			add_anew_book(b, IS, title , author, qua);break;
		case'B': cout << "Please enter the book's ISBN : " << endl;
			cin >> IS;
			bor = borrow_abook(b, IS);
			if (bor)
			{
				cout << "Enjoy reading the book, and please return the given book in a week." << endl;
			}
			else cout << "Sorry there is no avialabe copy right now, please check later ." << endl;  break;
		case'R':cout << "Please enter the book's ISBN : " << endl;
			cin >> IS;
			ret = return_abook(b, IS);
			if (ret)
			{
				cout << "Thanks for returning the book in time, hope you enjoyed it." << endl;
			}
			else cout << "Sorry, this book doesn't belong to our BookStore." << endl; break;
		case'S':cout << "Please enter the book's ISBN : " << endl;
			cin >> IS;
			ser = search_for_abook(b, IS);
			if (ser)
			{
				cout << "Book was found ." << endl;
			}
			else cout << "Book was not found" << endl;
			break;
		case'P':cout << "Please enter the book's position to print it's info :" << endl;
			int i;
			cin >> i;
			(b+i)->print_book_info();
			break;
		case'T':print_all_books(b);
			break;
		}
		cout << "1. To add a new book, please enter 'A': " << endl; 
		cout << "2. To borrow a book, please enter 'B': " << endl;
		cout << "3. To return a borrowed book, please enter 'R': " << endl; 
		cout << "4. To search for a book, please enter 'S': " << endl; 
		cout << "5. to print a book's info, please enter 'P' :" << endl; 
		cout << "6. to print the info of all the book available in stock, please enter 'T' :" << endl; 
		cout << "7. To exit, please enter 'E' : " << endl; 
		cin >> c;
	}

	system("pause");
}
Thanks for helping out . I just added a value to the size, and the program is working but it still preforming alot of mistakes .
You have many logic errors. For example:
b is an array. But on the lines 158, 161, etc you are using it as if it was a pointer to single value.

As I said, do not use pointers. At all. There should be no single new in your program.

You can declare static array as:
Book b[50];. Use pass-by reference if really needed. Or you can pass array and index to the function which need to manipulate array content.
Topic archived. No new replies allowed.