0

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?

Edited by M4ver1k: Clarification

2
Contributors
2
Replies
3
Views
4 Years
Discussion Span
Last Post by M4ver1k
1

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;
}
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.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.