Hi all I'm new to this forum - Hope you can help me with my problem.
First off all; I apologize for my poor english skills, i'm from sweeden

I'm trying to make a simple phonebook by using a vector of struct's.
As you can see in the code pasted below i'm having a hard time when trying to modify
data in my vector. eg. I want to change the address of Person[0]
Do i need to pass Person[0] from main - or can i just pass "person" and then return the modifed address to person[0] ? - How do i do this ?

Hope it makes sense

Heres what I have so far: (Just working on one element so far, person[0])

struct Book
{
    std::string Name;
    std::string Address;
    int Phone;
};

void InsertData(vector<Book> &Insert)
{
    Book x;
    cout << "Enter name: ";
    getline(cin,x.Name);
    cout << "Enter address: ";
    getline(cin,x.Address);
    cout << "Enter phone number: ";
    cin >> x.Phone;
    Insert.push_back(x);
}

// Print element [0]
void Print(vector<Book> &PrintData)
{ 
    cout << "\nName " << PrintData[0].Name 
         << "\nAddress: " << PrintData[0].Address 
         << "\nPhone: " << PrintData[0].Phone << "\n\n";
}

void ChangeAdr(vector<Book> &InsAdr)
{
    Book x;    
    cin.ignore();
    cout << "Enter new address: ";
    getline(cin,x.Address);
    // This is where I come up short
    // How do I return the new adr. to Person[0]
}

int main(int argc, char *argv[])
{
    vector<Book> Person;

    InsertData(Person);  
    Print(Person);
    ChangeAdr(Person);
    Print(Person);
      
    system("PAUSE");	
    return 0;
}

Thx in Advance

Kind regards Dan

Ancient Dragon commented: Thanks for using code tags correctly :) +25

>>First off all; I apologize for my poor english skills, i'm from sweeden'
You are doing great so don't worry about it.

There are a couple ways to mondify the data in the vector

int main()
{
    vector<Book>  Person;
    Book x;
    cout << "Enter name: ";
    getline(cin,x.Name);
    cout << "Enter address: ";
    getline(cin,x.Address);
    cout << "Enter phone number: ";
    cin >> x.Phone;
    Person.push_back(x);

    // Now change the address
    cout << "Enter address: ";
    getline(cin,Person[0].Address);

    // Another way to change the adress
    vector<Book>::iterator it;
    it = Person.begin();
    cout << "Enter address: ";
    getline(cin, it->Address);



}

Thanks a lot for the quick answer. - works like a charm.

Heres the but...
As you probably guessed i'm following a Book where this is an assignment
one part of it is to be able to change the data in the original vector from a function.
My main problem is to pass one person to the ChangeAdr funtion and return it.
(Gotta read up on pointers ) - Sorry if I didn't make myself clear enough

void ChangeAdr( ????????? )
{
    getline(cin, ????[0].Address); 

    // This is where I come up short
    // How do I return the new adr. to Person[0]
}

Thx alot for the help

Kind Regards Dan

Pass the object by reference so that ChangeAdr() can change the original Book entry. The function is void so it does not return anything.

void ChangeAdr( Book& bk)
{
    getline(cin, bk.Address); 

    // This is where I come up short
    // How do I return the new adr. to Person[0]
//
// You don't -- that's what pass by reference does.
}

int main()
{
     vector<Book> Person;
    <snip>
    ChangeAdr( Person[0] );    
}

Wow - thx for the quick repply - Again, it works great

One last newbie question, and i'll leave you to something else then my beginner problems.

Is pretty clear i've misunderstood pass by reference, so i'm probably passing my vector the wrong way in my other functions aswell; void InsertData(vector<Book> &Insert)
Is there another way (the right way) to do this ?

Kind Regards Dan

>>void InsertData(vector<Book> &Insert)
Nothing wrong with passing the entire array by reference if the function needs it.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.