Data.txt file:
John Smith Freshman U2010001
Kirti Seth Freshman U2010002
Nick Johnson Freshman U2010003
Alan Walker Freshman U2010004
Peter Foster Freshman U2010005
As soon as a user enters his/her ID, the program finds the line number, where the ID, that has been entered exists, and a new file NEWFILE.txt should store all lines except that line, but for some reason, my code is just storing the lines below the line, in which the user's ID exists, instead of storing all lines and skipping that line.
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main()
{
string Delete_this_Line;
string line, Aline;
int LineNumber = 1, i=1;
ifstream Read("Data.txt", ios::in);
ofstream NewFile("NewFile.txt", ios::out | ios::app);
if (Read.is_open())
{
cout << "ID (to be deleted): ";
cin >> Delete_this_Line;
while (!Read.eof())
{
getline(Read, line);
size_t found = line.find(Delete_this_Line);
if (found != string::npos)
{
break;
}
LineNumber++;
}
while (!Read.eof()) // for each candidate word read from the file
{
i++;
getline(Read, Aline);
NewFile << Aline << "\n";
if (i == LineNumber)
{
continue;
}
}
}
Read.close();
NewFile.close();
return 0;
}
When I enter the ID U2010003, the compiler copies the following data into the NEWFILE.txt file:
NEWFILE.txt:
Alan Walker Freshman U2010004
Peter Foster Freshman U2010005
But I've expected to get the following result => NEWFILE.txt:
John Smith Freshman U2010001
Kirti Seth Freshman U2010002
Alan Walker Freshman U2010004
Peter Foster Freshman U2010005
I honestly don't know why this is happening, can you please help me to achieve the desired result?