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
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;
}
}
}
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.