Not getting the right output for a program that uses Composition

Hello, the output for my program is not displaying the names and addresses that are read in from a textfile. Instead, it displays the initialized values from the default constructor.

So, if I were to change the code in the Author.cpp file to:

1
2
3
4
5
Author::Author()
{
	name = "sdfsdfsf";
	address = "sdfsdfsf";
}


Those values would be displayed in all iterations of my displayBooks function. I'm not getting any compiler errors and the output is correct for the Book class. I don't know why this is happening. Any help would be appreciated.


Driver.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
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
  // driver code

#include "driver.h"

int main()
{
	// Display Menu 
	int option = 0;
	const int CREATE = 1;
	const int READ = 2;

	cout << "\nCS 1410 Project 3";
	cout << "\nSelect one of the following two options: ";
	cout << "\n   1 - create a test file";
	cout << "\n   2 - read the test file and display the results";
	cout << "\n>> ";

	// run the selected option
	cin >> option;
	if (option == CREATE)
	{
		createTestFile();
		cout << "\nTest file has been created. ";
	}
	else if (option == READ)
	{
		readTestFile();
	}
	else
	{
		cout << "\nInvalid option.";
	}

	system("PAUSE");
	return 0;
}

void displayBooks(const vector<Book>& books)
{
	// set up cout to display currency
	cout.setf(ios::fixed);
	cout.setf(ios::showpoint);
	cout.precision(2);

	// display heading
	cout << "\nRecommended Reading List\n";

	// display each account
	for (unsigned i = 0; i < books.size(); i++)
	{
		Author p = books[i].getAuthor();
		cout << books[i].getTitle() << '\n';
		cout << p.getName() << '\n';
		cout << p.getAddress() << '\n';
		cout << books[i].getPages() << " pages\n";
		cout << '$' << books[i].getPrice() << "\n\n\n";
	}
}

void createTestFile()
{
	// create a vector for storing the account objects
	vector<Book> myBooks;

	// create three Author objects
	Author p1("J.K.Rowling", "Edinburgh, Scotland");
	Author p2("Suzanne Collins", "Connecticut, USA");
	Author p3("J.R.R. Tolkien", "Bournmouth, England");

	// Create three Book objects
	Book b1(p1, "Harry Potter and the Sorcerer's Stone", 256, 24.95);
	Book b2(p2, "Mockingjay", 400, 12.99);
	Book b3(p3, "The Hobbit", 322, 14.29);

	// add the books to the vector
	myBooks.push_back(b1);
	myBooks.push_back(b2);
	myBooks.push_back(b3);

	// write the books to a file
	// the file will be in the same folder as the executable file
	// assume that the file opens
	ofstream outputFile;
	outputFile.open("bookData.txt");

	for (unsigned i = 0; i < myBooks.size(); ++i)
	{
		myBooks[i].writeData(outputFile);
	}
}

void readTestFile()
{
	vector<Book> myBooks;
	ifstream inputFile;

	try
	{
		openFile(inputFile, "bookData.txt");
	}

	catch (Exceptions e)
	{
		int error = e.getError();

		if (error == OPEN_ERROR)
		{
			cout << "There was a problem opening the file...";
			system("PAUSE");
			exit(1);
		}		
	}
	
	if (inputFile.good())
	{
		while (!inputFile.eof())
		{
			Book b;

			try
			{
				b.readData(inputFile);
			}

			catch (Exceptions e)
			{
				int error = e.getError();

				if (error == READ_ERROR)
				{
					cout << "There was a problem reading the file...\n";
					system("PAUSE");
					exit(1);
				}
			}

			if (inputFile.good())
			{
				myBooks.push_back(b);
			}
		} 

		displayBooks(myBooks);
	}
}

void openFile(ifstream& in, const string& _name)
{
	// try to open the file
	in.open(_name);

	// if the fail but is set, teh file won't open ... throw an open error
	if (!in)
	{
		throw Exceptions(OPEN_ERROR);
	}
}


Book.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
64
65
66
67
68
69
70
71
72
73
74
75
76
#include "Book.h"

Book::Book()
{
	title = "";
	pages = 0;
	price = 0;
}

Book::Book(Author _author, string _title, int _pages, double _price)
{
	writer = _author;
	title = _title;
	pages = _pages;
	price = _price;
}

Author Book::getAuthor() const
{
	return writer;
}

string Book::getTitle() const
{
	return title;
}

int Book::getPages() const
{
	return pages;
}

double Book::getPrice() const
{
	return price;
}

void Book::readData(ifstream& in)
{
	string strPages;
	string strPrice;

	getline(in, title, '\n');
	getAuthor().readData(in);
	getline(in, strPages, '\n');
	getline(in, strPrice, '\n');

	try
	{
		pages = stoi(strPages);
		price = stod(strPrice);
	}

	catch (invalid_argument)
	{
		throw (READ_ERROR);
	}

	if (in.fail() && !in.eof())
	{
		throw Exceptions(READ_ERROR);
	}

	if (in.fail() && in.eof())
	{
		throw Exceptions(END_ERROR);
	}
}

void Book::writeData(ofstream& out) const
{
	out << getTitle() << '\n';
	getAuthor().writeData(out);
	out << getPages() << '\n';
	out << getPrice() << '\n';
}


Book.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
#pragma once
#include "Author.h"

class Book
{
private: 
	Author writer;
	string title; 
	int pages; 
	double price; 

public: 
	Book();
	Book(Author, string, int, double);

	Author getAuthor() const;

	string getTitle() const;
	int getPages() const;
	double getPrice() const;

	void readData(ifstream&);
	void writeData(ofstream&) const;
};


Author.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
#include "Author.h"

Author::Author()
{
	name = "";
	address = "";
}
Author::Author(string _name, string _address)
{
	name = _name; 
	address = _address;
}

string Author::getName() const
{
	return name;
}

string Author::getAddress() const
{
	return address; 
}

void Author::readData(ifstream& in)
{
	getline(in, name, '\n');
	getline(in, address, '\n');

	if (in.fail() && !in.eof())
	{
		throw Exceptions(READ_ERROR);
	}

	if (in.fail() && in.eof())
	{
		throw Exceptions(END_ERROR);
	}
}

void Author::writeData(ofstream& out) const
{
	out << name << '\n';
	out << address << '\n';
}


Author.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#pragma once
#include <iostream>
#include <string>
#include <fstream>
#include "Exceptions.h"
using namespace std; 

class Author
{
private:
	string name; 
	string address; 

public: 
	Author();
	Author(string, string);

	string getName() const;
	string getAddress() const; 

	void readData(ifstream&);
	void writeData(ofstream&) const;
};


Last edited on
Check line 44 in your book.cpp
Thank you very much. I figured out what the problem was.
Topic archived. No new replies allowed.