add new element without loosing existing data

Nov 22, 2010 at 6:42pm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#include <cstdlib>
#include <iostream>
#include <string>
#include <sstream>

using namespace std;
struct book
        {
		string title;
		int price;
	};

int main()
{
        string answer;
	book *bigbook;
	bigbook = new book[1];
	
	bigbook[0].title="harry potter";
	bigbook[0].price= 300000;
	
	cout<<endl<<"book no 0 is : "<<bigbook[0].title;
	cout<<endl<<"do you want to add a new book ? (y/n)";
	cin>>answer;
	if(answer=="y")
	{
		//increase the bigbook array but dont loose en existing data
		//its mean how to create bigbook[1] without loosing bigbook[0]
	}
	else
	{
		cout<<endl<<"see you later";
	}
	cout<<endl<<endl;
	system("PAUSE");
	return 0;
}


in this case, bigbook will grows to unexpected number of element, my froblem is how to add new book without loosing existing data in previous element. thanks for help
Nov 22, 2010 at 6:45pm
Same answer as your other post. Use a vector instead of an array. Vectors can grow and shrink.
Last edited on Nov 22, 2010 at 6:46pm
Nov 22, 2010 at 6:52pm
thanks, ill find out....
Topic archived. No new replies allowed.