I'm trying to understand how pointers and arrays work to dynamically expand memory during run time. I have this program here to illustrate what i'm talking about. Say the user makes int input = 5
and after this program runs its course sName
and sID
each hold 5 user specified strings. If I wanted to give the user the ability to add more elements after the initial 5, will I have to attempt to create new arrays that will be n+5 (where n is the new number of elements the user wants to add) and then copy the values of sName and sID into those arrays in addition to whatever the user puts in? Or is there a simpler way, other than just using vectors?
#include <iostream>
#include <string>
using namespace std;
int main()
{
int input;
string *sName = NULL;
string *sID = NULL;
string temp, temp2;
cout << "How many names";
cin >> input;
sName = new string[input];
sID = new string[input];
for (int i = 0; i < input; i++)
{
cout << "Enter name ";
cin >> temp;
cout << "enter id ";
cin >> temp2;
*(sName + i) = temp;
*(sID + i) = temp2;
}
return 0;
}