I write a program using cpp to read strings with cin and save them in an allocated memory. An extra work I need to do is dealing with the circumstance when the size of the input is more than expected. When I testing the code, it doesn't display the content of the final memory saves, cannot terminate automatically. Here is the code.
#include <iostream>
#include <memory>
using namespace std;
int main(){
allocator<string> sa;
cout << "Please input the amount of words" << endl;
int count;
cin >> count;
auto p = sa.allocate(count);
cout << "Please input the text" << endl;
string s;
auto q = p;
while(cin >> s){
if (q == p + count) {
auto p2 = sa.allocate(count * 2);
auto q2 = uninitialized_copy_n(p, count, p2);
while (q != p) {
sa.destroy(--q);
}
sa.deallocate(p, count);
p = p2;
q = q2;
count *= 2;
}
sa.construct(q++, s);
}
for (auto pr = p; pr != q; ++pr) {
cout << *pr << " ";
}
cout << endl;
while (q != p) {
sa.destroy(--q);
}
sa.deallocate(p, count);
return 0;
}