updating user info, jumping into c++, array and pointer

Hi guys! This is a problem from jumping into C++ from pointers chapter. Need to make a program that lets user keep and update the number of days they last met their friends(need to update by user themselves.)
the problem is the next time the user wants to add contact, the same info stored in array[0] gets overwritten in array[1] as well.

here is the partial code. if you have problem in finding error in this, i can post the entire code as well.

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
  
//the add contact function

void add_contact(contact array[],int *p_x)
{


        cout<<"Enter name: ";
        cin>>array[*p_x].name;
        cout<<"Enter number of days you last met "<<array[*p_x].name<<": ";
        cin>>array[*p_x].days;
        (*p_x)++;

}


//now is the int main function

    int x=0;  
    string choice;
    contact array[5];
    do
    {
        cout<<"A to add contact\t U to update contact\t Da to display all contact\nE to exit\tDs to display info of specific contact\nEnter what would you like to do: ";
        cin>>choice;
        if(choice=="A")
        {
            add_contact(array,&x);
        }
        else if(choice=="U")
        {
            update_contact(array);
        }
        //some other if statements;
        }while(choice!="E");
I think its because of this

(*p_x)++;

You increase it in the add_contact function. But only within the function itself. Once you go back to main, and want to add another contact, and you then send it x - add_contact(array,&x);

You're still sending in x as being 0.

Im not 100% sure if this is correct but try it and we'll see :) Try increasing x after you call for the function -

add_contact(array,&x);
x++;

Edit: Yeh ignore everything above, pretty sure its completely wrong.
Last edited on
add_contact(...) should work as expected.

What do you mean by 'gets overwritten'?

I don't know what update_contact(...) does. If it should update a single contact you need to provide an index.
@TarikNeaj I think it with
(*p_x)++;
,
it indeed was updating x, because i too was unsure about this, and had added a 'cout' after 'add_contact' function. each time i called
add_contact
, updated value of x was printed.

@coder777
What do you mean by 'gets overwritten'?

It means that when I added a second contact and printed the info stored(by display_contact function- Da above), both array[0] and array[1] contained the same information which I had entered in array[0].

also I am continuously running into 'program stopped running' problem. i think it could be because of running out of memory even though i think my program is quite small. i use code blocks and windows 8. could be memory leak but am not able to spot it. so am going to post this problem in 'new topic' with the entire code for the same problem so that others users could also possibly help me(with both improving code and spotting memory leak).
the link to new post is :

http://www.cplusplus.com/forum/beginner/159919/

Any help would be highly appreciated!





Topic archived. No new replies allowed.