Deleting a user inputted name from a vector

I have a class person, with a first name, last name...ect. The user is prompted a menu and I am having trouble with the option to delete a name. It seems the string variable is not getting updated when the user enters it and another prob is getting an infinite loop when nothing matches in the vector.

I know what I have now is no good, but I have tried many diff ways and cant come up with a working solution

I would GREATLY, appreciate some help with this.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
if(choice==5){

if(db.size() == 0)
{
std::cout<<"Error, the data base is empty, no persons to erase! "<<std::endl;
}
else{
std::cout<<"Please enter the FIRST name of the person you would like to delete: "<<std::endl;
std::getline(std::cin, nameTdel);
std::cin.get();
//search for name
bool found = false;
if(found == false)
{
for(int i = 0; db.size()>i; i++)
{
 if(nameTdel == db[i].getFname())
  {
   db.erase(db.begin()+i);
   std::cout<<"The person "<<nameTdel<<" has been removed from the data base"<<std::endl;
	found=true;
		}
	}
	std::cout<<"Name not found!"<<std::endl;
}
}
}
1
2
db.erase( std::find_if( db.begin(), db.end(), 
                        [&]( const std::string &s ) { return ( s == nameTdel ) } ); 
Last edited on
Or

1
2
db.erase( std::find_if( db.begin(), db.end(),
                        std::bind2nd( std::equal_to<std::string>(), nameTdel ) );
Last edited on
This is an assignment from an introductory course, we haven't been taught any thing like that. Thanks though.
And what is the problem? Is nameTdel empty?
yeah, every time I go through the debugging, nameTdel has nothing in it even after I enter something
It is because before the statement with getline you used statement with operator >> and the new line character was placed in the input buffer. You should ignore it before using getline.
Topic archived. No new replies allowed.