I am trying to make an array of structs that are able to change size at runtime according to user input. When I run the program it stalls and I don't know how to fix it...
I believe the problem lies in the function:
struct user *growarray(struct user* p_array,int *size);
struct user{
int days; // days since you last talked with the person.
string name;
};
int size = 2;
user *users = new user[size];
int next_element=0;
string newname;
int day;
struct user *growarray(struct user* p_array,int *size);
void addusers(){
while(next_element >= 0){
cout << "Please enter name or 0 to exit: ";
cin >> newname;
if(newname == "0"){
break;
}
cout << "Please enter number of days since last contact: ";
cin >> day;
if(size == next_element + 1){
users = growarray(users,& size);
}
users[next_element].days = day;
users[next_element].name = newname;
next_element++;
}
}
struct user *growarray (struct user *p_values, int *size)
{
*size *= 2;
user *p_new_values = new user[ *size ];
for ( int i = 0; i < *size; ++i )
{
p_new_values[ i ].name = p_values[ i ].name;
p_new_values[i].days = p_values[ i ].days;
}
delete [] p_values;
return p_new_values;
}
int main()
{
int next_elt = 0;
addusers();
return 0;
}