Forgive me if the title was even worded properly. What I'm trying to accomplish is to be able to create an unlimited number of objects for use by struct and quickly realizing that my thought process on how to achieve that is wrong. Here's the related code:

struct person {
        string first;
        string last;
        string addr;
        string phone;
};

int main (){
        vector<int> pnum;

        tmp = pnum.size();
        pnum.push_back(tmp);
        AddPerson(&pnum[tmp]);  // <-- This has a problem in compiling
}

It feels like a vector/array is probably not what I should be doing, is there a smarter way to go about creating dynamic objects that I can group or manage logically?

Recommended Answers

All 2 Replies

Something like this?

struct person {
        string first;
        string last;
        string addr;
        string phone;
};

int main ()
{
    vector<person> pvec;
    person tp;

    tp.first = "Rhino";
    tp.last = "Neil";
    tp.addr = "22 Acacia Avenue";
    tp.phone = "5554646";

    pvec.push_back(tp);

    tp.first = "Orson";
    tp.last = "Cart";
    tp.addr = "Some Place, Unknown";
    tp.phone = "5551212";

    pvec.push_back(tp);

    for (person p : pvec)
        cout << p.first << " " << p.last << " - " << p.addr << " - " << p.phone << endl;

    cout << endl << "press Enter to exit...";

    cin.get();

    return 0;
}

This looks like what I'm probably after. Thank you so much, I'm going to break down what you have so that I can commit it to memory. You rock.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.