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

fstream objects are normally passed by reference, not by pointer. void delete_record( istream& file, string keyword ){ . This allows you to use file just as if it had been declared inside the function and removed the indirection star. while ( *file >> fname && file->peek() != EOF ) It is not necessary to check for EOF because *file will become NULL on EOF. This is all that is necessary while ( *file >> fname ) Remove the star if you change the function as described above.

If that does not fix the problem then consider using getline() to read the entire line and then using stringstream to split it into individual words.

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.