Question about array of objects....

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Sep 2008
Posts: 38
Reputation: cproud21 has a little shameless behaviour in the past 
Solved Threads: 0
cproud21 cproud21 is offline Offline
Light Poster

Question about array of objects....

 
0
  #1
Mar 25th, 2009
I have the following program that declares 100 Book objects... The way that I have it now, each item in the array is set to a default value. Is there a way to add only the items that are input by the user to the array? and not start off with 100 "John Doe" instances?

here is the code...
#include <ostream>
using namespace std;

class Book
	{
	private:
		string title;		
		string authorLastName;
		string authorFirstName;
		int year;
		
	public:
		Book();	// The default constructor
		// The default constructor sets the title to ìunknownî,
		// the author to ìJohn Doeî, and the year to 0
		Book(string, string, string, int);	
		Book(const Book& source);	// The copy constructor
		
		// Mutator functions
		void setTitle(string);
		void setAuthor(string, string);
		bool setYear(int);		// A valid year is any year from
		// 1500 and 2009. The function will set a valid year to the year
		// member variable and return true. It not do the set on an invalid
		// year, and return false.
		
		// Accessor functions
		string getTitle();
		string getAuthorLastName();
		string getAuthorFirstName();
		int getYear();
		bool operator==(const Book & right);
		
		
		
		
		
	};

#include <iostream>
using namespace std;
#include <string>
#include "Book.h"
#include <ostream>

Book::Book()
{
	title = "Unknown";
	authorLastName = "Doe";
	authorFirstName = "John";
	year = 1500;
}

Book::Book(string ti, string ln, string fn, int yr)
{
	title = ti;
	authorLastName = ln;
	authorFirstName = fn;
	year = yr;
}

Book::Book(const Book& source)
{
	title = source.title;
	authorLastName = source.authorLastName;
	authorFirstName = source.authorFirstName;
	year = source.year;
}

void Book::setTitle(string ti)
{
	title = ti;
}

void Book::setAuthor(string ln, string fn)
{
	authorLastName = ln;
	authorFirstName = fn;
}



bool Book::setYear(int yr)
{
	bool result = false;
	if (year >= 1500 && year <= 2009)
	{	result = true;
		year = yr;
	}
	else
		result = false;
	
	return result;
}

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

string Book::getAuthorLastName()
{
	return authorLastName;
}

string Book::getAuthorFirstName()
{
	return authorFirstName;
}

int Book::getYear()
{
	return year;
}
bool Book::operator==(const Book & right)
{
	if(title == right.title && authorLastName == right.authorLastName)
		return true;
	else
		return false;
}

#include <iostream>
using namespace std;
#include <string>
#include "Book.h"


void displayMenu();
void selectionSort(Book[], int);
int search(Book[], string, int);

int main()
{
	const int SIZE = 100;
	Book bookList[SIZE];
	string locTitle;
	string locLname;
	string locFname;
	int locYear = 0;
	int choice = 1;
	
	while (choice != 5)
	{
		displayMenu();
		cin >> choice;
		
		
		if (choice == 1)
		{
			int location = 0;
			for (int x = 0 ; x < 1 ; x++)
			{	
				cout << "Enter the Title: ";
				cin >> locTitle;
				cout << "Enter the author's last name: " ;
				cin >> locLname;
				cout << "Enter the author's first name: " ;
				cin >> locFname;
				cout << "Enter the year of publication for the book: " ;
				cin >> locYear;
				
				cout << "Enter the location of the book, which you would like to overwrite: ";
				cin >> location;
				bookList[location - 1].setTitle(locTitle);
				bookList[location - 1].setAuthor(locLname, locFname);
				bookList[location - 1].setYear(locYear);
			}
			
		}
		
		if (choice == 2)
		{
			for (int r = 0; r < SIZE; r++)
			{
				cout << "Title: " << bookList[r].getTitle() << endl;
				cout << "Author Last Name: " << bookList[r].getAuthorLastName() << endl;
				cout << "Author First Name: " << bookList[r].getAuthorFirstName() << endl;
				cout << "Year: " <<bookList[r].getYear() << endl;
				cout << " ---------------------------------- " << endl;
				cout << endl;
				
			}
			cout << " ---------------------------------- " << endl;
			cout << " ---------------------------------- " << endl;
		}
		
		if (choice == 3)
		{
			selectionSort(bookList, SIZE);
		}
		
		if (choice == 4)
		{
			int results = 0;
			string searchTarget;
			cout<< "Enter the author to search for: ";
			cin >> searchTarget;
			results = search(bookList, searchTarget, SIZE);
			
			
			if (results = -1)
			{
				cout << "Entry Not Found!" << endl;
			}
			else
			{
				cout << "Results" << endl;
				cout << "Title: " << bookList[results + 1].getTitle() << endl;
				cout << "Author Last Name: " << bookList[results + 1].getAuthorLastName() << endl;
				cout << "Author First Name: " << bookList[results + 1].getAuthorFirstName() << endl;
				cout << "Year: " <<bookList[results + 1].getYear() << endl;
				cout << " ---------------------------------- " << endl;
				cout << endl;
			}
			
		}
	}

	
	return 0;
}

void displayMenu()
{
	cout << "Menu" << endl;
	cout << "1. Add a book to the list. " << endl;
	cout << "2. Print out the current list of books. " << endl;
	cout << "3. Sort the list by the author's last name. " << endl;
	cout << "4. Search for books by author.  " << endl;
	cout << "5. Exit the program. " << endl;
	cout << "Enter your choice: ";
}

void selectionSort(Book list[], int SIZE)
{
	int swp_index = 0; //tracks smallest found value
	Book temp;
	
	for (int a = 0; a < (SIZE - 1); a++)
	{
		swp_index = a;
		for(int index = a + 1; index < SIZE; index++)
		{
			if (list[index].getAuthorLastName() < list[swp_index].getAuthorLastName() )
			{
				swp_index = index;
			}
		}
		
		temp = list[a];
		list[a] = list[swp_index];
		list[swp_index] = temp;
	}
}

int search(Book list[], string value, int size)// method to find index of specified element
{
	int index = 0;
	int position = -1;
	bool found = false;
	
	while(index < size && !found)
	{
		if (list[index].getAuthorLastName() == value)
		{
			found = true;
			position = index;
		}
		index++;
	}
	return position;
}
Reply With Quote Quick reply to this message  
Join Date: Mar 2009
Posts: 39
Reputation: Danny_501 is an unknown quantity at this point 
Solved Threads: 1
Danny_501's Avatar
Danny_501 Danny_501 is offline Offline
Light Poster

Re: Question about array of objects....

 
0
  #2
Mar 25th, 2009
So what your saying is you don't want just 100 items in the array? (more or less)
Reply With Quote Quick reply to this message  
Join Date: Mar 2009
Posts: 39
Reputation: Danny_501 is an unknown quantity at this point 
Solved Threads: 1
Danny_501's Avatar
Danny_501 Danny_501 is offline Offline
Light Poster

Re: Question about array of objects....

 
0
  #3
Mar 25th, 2009
LOL why did i even ask that, of course thats what you mean.

The only solution to your problem is you need to store the data dynamically. Consider looking up 'Dynamic Memory Allocation', or 'Data Structures'.

Maybe you could use the book objects in a 'Linked List' instead of an array.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC