I am trying to create a C++ program where a user enters his choice if he wishes to add another record, and if yes then create a new object for that record.
So if I am including constructors, then how do I create a new object every time the user wants?
(If I give a predefined size to the array of object, then constructor will be called, say 50 times and initialize all 50 objects, while the user may only want to enter less). Lets just say
class grocerylist
{
float price;
char pname;
public: grocerylist(){.....} //constructor
<some code>
.....
}
main()
{
//grocerylist a[50]; this will call constructor 50 times! which is not desired
}
My guess here is to use the new operator inside a loop, which will break when user doesnt want to enter any more records.But the problem is, it's scope will be destroyed once it comes out of a loop.
std::vectorin your favorite C++ textbook.grocerylist *glist = new grocerylist();grocerylist glist;.