DanLB 25 Newbie Poster

Hi all - hope you can help me with this.

I'm trying to search through a vector of struct's; I'm able to loop through the vector using a simple for() loop, but i'm unable to use the generic find(). Secondly i want to delete a element
if there's a match.

Below is what I have so far (sorry about the big chunk of code, but i'm a newbie so my problem might be how the vector is passed or something - hence the whole code)

#include <cstdlib>
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>  // For find

using namespace std;
struct Person
{
    std::string Name;
    std::string Address;
    int Phone;
};

void ChangeAdr(string Name, string Adr, vector<Person>& Book);
void Delete(string Name, vector<Person>& Book);

int main(int argc, char *argv[])
{
    vector<Person> Book;
    string Name, Adr;
    
    // Add contact
    Person x;
    x.Name = "James";
    x.Address = "Bond";
    x.Phone = 007;
    Book.push_back(x);

    // Edit address
    cout << "Enter contacts name: ";
    getline(cin, Name);
    cout << "Enter new address: " ;
    getline(cin, Adr);
    ChangeAdr(Name, Adr, Book);
  
    //Delete Contact
    cout << "Enter contacts name: ";
    getline(cin, Name);
    Delete(Name, Book);  

    return 0;
}

void ChangeAdr(string Name, string Adr, vector<Person>& Book)
{
    for (int i=0; i!=Book.size(); i++)
    {
      if (Book[i].Name == Name)
      {
          Book[i].Address = Adr;
          break;
      }    
    }       
}

void Delete(string Name, vector<Person>& Book)
{
    for(vector<Person>::iterator j = Book.begin();  j != Book.end(); ++j)
   {
      if ((*j).Name == Name)
      {
          delete (*j);  // Dosn't work !
          break;
      }
   }
}

The changeAdr() works …

DanLB 25 Newbie Poster

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

DanLB 25 Newbie Poster

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

DanLB 25 Newbie Poster

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 …

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