I've attached below my codes. I'm stumped. The funny thing is, I think I can make this work with a vector instead of the arrayListType class. I cannot figure out how to load the objects into the array.
I create an arrayListType object of type extPersonType, call it addressBookType, and make its size 500. So I should have an array that is capable of holding 500 extPersonType objects.
I want to load each element of the array with an extPersonType object using the default constructor. I could then iterate through the array a second time to set the first name, last name, street, state, zip, etc. of each of the elements in the array. Or, it would be even nicer if I could use the overloaded constructor and create the objects along with their attributes in the same 'for' loop.
(FYI, the "prog.dat" file is just a text file that contains the data to load the object attributes.)
Thanks in advance,
Dani
File 1
/*
* File: main.cpp
*/
#include <cstdlib>
#include <iostream>
#include <string>
#include <fstream>
#include "arrayListType.h"
#include "extPersonType.h"
using namespace std;
/*
*
*/
int main(){
//variable declarations
ifstream inData;
int arraySize; //size of list
string first, last, streetname, cityname, statename, zipcodename, relate, phone;
int month, day, year;
//user determines address book size
cout << "Please enter the number of entries you want in the address book (max of 500): ";
cin >> arraySize;
//open data file
inData.open("prog.dat"); //opens the input file
//initialize address book capable of holding 500 entries
arrayListType<extPersonType> addressBookType(arraySize);
//create address book entry objects and load list
for(int i = 0; i < arraySize; i++){
inData >> first >> last >> streetname >> cityname >> statename >> zipcodename >> month >> day >> year >> relate >> phone;
//addressBookType.insert(extPersonType(first, last, streetname, cityname, statename, zipcodename, month, day, year, relate, phone));
extPersonType *Entry;
Entry = new extPersonType[i];
addressBookType.insertAt(i, Entry/*what do I put here to load the array with empty objects?*/);
}
//close data file
inData.close();
return 0;
}
File 2 (Error file)
Error file for Ch3, problem 9 homework exercise.
"/usr/bin/make" -f nbproject/Makefile-Debug.mk QMAKE= SUBPROJECTS= .build-conf
make[1]: Entering directory `/cygdrive/c/Users/Kevin/Documents/NetBeansProjects/CS270_Ch3_Prob9'
"/usr/bin/make" -f nbproject/Makefile-Debug.mk dist/Debug/Cygwin-Windows/cs270_ch3_prob9.exe
make[2]: Entering directory `/cygdrive/c/Users/Kevin/Documents/NetBeansProjects/CS270_Ch3_Prob9'
mkdir -p build/Debug/Cygwin-Windows
rm -f build/Debug/Cygwin-Windows/main.o.d
g++ -c -g -MMD -MP -MF build/Debug/Cygwin-Windows/main.o.d -o build/Debug/Cygwin-Windows/
main.o main.cpp
main.cpp: In function `int main()':
main.cpp:47: error: no matching function for call to `arrayListType<extPersonType>::insertAt(int&,
extPersonType*&)'
arrayListType.h:42: note: candidates are: void arrayListType<elemType>::insertAt(int, const elemType&)
[with elemType = extPersonType]
make[2]: *** [build/Debug/Cygwin-Windows/main.o] Error 1
make[1]: *** [.build-conf] Error 2
make: *** [.build-impl] Error 2
nbproject/Makefile-Debug.mk:68: recipe for target `build/Debug/Cygwin-Windows/main.o' failed
make[2]: Leaving directory `/cygdrive/c/Users/Kevin/Documents/NetBeansProjects/CS270_Ch3_Prob9'
nbproject/Makefile-Debug.mk:61: recipe for target `.build-conf' failed
make[1]: Leaving directory `/cygdrive/c/Users/Kevin/Documents/NetBeansProjects/CS270_Ch3_Prob9'
nbproject/Makefile-impl.mk:39: recipe for target `.build-impl' failed
BUILD FAILED (exit value 2, total time: 999ms)
File 3 (Progr.dat)
Kevin Edgington
1905 Carlyle Dr
Las Cruces
NM 88005 11 12 1971 family 680-1801
Gisela Edgington
1905 Carlyle Dr
Las Cruces
NM 88005 11 12 1971 family 680-1801
Joshua Kern
123 Hemming St
Deming
NM 83456 5 22 1978 business 456-7890
Frank Sanchez
5489 Midway Ave
Albuquerque
NM 88034 12 14 1953 friend 555-5555
Albert Benevidez
PO Box 2345
Santa Fe
NM 88456 7 14 1968 friend 555-5678
Brenda Smith
890 South Rd
El Paso
TX 45678 8 9 2000 family 647-9876
Howard Smith
342 South Rd
El Paso
TX 45678 3 23 1989 friend 647-4567
Anna Klein
234 Broadway Ave
Bermingham
SC 11111 1 23 1987 business 889-9999
Alexis Robinson
333 Sexton St
Atlanta
GA 34343 5 15 1990 business 987-6543
Bob Goodyear
1123 Tread Ave
Anchorage
AK 87898 11 23 1976 family 888-6666
Francis Jones
346 Main St
Dallas
TX 44555 9 18 2000 family 575-6767
Zach Taylor
5438 Jupiter
Denver
CO 90089 12 19 1995 friend 666-6666
Gary Bales
456 Bales St
Salt Lake City
UT 88778 4 19 1945 business 774-9863
Fred Atkinson
4890 Backway Blvd
Columbus
OH 66848 6 16 1966 business 456-9837
Brian Collier
435 Roadrunner Pkwy #16
Lackluster
RI 85921 5 28 1949 business 670-4569
Cliff Baxter
444 Sanchez St
Vado
NV 44444 7 18 1998 friend 456-2345
Phil Silano
555 Michigan Ave
Lake Havasu
NV 44554 8 30 2001 family 666-9999
Keith Vick
678 Via Del Toro
Lordsburg
NM 98989 9 10 1969 friend 456-4534
John Jacob
459 Jingleheimer Rd
Schmidt
WV 88488 3 24 1999 business 938-0987
Al Estes
1421 S Vickers St
vladamir
MA 72974 2 28 1996 friend 883-8274
Frank Unpingco
889 Los Avalos
Roswell
NM 88905 10 18 1999 business 884-2859
Tony Spence
898 Perpetuation
Flagstaff
AZ 99999 9 30 1934 family 849-3495
Angela Brooks
989 Mt Holyoak Rd
Fairview
MO 55768 1 27 1947 family 874-8838
Renee Crumree
8467 S Peachtree Rd
Franklin
MN 34523 4 25 1980 business 777-4939
Jamie Ferrero
8484 Alabaster Ave
Great Falls
MO 84593 6 16 1942 friend 354-4563
Melinda Refford
456 Bay Ave
Coral Springs
FL 89361 1 31 2009 family 576-5768
Carly Hildebrandt
9939 17th St
St. Louis
MN 98765 3 27 1969 friend 123-7648
File 4
/*
* File: arrayListType.cpp
*
* Created on March 30, 2014, 11:05 AM
*/
#include "arrayListType.h"
//determine whether the array is empty
template <class elemType> bool arrayListType<elemType>::isEmpty() const{
return (length == 0);
}
//determine whether the array is full
template <class elemType> bool arrayListType<elemType>::isFull() const{
return (length == maxSize);
}
//determine the number of elements in the list
template <class elemType> int arrayListType<elemType>::listSize() const{
return length;
}
//determine the size of the list
template <class elemType> int arrayListType<elemType>::maxListSize() const{
return maxSize;
}
//output elements of the list
template <class elemType> void arrayListType<elemType>::print() const{
for (int i = 0; i < length; i++){
cout << list[i] << " ";
}
cout << endl;
}
//determine whether two items in the list are the same
template <class elemType> bool arrayListType<elemType>::isItemAtEqual(int location, const elemType& item) const{
return(list[location] == item);
}
//inserts and item into the list
template <class elemType> void arrayListType<elemType>::insertAt(int location, const elemType& insertItem){
if (location < 0 || location >= maxSize)
cerr << "The position of the item to be inserted is out of range" << endl;
else
if(length >= maxSize)
cerr << "Cannot insert in full list" << endl;
else
{
for (int i = length; i > location; i--)
list[location] = insertItem;
length++;
}
}
//inserts an item at the end of the list
template <class elemType> void arrayListType<elemType>::insertEnd(const elemType& insertItem){
if(length >= maxSize)
cerr << "Cannot insert in a full list" << endl;
else
{
list[length] = insertItem;
length++;
}
}
//remove item from list at specified location
template <class elemType> void arrayListType<elemType>::removeAt(int location){
if(location < 0 || location >= length)
cerr << "The location of the item to be removed is out of range" << endl;
else
{
for (int i = location; i < length - 1; i++)
list[i] = list[i + 1];
length--;
}
}
//retrieve element from list at specified location
template <class elemType> void arrayListType<elemType>::retrieveAt(int location, elemType& retItem) const{
if(location < 0 || location >= length)
cerr << "The location of the item to be retrieved is out of range" << endl;
else
retItem = list[location];
}
//replace element in list at specified location
template <class elemType> void arrayListType<elemType>::replaceAt(int location, const elemType& repItem){
if (location < 0 || location >= length)
cerr << "The location of the item to be replaced is out of range" << endl;
else
list[location] = repItem;
}
//remove all elements from the list
template <class elemType> void arrayListType<elemType>::clearList(){
length = 0;
}
//constructor
template <class elemType> arrayListType<elemType>::arrayListType(int size){
if (size < 0){
cerr << "The array size must be positive. Creating an array of size 500 " << endl;
maxSize = 500;
}
else
maxSize = size;
length = 0;
list = new elemType(maxSize);
assert(list != NULL);
}
//destructor
template <class elemType> arrayListType<elemType>::~arrayListType(){
delete [] list;
}
//copy constructor
template <class elemType> arrayListType<elemType>::arrayListType(const arrayListType<elemType>& otherList){
maxSize = otherList.maxSize;
length = otherList.length;
list = new elemType[maxSize];
assert(list != NULL);
for (int j = 0; j < length; j++)
list [j] = otherList.list[j];
}
template <class elemType> const arrayListType<elemType>& arrayListType<elemType>::operator=(const arrayListType<elemType>& otherList){
if (this != &otherList){
delete [] list;
maxSize = otherList.maxSize;
length = otherList.length;
list = new elemType[maxSize];
assert(list != NULL);
for(int i = 0; i < length; i++)
list[i] = otherList.list[i];
}
return *this;
}
//search list for a given item
template <class elemType> int arrayListType<elemType>::seqSearch(const elemType& item) const{
int loc;
bool found = false;
for (loc = 0; loc < length; loc++)
if (list[loc] == item){
found = true;
break;
}
if (found)
return loc;
else
return -1;
}
//insert item specified by the parameter
template <class elemType> void arrayListType<elemType>::insert(const elemType& insertItem){
int loc;
if (length == 0)
list[length++] = insertItem;
else if (length == maxSize)
cerr << "Cannot insert in a full list." << endl;
else{
loc = seqSearch(insertItem);
if (loc == -1)
list[length++] = insertItem;
else
cerr << "the item to be inserted is already in the list. No duplicates are allowed." << endl;
}
}
//remove item from list...removeItem specifies the item
template <class elemType> void arrayListType<elemType>::remove(const elemType& removeItem){
int loc;
if(length == 0)
cerr << "Cannot delete from an empty list." << endl;
else{
loc = seqSearch(removeItem);
if (loc != -1)
removeAt(loc);
else
cout << "The item to be deleted is not in the list." << endl;
}
}
File 5
/*
* File: arrayListType.h
*
* Created on March 30, 2014, 11:05 AM
*/
#include<cstdlib>
#include<iostream>
#include<list>
using namespace std;
#ifndef ARRAYLISTTYPE_H
#define ARRAYLISTTYPE_H
template <class elemType>
class arrayListType {
public:
//overload assignment operator
const arrayListType<elemType>& operator=(const arrayListType<elemType>& otherList);
//determine whether the list is empty
bool isEmpty() const;
//determine whether the list is full
bool isFull() const;
//determine the number of elements in the list
int listSize() const;
//determine the size of the list
int maxListSize() const;
//output elements of the list
void print() const;
//determine whether two items in the list are the same
bool isItemAtEqual(int location, const elemType& item) const;
//inserts and item into the list
void insertAt(int location, const elemType& insertItem);
//inserts and item at the end of the list
void insertEnd(const elemType& insertItem);
//remove item from list at specified location
void removeAt(int location);
//retrieve element from list at specified location
void retrieveAt(int location, elemType& retItem) const;
//replace element in list at specified location
void replaceAt(int location, const elemType& repItem);
//remove all elements from the list
void clearList();
//search list for a given item
int seqSearch(const elemType& item) const;
//insert item specified by the parameter
void insert(const elemType& insertItem);
//remove item from list...removeItem specifies the item
void remove(const elemType& removeItem);
//constructor
arrayListType(int size = 500);
//copy constructor
arrayListType(const arrayListType<elemType>& otherlist);
//destructor
~arrayListType();
protected:
elemType *list; //array to hold the list elements
int length; //to store the length of the list
int maxSize; //to store the maximum size of the list
};
#endif /* ARRAYLISTTYPE_H */
File 6
/*
* File: extPersonType.cpp
*
* Created on March 30, 2014, 10:18 AM
*/
#include "extPersonType.h"
//CONSTRUCTORS------------------------------------------------------------------
//default constructor
extPersonType::extPersonType() {
firstName = "";
lastName = "";
street = "";
city = "";
state = "";
zipcode = "";
dMonth = 0;
dDay = 0;
dYear = 0;
relationshipType = "";
phoneNumber = "";
}
//overloaded constructor
extPersonType::extPersonType(string first, string last, string streetname, string cityname, string statename, string zipcodename, int month, int day, int year, string relate, string phone){
firstName = first;
lastName = last;
street = streetname;
city = cityname;
state = statename;
zipcode = zipcodename;
dMonth = month;
dDay = day;
dYear = year;
relationshipType = relate;
phoneNumber = phone;
if(isValid(month, day, year))
setDate(month, day, year);
}
//PRINT FUNCTION----------------------------------------------------------------
//Prints an entry from the address book
void extPersonType::printAnEntry(){
cout << firstName << " " << lastName << endl;
cout << street << endl;
cout << city << ", " << state << " " << zipcode << endl;
}
//MUTATORS----------------------------------------------------------------------
//sets person's first and last name
void extPersonType::setName(string first, string last){
firstName = first;
lastName = last;
}
//sets street address
void extPersonType::setStreet(string streetname){
street = streetname;
}
//sets city name
void extPersonType::setCity(string cityname){
city = cityname;
}
//sets state name
void extPersonType::setState(string statename){
state = statename;
}
//sets zip code
void extPersonType::setZipcode(string zipcodename){
zipcode = zipcodename;
}
//set birth date
void extPersonType::setDate(int month, int day, int year){
dMonth = month;
dDay = day;
dYear = year;
}
//set relationship type
void extPersonType::setRelation(string relate){
relationshipType = relate;
}
//set phone number
void extPersonType::setPhoneNumber(string phone){
phoneNumber = phone;
}
//ACCESSORS---------------------------------------------------------------------
//retrieves person's first name
string extPersonType::getFirstName() const{
return firstName;
}
//retrieves person's last name
string extPersonType::getLastName() const{
return lastName;
}
//retrieves street name
string extPersonType::getStreet(){
return street;
}
//retrieves city name
string extPersonType::getCity(){
return city;
}
//retrieves state name
string extPersonType::getState(){
return state;
}
//retrieves zip code
string extPersonType::getZipcode(){
return zipcode;
}
//return day of birthday
int extPersonType::getDay() const{
return dDay;
}
//return month of birthday
int extPersonType::getMonth() const{
return dMonth;
}
//return year of birthday
int extPersonType::getYear() const{
return dYear;
}
//retrieve the relationship type
string extPersonType::getRelation(){
return relationshipType;
}
//retrieve the phone number
string extPersonType::getPhoneNumber(){
return phoneNumber;
}
//BIRTHDAY DATE VALIDATORS------------------------------------------------------
//perform validation check on birth date
bool extPersonType::isValid(int month, int day, int year) const{
if(month <= 0 || month > 12 || day <= 0 || day > 31 || year <= 0)
return false;
else if (!isLeapYear(year) && month == 2 && day < 28)
return false;
else
return true;
}
//check for leap year
bool extPersonType::isLeapYear(int year)const{
if ( (year % 4 == 0 && year % 100 != 0) || ( year % 400 == 0))
return true;
else
return false;
}
File 7
/*
* File: extPersonType.h
*
* Created on March 30, 2014, 10:18 AM
*/
#include<string>
#include<iostream>
using namespace std;
#ifndef EXTPERSONTYPE_H
#define EXTPERSONTYPE_H
class extPersonType{
public:
//default constructor
extPersonType();
//constructor with parameters
extPersonType(string first, string last, string streetname, string cityname, string statename, string zipcodename, int month, int day, int year, string relate, string phone);
//PRINT FUNCTION
//print function
void printAnEntry();
//MUTATORS
//sets person's first and last name
void setName(string first, string last);
//set street name
void setStreet(string streetname);
//set city name
void setCity(string cityname);
//set state name
void setState(string statename);
//set zip code
void setZipcode(string zipcodename);
//function to set the birth date
void setDate(int month, int day, int year);
//function to set relationship type
void setRelation(string relate);
//function to set phone number
void setPhoneNumber(string phone);
//ACCESSORS
//retrieves person's first name
string getFirstName() const;
//retrieves person's last name
string getLastName() const;
//retrieve street name
string getStreet();
//retrieve city name
string getCity();
//retrieve state name
string getState();
//retrieve zip code
string getZipcode();
//function to return the day
int getDay() const;
//function to return the month
int getMonth() const;
//function to return the year
int getYear() const;
//function to retrieve the relationship type
string getRelation();
//function to retrieve the phone number
string getPhoneNumber();
//BIRTHDAY DATE VALIDATIONS
//function to check for leap year
bool isLeapYear(int year) const;
//function to check for date validity
bool isValid(int month, int day, int year) const;
private:
string firstName; //person's first name
string lastName; //person's last name
string street; //person's street address
string city; //person's city of residence
string state; //person's state of residence
string zipcode; //person's zip code
int dMonth; //variable to store month
int dDay; //variable to store day
int dYear; //variable to store year
int daysInMonth[]; //array to hold number of days in each month
int daysInMonthLeap[]; //array to hold number of days in each month during a leap year
string relationshipType;//friend, family, or business associate
string phoneNumber; //contacts phone number
};
#endif /* EXTPERSONTYPE_H */