Delete data from txt file

I need to delete data from txt file. My code is not working properly. Where am I making mistakes? I need to delete the person number from the file.

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
28
29
30
31
32
33
34
  int delete = 0;
				ifstream readfile("person.txt");
				ofstream writefile("person.tmp");

				do
				{
					cout << "Delete data " << endl;
					cout << "*****************" << endl;
					cout << "data that needs to be deleted " << endl;
					cin >> personnumber;
					while (!readfile.eof())
					{
						readfile >> personnumber ;
						if (delete == personnumber)
						{
							cout << "\n record \n";
							
							cout << personnumber << endl;
							

						}
						else
						{
							writefile  << personnumber  << "\n";
						}
					}
					writefile.close();
					readfile.close();
					remove("person.txt");
					rename("person.tmp", "person.txt");
					cout << "\n Will you do anything else(y)/(n)? "; cin >> answer;
				} while (!(answer == 'n'));
				cout << "success.";
				break;
Last edited on
You shouldn't name a variable like a keyword (delete).

On line 13 you overwrite the value from line 10. 'delete' is never changed. So only 0 would be removed.
Topic archived. No new replies allowed.