Create an array that holds struct objects C++ -


i novice programmer , creating program holds several objects of struct type. program needs accept user input don't know how it. first, here's code i'm using define struct:

struct apartment{         int number;         string owner;         string condition; }ap; 

and here's code i'm using ask user input:

cout << "enter apartment number: " << endl; cin >> ap.number; cout << "enter name of owner: " << endl; cin >> ap.owner; cout << "enter condition: " << endl; cin >> ap.condition;  apartment building[50] = { ap.number, ap.owner, ap.condition}; 

the last line of code how trying save object in array, bu don't know if works. later need print objects, nice if helped me too. using visual studio 2013 compiler, in case makes difference.

struct apartment{     int number;     string owner;     string condition; }ap;  void addapartment(vector<apartment> &vtnew) {     apartment building;     cout << "enter apartment number: " << endl;     cin >> building.number;     cout << "enter name of owner: " << endl;     cin >> building.owner;     cout << "enter condition: " << endl;     cin >> building.condition;     vtnew.push_back(building); }  void displayapt(vector<apartment> vtnew) {     vector<apartment> ::iterator it;     (it = vtnew.begin(); != vtnew.end(); it++)     {         cout << "apartment number" <<it->number;         cout << "name of owner" << it->owner;         cout << "condition" << it->condition;     } }  int main() {     vector<apartment> vt;     addapartment(vt);     addapartment(vt);     addapartment(vt);     displayapt(vt);     return 0; } 

what doing in code declare array apartment hold 50 apartments, when using vectors need not give count beforehand, can keep adding apartments, without worrying size!


Comments

Popular posts from this blog

dns - How To Use Custom Nameserver On Free Cloudflare? -

python - Pygame screen.blit not working -

c# - Web API response xml language -