help with adt problem

Hi,
So I'm trying to write a code that reads in from a file a book title, number of authors, authors names (up to 4), publisher, isbn, price, and the number of copies. There is one item per line.
This information needs to be stored in an array then displayed, sorted by book title then displayed again.
It then needs to read in a series of transactions from another file and complete a task for each transaction.
I'm only at the beginning right now. I'm trying to fill the array and display it the first time (unsorted). What I have builds fine but when I try to run it I'm getting a debug error and I don't know why. Could someone please take a look and explain what's happening? thank you!

here is my code:
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
#include"my_class.h"

const int ARRAY_SIZE = 50;
void print(Book_Type array[], int size);

int main()
{
	ifstream infile;
	Book_Type books[ARRAY_SIZE];
	string title, author[4], isbn, pub;
	double cost;
	int num_copies, num_auth, count = 0;
	infile.open("mp7datados.txt");

	getline(infile, title);
	infile >> num_auth;
	for (int i = 0; i < num_auth; i++)
	{
		getline(infile, author[i]);
	}
	getline(infile, pub);
	infile >> isbn >> cost >> num_copies;

	while (!infile.eof())
	{
		books[count++].load_data(title, num_auth, author, pub, isbn, cost, num_copies);
		getline(infile, title);

		infile >> num_auth;
		for (int i = 0; i < num_auth; i++)
		{
			getline(infile, author[i]);
		}
		getline(infile, pub);
		infile >> isbn >> cost >> num_copies; 
	}
	print(books, count);

	return 0;
}

void print(Book_Type array[], int size)
{
	string tite, auth[4], _isbn, pub, author;
	double cost;
	int num_cops, num_auths;
	for (int i = 0; i < size; i++)
	{
		array[i].return_data(tite, num_auths, auth, pub, _isbn, cost, num_cops);
		cout << "Title: " << tite << endl;
		if (num_auths > 1)
		{
			cout << "Authors: ";
			for (int x = 0; x < num_auths; x++)
			{
				auth[x] = author;
				cout << author << endl;
			}
		}
		else
		{
			for (int y = 0; y < num_auths; y++)
			{
				auth[y] = author;
				cout << "Author: " << author << endl;
			}
		}
		cout << "Publisher: " << pub << endl;
		cout << "ISBN: " << _isbn << " Price: " << cost << " Number of Copies: " << num_cops << endl;

	}
}


my 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
#pragma once
#include<iostream>
#include<fstream>
#include<string>
#include<iomanip>

using namespace std;

class Book_Type
{
public:
	void load_data(string Title, int NumAuth, string Auth[4], string Pub, string Isbn, double Cost, int Copies);
	void return_data(string& Title, int& NumAuth, string Auth[4], string& Pub, string& Isbn, double& Cost, int& Copies);
	int numb_copies();
	string ISBN();
	void check_price(int cost);
	void set_copies(int Copies);
	void add_copies(int Copies);
	void sub_copies(int Copies);
private:
	string title;
	int num_authors;
	string authors[4];
	string publisher;
	string isbn;
	double price;
	int num_copies;

};

void Book_Type::load_data(string Title, int NumAuth, string Auth[4], string Pub, string Isbn, double Cost, int Copies)
{
	title = Title;
	num_authors = NumAuth;
	authors[4] = Auth[4];
	publisher = Pub;
	isbn = Isbn;
	price = Cost;
	num_copies = Copies;
}

void Book_Type::return_data(string& Title, int& NumAuth, string Auth[4], string& Pub, string& Isbn, double& Cost, int& Copies)
{
	Title = title;
	NumAuth = num_authors;
	Auth[4] = authors[4];
	Pub = publisher;
	Isbn = isbn;
	Cost = price;
	Copies = num_copies;
}

int Book_Type::numb_copies()
{
	
	return num_copies;
}

string Book_Type::ISBN()
{
	return isbn;
}

void Book_Type::check_price(int cost)
{
	if (cost < 0)
	{
		cout << "You cannot have a negative cost" << endl;
	}
	if (cost >= 0)
	{
		price = cost;
	}
}

void Book_Type::set_copies(int Copies)
{
	if (Copies < 0)
	{
		cout << "Cannot have a negative number of copies" << endl;
	}
	num_copies = Copies;
}

void Book_Type::add_copies(int Copies)
{
	if (Copies < 0)
	{
		cout << "Cannot add a negative number of copies" << endl;
	}
	num_copies += Copies;

}

void Book_Type::sub_copies(int Copies)
{
	if (Copies < 0)
	{
		cout << "Connot subtract a negative number of copies" << endl;
	}
	if (num_copies - Copies < 0)
	{
		cout << "Cannot have a negative number of books" << endl;
	}
	num_copies -= Copies;
}

Line 35 and 46 are clear errors.
1
2
3
4
5
6
7
8
9
10
// given
string authors[4];

authors[0] is a string
authors[1] is a string
authors[2] is a string
authors[3] is a string
// but
authors[4] is not part of the array.
If it were, it would be a string, not array.

If you want to copy an array, then you have to copy every element individually:
1
2
3
for ( int au=0; au < NumAuth; ++au ) {
  authors[au] = Auth[au];
}

OR
std::copy_n( Auth, NumAuth, authors );


Overall, I would recommend changing the main() into:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
int main()
{
	std::ifstream infile("mp7datados.txt");
	std::vector<Book_Type> books;
	Book_Type entry;
	while ( infile >> entry )
	{
		books.push_back( entry );
	}

	for ( const auto & book : books )
	{
		std::cout << book << '\n';
	}

	return 0;
}


This requires that you define two more functions:
1
2
3
4
5
6
7
std::istream& operator>> ( std::istream& in, Book_Type& book ) {
  // read from in and store in book
}

std::ostream& operator<< ( std::ostream& out, const Book_Type& book ) {
  // print book to out
}
You should check that the file opened before reading from it.
Topic archived. No new replies allowed.