| | |
Question about array of objects....
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Sep 2008
Posts: 38
Reputation:
Solved Threads: 0
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...
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;
}![]() |
Similar Threads
- Allocating an array of objects ? (C++)
- Array of objects, sorting it in ascending order of a certain attribute. (Java)
- One object vs Many objects (C++)
- Separating parts of an object (Java)
- JSON to class object (ASP.NET)
- add event listeners for dynamic list of objects (JavaScript / DHTML / AJAX)
- question on freeing memory (C++)
- array of objects memory allocation (C++)
Other Threads in the C++ Forum
- Previous Thread: Print Array: strings!
- Next Thread: How do u build a graph????
| Thread Tools | Search this Thread |
api array based binary c++ c/c++ calculator char char* class classes code coding compile console conversion count database delete deploy desktop developer directshow dll download dynamic dynamiccharacterarray email encryption error file forms fstream function functions game givemetehcodez google graph gui homeworkhelp iamthwee ifstream input int integer java lib linkedlist linker linux list loop looping loops map math matrix memory multiple news number numbertoword output parameter pointer problem program programming project python random read recursion recursive reference return rpg sorting string strings struct temperature template templates test text text-file tree unix url variable vector video visualstudio win32 windows winsock wordfrequency wxwidgets





