# [resolved] Help with c++...methods, constructors,arrays, classes...



## Cro (Oct 10, 2005)

*Help with c++...methods, constructors,arrays, classes...*

hi guys.. im just having a nightmare solving this assignment i have.... Ive done quite alot but now im stuck.. i hope somebody can help me... pleaes bare with me:

ok so here is the situation:

1 - i created the CBook.h in which i created definitions for ADT for a book.
- you can see the file code below...im not sure about the authors as it 
needs to be an array of up to 4 authors..(i dont think i did that  )
2 - i also created the CBook.cpp where i wrote the class implementations:
- here im not sure about the "isAuthor" method as it needs to accept
all the values a book object needs and assigns them to the object.
3 - finally the main program the book.cpp file (the driver program).
- the thing that needs to be done here is:

1 - Create an array that contains one author to be used to pass to the setBookInfo method. Declare a signle book object using values you make up. Assign the object values using the setBookInfo method. Display the contents of the object.

2 - Declare an array in the driver program of 15 book objects called bookArray. Declare an integer which keeps track of how many elemnts are in the array. Write a function called populateArray which accepts the book array and a reference argument of the count of how many elements. The function will open the supplied file and assign the book information to book objects wihin the array.

3 - the main program should loop around displaying all the book information read in.

4 - Ask the user to enter the name of an Author. Write a loop that will look to see which books have that author and for each such book displays the book title.

*Now here is how the input file looks:* without the comments

5 -- this just says that there is 5 records 
C++Programing: From Problem Analysis to Program Design -- this is the title
ABC -- this is the publisher
2000 -- this is the year
52.50 -- this is the price
20 -- this is the quantity
1 -- this says that there is one author
Malik, D.S. -- this is the author // and then so on... with the others
Fuzzy Discrete Structures
Physica-Verlag
2000
89.00
10
2
Malik, Davender
Mordeson, John
Fuzzy Mathematic in Medicine
Physica-Verlag
2000
89.00
10
3
Mordeson, John
Malik, Davender
Cheng, Shih-Chung
Harry John and The Magician
McArthur A. Devine Books
1999
19.95
10
3
Goof, Goofy
Pluto, Peter
Headington, Mark
Dynamic InterWeb Programming
GNet
1998
39.99
25
1
Alvin Monkey



*the CBook.h file:*


```
#include <string>

using namespace std;

#ifndef CBOOK_H
#define CBOOK_H

//////////////////////////   1    \\\\\\\\\\\\\\\\\\\\\\\\\\\\\/
class CBookProperties
{
	public:
		CBookProperties(); //constructor for unassigned
		
		//Gets----------------
		string getTitle() const;
		string getPublisher();
		double getPrice();
		int getQuantity();
		//--------------------

		//Sets----------------
		void setTitle(string title);
		void setPublisher(string publisher);
		void setPrice(double price);
		void setQuanity(int quantity);

		void setBookInfo(string title, string publisher, string authors, int year, double price, int quantity);
		//--------------------

		bool isAuthor(string) const; // for checking if the author name exists
		bool isInStock(int) const; // 
		void makeSale();

		void printInfo(); // displays the information

		static int count;

	private:
		string bookTitle;
		string bookPublisher;
		string authors;
		int publicationYear;
		double price;
		int quantityInStock;
		int numOfAuthors;
};
#endif
```
*the CBook.cpp file:*


```
#include "CBook.h"
#include <iostream>
#include <iomanip>

using namespace std;

int CBookProperties::count = 0;

//////////////////////////////////////   2   \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\/
CBookProperties::CBookProperties()
{
	bookTitle = "*unassigned*";
	bookPublisher = "*unassigned*";
	authors = "*unassigned*";
	publicationYear = 0;
	price = 0.00;
	quantityInStock = 0;
	numOfAuthors = 0;
}

////////////////////////   3   -   Set Methods   \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\/
void CBookProperties::setTitle(string inTitle)
{
	bookTitle = inTitle;
}

void CBookProperties::setPublisher(string inPublisher)
{
	bookPublisher = inPublisher;
}

void CBookProperties::setPrice(double inPrice)
{
	price = inPrice;
}

void CBookProperties::setQuanity(int inQuantity)
{
	quantityInStock = inQuantity;
}

/////////////////////////   4   -   Get Methods   \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\/
string CBookProperties::getTitle() const
{
	return bookTitle;
}

string CBookProperties::getPublisher()
{
	return bookPublisher;
}

double CBookProperties::getPrice()
{
	return price;
}

int CBookProperties::getQuantity()
{
	return quantityInStock;
}

/////////////////////////   5   -   setBookInfo method   \\\\\\\\\\\\\\\\\\\\\\\\\\/
void CBookProperties::setBookInfo(string inTitle, string inPublisher, string author, int year, double inPrice, 
								  int inQuantity)
{
	bookTitle = inTitle;
	bookPublisher = inPublisher;
	authors = author;
	publicationYear = year;
	price = inPrice;
	quantityInStock = inQuantity;
	//count++;
}

/////////////////////////   6   -   isAuthor method   \\\\\\\\\\\\\\\\\\\\\\\\\\\\\/
bool CBookProperties::isAuthor(string inName) const
{
	if(authors == inName)
		return true;
	return false;
}

/////////////////////////   7   -   isInStock method   \\\\\\\\\\\\\\\\\\\\\\\\\\\\/
bool CBookProperties::isInStock(int inStock) const
{
	if(quantityInStock >= 1)
		return true;
	return false;
}

/////////////////////////   8   -   makeSale method   \\\\\\\\\\\\\\\\\\\\\\\\\\\\/
void CBookProperties::makeSale()
{
	quantityInStock--;
}

/////////////////////////   9   -   printInfo method   \\\\\\\\\\\\\\\\\\\\\\\\\\\/
void CBookProperties::printInfo()
{
	cout << endl << fixed << showpoint << setprecision(2)
		 << "****************************" << endl
		 << "Title: " << bookTitle << endl
		 << "Publisher: " << bookPublisher << endl
		 << "Year of Publication: " << publicationYear << endl
		 << "Number of Authors: " << numOfAuthors + 1 << endl
		 << "Authors:" << endl << authors << endl
		 << "Price: " << price << endl
		 << "Quantity in Stock: " << quantityInStock << endl
		 << "****************************" << endl << endl;
}
```
*the book.cpp file:*

here i played with the main program but im really stuck::


```
#include <iostream>
#include <iomanip>
#include <string>
#include <fstream>
#include "CBook.h"

using namespace std;

//void populateArray(

void main()
{
	string title;
	string publisher;
	string authors;
	int year;
	double price;
	int quantity;

	int index = 4;

    ifstream infile;
	infile.open("C:\\Documents and Settings\\Magas\\Desktop\\SCHOOL\\C++Assign3\\Assignment3\\bookData2.txt");

	CBookProperties myArray[4];

	//infile >> title >> publisher >> year >> price >> quantity;
	for(int i = 0; i < index; i++)
	{

		getline(infile, title);
		getline(infile, publisher);
		infile >> year;
		infile >> price;
		infile >> quantity;
		
		myArray[i].setBookInfo(title, publisher, authors, year, price, quantity);
	}

	for(int i = 0; i < index; i++)
		myArray[i].printInfo();
}
```
please if somebody can help me a little bit with the main program ... and please if you can check if i did everything correctly up until now? .... any help is very much appreciated... :4-dontkno


----------

