I have a vector filled with names such as
John Doe
Jane Doa
etc.

I need to write a function which deletes what is input via the user

void delete(vector<string>&name)
{
	string FName, LName;

	cout<<"Enter first name and last name to be deleted";
	cin>>FName>>LName;
}

ie input: John Doe
and I want John Doe deleted from the vector

Any help would be appreciated I have been lost for a while now :/

Recommended Answers

All 10 Replies

I suggest you use getline() to get input from the user. Getline takes in input until a newline (enter) is detected. This is handy in you case, because you need 2 words (first and last name).

To erase the element from the vector, you can use the erase method from the vector class. The only thing you need to know, is which element to delete. This can be found with the std::find function from <algorithm>

Example:

#include <vector>
#include <iostream>
#include <string>
#include <algorithm>


int main(){
    std::vector<std::string> names;
    names.push_back("John Doe");
    names.push_back("Jane Doa");

    std::string to_delete ="";
    std::cout << "Enter name to delete (case sensitive):\n";
    std::getline(std::cin, to_delete);

    std::vector<std::string>::iterator index = std::find(names.begin(), names.end(),to_delete);
    if (index != names.end()){
        std::cout << "Deleted: " << to_delete << '\n';
        names.erase(index);
    } else {
        std::cout << "not found\n";
    }
    std::cout << names.size() << " users remain in vector\n";
    return 0;
}

well I am using the function in a forward declaration and when it goes to the getline part it just skips the input and responds with not found. Doesn't give me a chance to give any input.

edit: nevermind it is something with my overall code, guess I will have to review it.

So I added some more code and now I am getting the above problem in which I don't get a chance to enter input.

Here is my code:

#include <vector>
#include <iostream>
#include <string>
#include <algorithm>

using namespace std;

void delete_name(vector<string>&);
void display_all(vector<string>);
 
int main()
{
    char cInput;
	vector<string> name;
  	name.push_back("John Doe");
  	name.push_back("Jane Doa");
	name.push_back("George Bush");
	name.push_back("Bill Gates");
	name.push_back("Bill Clinton");
 
while(true)
	{
		cout<<" Enter 1 - display names"<<endl;
		cout<<" Enter 2 - delete name"<<endl;
		cout<<"Enter number: ";
		cin>>cInput;
		switch(cInput)
		{
		case '1':
			display_all(name);
			break;
		case '2':
			delete_name(name);
			break;
		default:
			cout<<"invalid input"<<endl;
			break;
		}
	}

    return 0;
}

void delete_name(vector<string>&name)
{
	string to_delete = "";
	cout<<"Enter name to delete (First and Last Name)";
	::getline(cin, to_delete);

	vector<string>::iterator index = ::find(name.begin(), name.end(), to_delete);
	if (index != name.end()){
        cout << "Deleted: " << to_delete << '\n';
        name.erase(index);
    } else {
        cout << "not found\n";
    }
}

void display_all(vector<string>name)
{
	int i=0;
	cout<<"Display ALL student names.."<<endl;
	for (i=0; i<name.size(); i++)
		cout<<name[i]<<endl;
}

Put a cin.ignore() after every cin >> ..... .
The reason you don't get a change to input anything is because "cin >>" leaves a '\n' on the input stream. cin.ignore() will remove it.

Thanks for the help and quick reply niek_e, you're making c++ even more enjoyable than it was before to me.

While I am here, I'd rather not clutter the forums with another thread, wondering if there was a simple way to modify a vector element in kind of the same way.

ie:
cin>>Jane Doa;
cout<<"Enter modifications";
cin>>Jane Doe

And again thanks for your help :D

What I have thusfar

void modify_name(vector<string>names)
{
	string to_modify = ""
	cout<<"Enter name to modify (case sensitive)"<<endl;
	getline(cin.ignore(), to_modify);
}
{
	string to_modify = "";
	string new_modify = "";
	cout<<"Enter the name to be Modified (First and Last Name): "<<endl;
	getline(cin.ignore(), to_modify);

	vector<string>::iterator index = ::find(vecThisStudent.begin(), vecThisStudent.end(), to_modify);
	if (index != vecThisStudent.end()){
        vecThisStudent.erase(index);
		cout<<"Enter the new name (First and Last Name): "<<endl;
		getline(cin.ignore(), new_modify);
		vecThisStudent.push_back( new_modify);
		cout << "---- Name " << to_modify << " has been modified as "<< new_modify << '\n';
    } else {
        cout << "The name is Not Found. No Action is taken.\n";
    }
}

Problem is now when the new entry is being pushed_back the first letter entered is missed.
If I enter John Doe, the entry is ohn Doe

I think you misunderstood me. Change all the getline(cin.ignore(), new_modify); back to getline(cin, new_modify); What I meant was this:

int i =0 ;
string something = "";
cin >> i;
cin.ignore(); // is required here to remove \n from stream
getline (cin, something); // noproblem, everything reads fine

or

int i =0 ;
string something = "";
string else = "";
// we didn't use unformatted input ( >>) so no need for an ignore()
getline (cin, something); // noproblem, everything reads fine

getline (cin, else); // noproblem, everything reads fine

Ah thanks but I'm still having the missing letter problem.
in: John Doe
vector: ohn Doe

In your while(ture) statement where you ask the user to to select an option you have

cin>>cInput;

immediately proceeding that put cin.ignore() to remove the '\n' which is always left in the input stream following the use of cin>>.

So it should look like this.

cin>>cInput;
cin.ignore();

so from getline(cin.ignore(), to_modify) remove the .ignore() and simply have getline(cin, to modify).

Thanks Grub, worked like a charm.

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.