1,105,333 Community Members

C++ vector object toString

Member Avatar
nullifyQQ
Newbie Poster
5 posts since Jan 2013
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

I have 2 classes.
AddressBook and SingleAddress.
AddressBook is supposed to contain SingleAddress.
I'm omitting the #include and some of the extra functions.

Here's SingleAddress.h

class SingleAddress
{
private:
    string lastName,firstName,strAdd,city,country,email;
    int postCode,homeNum,mobileNum;
public:
    SingleAddress(
    string s1,string s2,string s3,
    string s4,string s5,string s6,
    int i1,int i2,int i3);
    string toString();
};

SingleAddress.cpp

SingleAddress::SingleAddress(string s1,string s2,string s3,string s4,string s5,string s6, int i1,int i2,int i3){
    firstName=s1;
    lastName=s2;
    strAdd=s3;
    city=s4;
    country=s5;
    email=s6;
    postCode=i1;
    homeNum=i2;
    mobileNum=i3;
}

string SingleAddress::toString()
{
    stringstream info;
    //last name, first name, street address, city, country, postal code, home phone number, mobile phone number, email address
    info<<"First name: "  <<firstName
        <<"\nLast name: " <<lastName
        <<"\nStreet address: "<<strAdd
        <<"\nCity: "      <<city
        <<"\nCountry: "       <<country
        <<"\nEmail address: "<<email
        <<"\nPostal code: "<<postCode
        <<"\nHome number: "<<homeNum
        <<"\nMobile number: "<<mobileNum;
    return info.str(); // To convert the above stringstream to string to be displayed out.
}

Here's my AddressBook.h

class AddressBook{
private:
    vector<SingleAddress*>addressBook;
public:
    AddressBook();
    void addAddress(SingleAddress* newAddress);
    void returnListOfAddresses();
};

AddressBook.cpp

AddressBook::AddressBook(){
}

void AddressBook::addAddress(SingleAddress* newAddress){
    addressBook.push_back(newAddress);

And this is where I'm awfully stuck.

void AddressBook::returnListOfAddresses(){

    for(int t=0; t<(int)addressBook.size(); t++){
        cout<<SingleAddress::toString(); //???????
    }
}

int main()

int main(){
        AddressBook*aB=new AddressBook();

        //Create address
        SingleAddress * sA = new SingleAddress(
        "bla","bla","bla","bla","bla","bla",
        1,2,3);

        //Display address
        cout<<sA->toString()<<endl;

        //Add address to AddressBook
        aB->addAddress(sA);

        //Display
        aB->returnListOfAddresses();

return 0;
}

So, the problem is that I don't know how to display all the entries in aB (the AddressBook).
And the question states that my returnListOfAddresses() must not take any input.
I tried adding #include "SingleAddress" and tried to use toString() from the SingleAddress class but it's not working.

I'm at a total loss right now.
Help plz~!!

Member Avatar
NathanOliver
Posting Virtuoso
1,657 posts since Apr 2009
Reputation Points: 284 [?]
Q&As Helped to Solve: 310 [?]
Skill Endorsements: 4 [?]
 
0
 

returnListOfAddresses() should look like this:

for(int t=0; t<(int)addressBook.size(); t++)
{        
    cout<<addressBook[t]->toString();
}
Member Avatar
nullifyQQ
Newbie Poster
5 posts since Jan 2013
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

thanks.
mods plz delete this.

Member Avatar
Nick Evan
Industrious Poster
4,827 posts since Oct 2006
Reputation Points: 4,005 [?]
Q&As Helped to Solve: 560 [?]
Skill Endorsements: 30 [?]
Team Colleague
Featured
 
0
 

mods plz delete this.

No can do.

TOS:

Members may edit their posts for a limited time period immediately after, for the purpose of correcting spelling and grammar mistakes and accidental ommissions. After this initial period expires, posts may only be edited or deleted by DaniWeb team members, and only in cases where they do not comply with our forum rules for the purpose of making said content comply with all rules.

You could delete your profile here

You
This article has been dead for over three months: Start a new discussion instead
Post:
Start New Discussion
View similar articles that have also been tagged: