Deleting a user inputted name from a vector

Aug 31, 2013 at 10:47pm
closed account (2604izwU)
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;
}
}
}
Aug 31, 2013 at 10:52pm
1
2
db.erase( std::find_if( db.begin(), db.end(), 
                        [&]( const std::string &s ) { return ( s == nameTdel ) } ); 
Last edited on Aug 31, 2013 at 10:54pm
Aug 31, 2013 at 10:56pm
Or

1
2
db.erase( std::find_if( db.begin(), db.end(),
                        std::bind2nd( std::equal_to<std::string>(), nameTdel ) );
Last edited on Aug 31, 2013 at 10:57pm
Aug 31, 2013 at 11:11pm
closed account (2604izwU)
This is an assignment from an introductory course, we haven't been taught any thing like that. Thanks though.
Aug 31, 2013 at 11:14pm
And what is the problem? Is nameTdel empty?
Aug 31, 2013 at 11:16pm
closed account (2604izwU)
yeah, every time I go through the debugging, nameTdel has nothing in it even after I enter something
Aug 31, 2013 at 11:23pm
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.