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.
//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);
}
elseif(choice=="U")
{
update_contact(array);
}
//some other if statements;
}while(choice!="E");
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.
,
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 :