I have an issue with my while loop (line 17) displaying the last donor in the list twice. I've looked around and I couldn't find any solutions that worked for me. Any ideas would be greatly appreciated.
Here is my function:
void delete_record( istream* file, string keyword ){
char response;
ofstream temp_file( "temp.txt" );
string fname, lname, phone, blood;
donor donora;
string temp;
search_records( file, keyword );
file->clear();
file->seekg(0);
cout << "Would you like to delete these records? (y/n)" << '\n';
cin >> response;
if ( response == 'y' ){
while ( *file >> fname && file->peek() != EOF ){
donora.set_fname( fname );
*file >> lname;
donora.set_lname( lname );
*file >> phone;
donora.set_phone( phone );
*file >> blood;
donora.set_blood( blood );
if ( (donora.get_fname() != keyword) &&
(donora.get_lname() != keyword) &&
(donora.get_phone() != keyword) &&
(donora.get_blood() != keyword) ){
donora.write_record( &cout );
cout << '\n';
} else {
// donora.write_record( &cout );
}
}
cout << "File(s) deleted" << '\n';
} else if ( response == 'n' ){
cout << "File has not been deleted" << '\n';
} else {
cerr << "Invalid choice. Please try again" << '\n';
delete_record( file, keyword );
}
}