So I want to use the vector::erase command to my remove contacts function, but I am kind of new to vectors and I need help understanding exactly how to do this.
I get if you already have a SET amount of vectors,
// erase the first 3 elements:
myvector.erase (myvector.begin(),myvector.begin()+3)
like so, but what if You want it to display the names to erase like my code below in the remove contacts function? What if I didn't have 3 elements in the vector? How do you give the user a option what vector to choose.
also, I am getting errors when I try to assign an ID to a vector element in addContact function.
#include<iostream>
#include<string>
#include<vector>
usingnamespace std;
vector<string>Names;
vector<string>Phones;
vector<string>Addresses;
void addContact();
void searchByID();
void searchByName();
void removeContact();
int main()
{
bool inMenu = true;
int sel;
while (inMenu) {
cout << "Chose a Number to Execute the Selected Option: " << endl;
cout << "[1]New Contact" << endl;
cout << "[2]Search By Name" << endl;
cout << "[3]Remove Contact" << endl;
cout << "[4] Exit PhoneBook" << endl;
cout << "Your choice: " << endl;
cin >> sel;
switch (sel)
{
case 1:
addContact();
break;
case 2:
searchByName();
break;
case 3:
removeContact();
break;
case 4:
inMenu = false;
break;
default:
cout << "Does not recognice Selection";
break;
} // end of switch
} // end of menu
return 0;
} // end of main
void addContact()
{
cin.clear();
cin.sync();
string name;
string phone;
cout << "Enter Contact Name: ";
cin >> name;
cout << "Enter Contact Phone Number: ";
cin >> phone;
Names.push_back(name);
Phones.push_back(phone);
}
void searchByID()
{
int value;
cout << "Enter your Contact’s ID that you need to search: ";
cin >> value;
if (value > Names.size())
{
cout << "This ID does not exist";
return; //terminates it
}
cout << "--Here is a little information on the contact ID--";
cout << "Name: " << Names[value] << endl; //index is this and this is used to differentiate the values within the vector
cout << "Phone Number: " << Phones[value] << endl;
}
void removeContact()
{
}
void searchByName()
{
bool found = false; //bool is a data type that is only true or false, so this will tell us if the search was either successful or failed
string name;
cout << "Enter the Contact Name to search: ";
cin >> name;
for (int i = 0; i != Names.size(); i++)
{
if (Names[i] == name)
{
cout << "Name:" << Names[i] << endl;
cout << "Phone:" << Phones[i] << endl;
found = true;
}
}
if (!found)
{
cout << "Contact was not found" << endl;
}
}
void removeContact()
{
bool found = false; //bool is a data type that is only true or false, so this will tell us if the search was either successful or failed
string name;
cout << "Enter the Contact Name to delete: ";
cin >> name;
for (int i = 0; i != Names.size(); i++)
{
if (Names[i] == name)
{
Names[i].erase << endl;
Phones[i].erase << endl;
found = true;
cout << "Contact Erased";
}
}
if (!found)
{
cout << "Contact was not found" << endl;
}
}
The erase function of a vector takes in an iterator. Phones.begin() is an
example of an iterator. Adding a number to Phones.begin() will also get you an
iterator.
1 2 3 4 5 6 7 8 9 10 11 12
for (size_t i = 0; i != Names.size(); i++)
{
if (Names[i] == name)
{
Names.erase(Names.begin()+i);
Phones.erase(Phones.begin()+i);
found = true;
cout << "Contact Erased";
break; // Must break out of loop if found
}
}
One bug fix to fiji885's solution: for (size_t i = 0; i < Names.size(); i++) // change != to <
Without this the loop will continue if you delete the last item.
In the original program:
Line 79 should be if (value >= Names.size())
Have you learned about classes and structures? If so then it would be better to put all info about a person in a Person struct and then have a single vector:
1 2 3 4 5
struct Person {
string name, phone;
};
vector<Person> people;
Also, a couple utility functions will let you remove redundant code. See showContact() and findContact() below.
#include<iostream>
#include<string>
#include<vector>
usingnamespace std;
vector < string > Names;
vector < string > Phones;
vector < string > Addresses;
void addContact();
void searchByID();
void searchByName();
void removeContact();
int
main()
{
bool inMenu = true;
int sel;
while (inMenu) {
cout << "Chose a Number to Execute the Selected Option: " << endl;
cout << "[1]New Contact" << endl;
cout << "[2]Search By Name" << endl;
cout << "[3]Remove Contact" << endl;
cout << "[4] Exit PhoneBook" << endl;
cout << "Your choice: " << endl;
cin >> sel;
switch (sel) {
case 1:
addContact();
break;
case 2:
searchByName();
break;
case 3:
removeContact();
break;
case 4:
inMenu = false;
break;
default:
cout << "Does not recognice Selection";
break;
} // end of switch
} // end of menu
return 0;
} // end of main
// Prompt for a name and search for it in the Names vector. Return
// The index if found, or Names.size() if not
unsigned
findByName()
{
string name;
cout << "Enter the Contact Name to search: ";
cin >> name;
unsigned i;
for (i = 0; i != Names.size(); i++) {
if (Names[i] == name) {
break;
}
}
return i;
}
// Given an index, display info about a contact
void
showContact(unsigned idx)
{
cout << "Name:" << Names[idx] << endl;
cout << "Phone:" << Phones[idx] << endl;
}
void
addContact()
{
cin.clear();
cin.sync();
string name;
string phone;
cout << "Enter Contact Name: ";
cin >> name;
cout << "Enter Contact Phone Number: ";
cin >> phone;
Names.push_back(name);
Phones.push_back(phone);
}
void
searchByID()
{
unsigned value;
cout << "Enter your Contact’s ID that you need to search: ";
cin >> value;
if (value >= Names.size()) {
cout << "This ID does not exist";
return; //terminates it
}
cout << "--Here is a little information on the contact ID--";
showContact(value);
}
void
removeContact()
{
unsigned i = findByName();
if (i < Names.size()) {
Names.erase(Names.begin() + i);
Phones.erase(Phones.begin() + i);
cout << "Contact Erased\n";
} else {
cout << "Contact was not found\n";
}
}
void
searchByName()
{
unsigned i = findByName();
if (i < Names.size()) {
showContact(i);
} else {
cout << "Contact was not found" << endl;
}
}