C++ book assignment

So This is what I have so far, and it compiles and runs, but it messes up after the second isbn under "Fuzzy Discrete Structures". Please reply with code structure, I'm still learning this and examples are better then reading full examples because I am a bit disabled when it comes to forming structure in sentences. Thank you!

main.cpp
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
#include "bookType.h"
#include <fstream>
#include <limits>


int main()
{
	bookType books[5];
	ifstream theFile("book.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;

} 




bookType.h
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;

}

book.dat
5
C++Programing: From Problem Analysis to Program Design
5-17-525281-3
ABC
2000
52.50
20
1
Malik, D.S.
Fuzzy Discrete Structures
3-7908-1335-4
Physica-Verlag
2000
89.00
10
2
Malik, Davender
Mordeson, John
Fuzzy Mathematic in Medicine
3-7908-1325-7
Physica-Verlag
2000
89.00
10
3
Mordeson, John
Malik, Davender
Cheng, Shih-Chung
Harry John and The Magician
0-239-23635-0
McArthur A. Devine Books
1999
19.95
10
3
Goof, Goofy
Pluto, Peter
Head, Mark
Dynamic InterWeb Programming
22-99521-453-1
GNet
1998
39.99
25
1


Thanks again!
Last edited on
Your input file format is not accurate - there is a stray 5 on the first line and the last book does not have any author named. Removing these I read the file (listed bottom) with the program below using a struct. You can modify it into a class with appropriate getters and setters if you wish:
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
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
#include <iostream>
#include <string>
#include <vector>
#include <fstream>
#include <sstream>
#include <limits>

struct Book
{
    std::string m_title;
    std::string m_ISBN;
    std::string m_publisher;
    unsigned int m_year;
    double m_price;
    unsigned int m_copies;
    unsigned int m_numAuthors;
    std::vector<std::string> m_authors;
};
std::ostream& operator << (std::ostream& os, const Book& b)//for printing Book objects
{
    os << b.m_title << "\n";
    os << b.m_ISBN << ", " << b.m_publisher << " (" << b.m_year << "), " << b.m_price << "\n";

    for (auto& elem : b.m_authors){os << elem << " ";};
os << "Copies available: " << b.m_copies << "\n";
    return os;
}

int main()
{
    std::vector<Book> books;
    std::ifstream inFile("D:\\input1.txt");//for storing Book objects
    while(inFile)
    {
        Book temp;
        getline(inFile, temp.m_title);
        getline(inFile, temp.m_ISBN);
        getline(inFile, temp.m_publisher);
        unsigned int temp_int;
        inFile >> temp.m_year;
        inFile.ignore(std::numeric_limits<std::streamsize>::max(), '\n');//for the newline characters
        inFile >> temp.m_price;
        inFile.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
        inFile >> temp.m_copies;
        inFile.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
        inFile>> temp_int;
        inFile.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
        temp.m_numAuthors = temp_int;
        unsigned int i{};
        std::string line;
        while ((i < temp_int )&& getline(inFile, line))//do as many time as there are authors for Book object
        {
            temp.m_authors.push_back(line);
            line = "";
            i++;
        }
        if(inFile)//check file is still open, avoids reading last entry twice
        {
            books.push_back(temp);
        }
    }
    for (auto& elem : books){std::cout << elem; std::cout << '\n';}//print using range-loops (C++11)
}

Input 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
C++Programing: From Problem Analysis to Program Design
5-17-525281-3
ABC
2000
52.50
20
1
Malik, D.S.
Fuzzy Discrete Structures
3-7908-1335-4
Physica-Verlag
2000
89.00
10
2
Malik, Davender
Mordeson, John
Fuzzy Mathematic in Medicine
3-7908-1325-7
Physica-Verlag
2000
89.00
10
3
Mordeson, John
Malik, Davender
Cheng, Shih-Chung
Harry John and The Magician
0-239-23635-0
McArthur A. Devine Books
1999
19.95
10
3
Goof, Goofy
Pluto, Peter
Head, Mark

Output
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
C++Programing: From Problem Analysis to Program Design
5-17-525281-3, ABC (2000), 52.5
Malik, D.S. Copies available: 20

Fuzzy Discrete Structures
3-7908-1335-4, Physica-Verlag (2000), 89
Malik, Davender Mordeson, John Copies available: 10

Fuzzy Mathematic in Medicine
3-7908-1325-7, Physica-Verlag (2000), 89
Mordeson, John Malik, Davender Cheng, Shih-Chung Copies available: 10

Harry John and The Magician
0-239-23635-0, McArthur A. Devine Books (1999), 19.95
Goof, Goofy Pluto, Peter Head, Mark Copies available: 10


Process returned 0 (0x0)   execution time : 0.125 s
Press any key to continue.

Topic archived. No new replies allowed.