Hi,
I have an input file which contains 3 sentences:
Jane likes chicken.
Alex likes chicken too.
both like chicken.
At the beginning of the 3rd sentence I want to add a word "They" while the reference line is the 1st line i.e "Jane likes chicken".
The code I have tried to develop can do so if reference is made to the exact line where I want to add the word but this is not what I want. Below is the program that I have developed but as I said before, it adds the word when reference is made to the exact line of insertion.
#include <iostream>
#include <fstream>
usingnamespace std;
string line;
int main()
{
/*opens the input file*/
ifstream infile("../testing_in.txt");
if (!infile.is_open())
{
cout<<"problem!!!!"<<endl;
return 1;
}
/*this part opens the temperate output file*/
ofstream outfile ("testing_out.txt");
/*this reads the lines in the input file and inserts
a word "They" in one of the lines specified*/
while(getline(infile,line))
{
if(line.substr(0,line.find(" "))== "both")
{
line.insert(0,"They ");
outfile<<line<<endl;
}
}
}
Is there a way whereby I can do something like this:
(1) Find a line that begins with "Jane".
(2) Then two (2) line after that line with Jane (which brings me to the line "both like chicken" in this particular case, insert the word "They".