Need help with abstract data type problem

Hey so this has been very very strange. Here is the problem:
Some of the characteristics of a book are the title, author(s), publisher, ISBN, price, and year of publication. Design a class bookType that defines the book as an ADT.

1. Each object of the class bookType can hold the following information about a book: title, up to four authors, publisher, ISBN, price, and number of copies in stock. To keep track of the number of authors, add another data member.
2. Include the member functions to perform the various operations on objects of the type bookType. For example, the usual operations that can be performed on the title are to show the title, and check whether a title is the same as the actual title of the book. Similarly, the typical operations that can be performed on the number of copies in stock are to show the number of copies in stock, set the number of copies in stock, update the number of copies in stock, and return the number of copies in stock. Add similar operations for the publisher, ISBN, book price, and author. Add the appropriate constructors and a destructor ( if one is needed).
3. Write the definitions of the member functions of the class bookType.

Write a program that uses the class bookType and test the various operations on the objects of class bookType. Declare an array of 100 components of the type bookType. Set to 5 components. Display the output.

Here is what I currently have although I have messed with it alot,
header file

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
#include <iostream>
#include <string>
#include <iomanip>
using namespace std;
class bookType
{
private:
	string _title;
	string _authors[4];
	string _publisher;
	string _ISBN;
	double _price;
	int _numOfcopies;
	int _numOfAuthor;
	string _year;
public:
	bookType();
	~bookType();
	void set_title(string title);
	string get_title();
	int compare_titles(string title);
	void list_authors();
	void set_numOfcopies(int num);
	int get_numOfcopies();
	void updateNumOfcopies(int num);
	//void show_numOfcopies();
	void set_publisher(string pub);
	string get_publisher();
//	void show_publisher();
	void set_ISBN(string isbn);
	string get_ISBN();
	//void show_ISBN();
	void set_price(double price);
	double get_price();
	//void show_price();
	void add_author(string author);
	string get_authors(int num);
	//void show_authors();
	void set_year(string year);
	string get_year();
	//void show_year();
	void clear_authors();
	void set_numOfAuthors(int num);
	int get_numOfAuthors();

	

	
};

bookType::bookType(){
	_title = "";
	_authors[0] = "";
	_authors[1] = "";
	_authors[2] = "";
	_authors[3] = "";
	_publisher = "";
	_price = 0;
	_ISBN = "";
	_numOfAuthor = 0;
	_numOfcopies = 0;
	_year = "";
}
bookType::~bookType()
{
	_title = "";
	_authors[0] = "";
	_authors[1] = "";
	_authors[2] = "";
	_authors[3] = "";
	_publisher = "";
	_price = 0;
	_ISBN = "";
	_numOfAuthor = 0;
	_numOfcopies = 0;
	_year = "";
}

void bookType::set_title(string title){
	_title = title;
}
string bookType::get_title(){
	return _title;
}
void bookType::clear_authors(){
	_numOfAuthor = 0;
}
void bookType::set_publisher(string pub){
	_publisher = pub;
}
string bookType::get_publisher(){
	return _publisher;
}

void bookType::add_author(string author)
{
	
		_authors[_numOfAuthor] = author;
		
	
}
void bookType::set_numOfAuthors(int num)
{
	_numOfAuthor = num;
}
int bookType:: get_numOfAuthors()
{
	return _numOfAuthor;
}
void bookType::list_authors()
{
	int i = 0;
	cout << "Authors are" << endl;
	for (int i = 0; i < 4; i++)
		if (_authors[i] != "")
		{
			cout << _authors[i] << endl;
		}
	

}
void bookType::set_numOfcopies(int num)
{
	_numOfcopies = num;
}
void bookType::updateNumOfcopies(int num)
{
	_numOfcopies = _numOfcopies + num;
}
int bookType::get_numOfcopies()
{
	return _numOfcopies;
}

void bookType::set_ISBN(string isbn)
{
	_ISBN = isbn;
}
string bookType::get_ISBN()
{
	return _ISBN;
}
void bookType::set_price(double price)
{
	_price = price;
}
double bookType::get_price()
{
	return _price;
}
void bookType::set_year(string year)
{
	_year = year;
}
string bookType::get_year()
{
	return _year;
}
int bookType::compare_titles(string title)//check this
{
	int i = (title == _title);
	return i;

}




here is my main
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
#include "bookType.h"
#include <fstream>


int main()
{
	bookType books[5];
	ifstream theFile("books.dat");
	string line;
	string line2;
	double number;
	for (int i = 0; i < 5; i++)
	{

	getline(theFile, line);
	books[i].set_title(line);
	getline(theFile, line);
	books[i].set_ISBN(line);
	getline(theFile, line);
	books[i].set_publisher(line);
	getline(theFile, line);
	books[i].set_year(line);
	theFile >> number;
	theFile.ignore(numeric_limits<streamsize>::max(), '\n');
	books[i].set_price(number);
	theFile >> number;
	theFile.ignore(numeric_limits<streamsize>::max(), '\n');
	books[i].set_numOfcopies(number);
	theFile >> number;
	theFile.ignore(numeric_limits<streamsize>::max(), '\n');
	books[i].set_numOfAuthors(number);
	//theFile >> line;
	//books[i].add_author(line);
	
	}
	
	for (int i = 0; i < 5; i++)
	{
		cout << books[i].get_title();
		cout << endl;
		cout << books[i].get_ISBN();
		cout << endl;
		cout << books[i].get_publisher();
		cout << endl;
		cout << books[i].get_year();
		cout << endl;
		cout << books[i].get_price();
		cout << endl;
		cout << books[i].get_numOfcopies();
		cout << endl;
		cout << books[i].get_numOfAuthors();
		cout << endl;
		//books[i].list_authors();
	}
	
	//a[2].list_authors();
	cout << endl;
	//a[0].get_authors(1);
	std::cin.get();
	return 0;

}


Sadly the code button doesn't seem to be working again so I typed it in manually but hopefully that will work. Anyways I was wondering what I am missing because I have tried to test reading the file and assigning it to each variable for each book. First I did just the first book which worked but when I looped to the second book after the first two strings for the second book it started to spit out 1's and other random values. Then I tried a second way which worked expect for after the firt book the doubles like price were only showing as ints. Then the third way which I have in this post worked for everything for each book expect for the authors which I had commented because I wanted to make sure everything else was working before that part.

Ok now for the strange part, it seems for each time I would tweak with it and get it closer to working but then when I was continue tweaking with it I would get some crazy outputs. Then I would go back to what had been working but when I run it I get more crazy outputs or errors with the same code that had been working. Then I restart my vso and it will run correctly again, expect for this last version which it will just allow me to run without an error but still is not giving correct outputs. Am I missing something? Does it have to do with not using the deconstruct correctly? It just seems like my memory is getting messed up which is affecting the output.
O yes I forget the file, and this way seems to be working for now again (besides the authors which i took out of the file) but I am not sure if I am missing something that is affecting the memory.

books.dat
C++Programing: From Problem Analysis to Program Design
5-17-525281-3
ABC
2000
52.50
20
1
Fuzzy Discrete Structures
3-7908-1335-4
Physica-Verlag
2000
89.00
10
2
Fuzzy Mathematic in Medicine
3-7908-1325-7
Physica-Verlag
2000
89.00
10
3
Harry John and The Magician
0-239-23635-0
McArthur A. Devine Books
1999
19.95
10
3
Dynamic InterWeb Programming
22-99521-453-1
GNet
1998
39.99
25
1




Hi,

Just some hopefully helpful general advice.

Set your compiler warnings as high as you can. It's a bit awkward in VS, (from what I remember of the short time I used it), because the highest level prints heaps of warnings about the normal header files.

If your code compiles, and the output is not what you expect, then learn to use a debugger. One can set breakpoints, a variable watch-list, watch how the values of variables change as one steps through the code 1 line at a time, deducing where things went wrong. This will save you days of staring at code.


Now for the code, or at least the design of it :+)

Because you have numbers_of various variables, these should at least be static, because then there will be only one value for that variable of all the instances of the class. If it is not static, then each instance can have it's own value - which is not what you want.

I would even be temped to have bookType as a struct with public access to everything, then have a bookCollection class which would hold a private std::vector of bookType, as well the numbers of copies and other quantities. That way the collection is in it's own class, and not an array in main()

With your code we have the typical scenario where there is a get / set function for each member, which was probably not helped by the wording of the assignment. There are a few things you can do.

Use an initialiser list in a constructor, this sets the values of the member variables conceptually before the object is created. One only needs set functions if changing the values after the object has been created - even then one may be able to have 1 function that updates several or all the variables at once. So this could avoid all those set functions. I suppose the trouble is that the assignment explicitly asked for these get /set functions. The problem I have with this is that it reinforces the get / set mentality, whereas in reality it is often not needed.

I don't have a problem with trivial get functions, but if you are going to print all the variables of the object, then there are other options. One is to have 1 PrintDetails function - remember a member function has direct access to all the other member variables. Another way is to overload the << operator, make it a friend of the class, and code it so it std::cout all the member variables.

I don't know how much of this you are allowed to implement, you could at least do the void PrintDetails() const {} function, any way I hope that I have mentioned some ideas that could be useful in the future.

Another thing is: don't have using namedpace std; Google to see why, and what to do instead. Note that all the expert members like JLBorges always post code with std::cout etc.

One final thing is const correctness. If a member function doesn't change the value of any member variable it can be marked const. Parameters to functions can often be const as well, unless you a changing the value in the outer scope, in which case they should be a reference. Any value which is truly a constant, like pi for example should definitely be const or even better a constexpr

Any way Good Luck :)
Topic archived. No new replies allowed.