returning pointers from an array of structs

Hi,

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);

Hope someone can help...

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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
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;
}
Topic archived. No new replies allowed.