So I am trying to make a program that gets an input from a text file, and has the user enter the term they want to search for. From there, the file should be printed out with 3 asterisks in front and behind all of the terms.
It works currently, but it only finds the first term in the file, I want it to find all terms. Any help?
Thanks
#include <iostream>
#include <fstream>
#include <string>
#include <cassert>
#include <cstdio>
using namespace std;
int main(void)
{
int a, i, z, loc;
string inputFileName;
string s, term;
ifstream fileIn;
char ch;
cout << "Enter the name of a file of characters: ";
cin >> inputFileName;
fileIn.open(inputFileName.data());
assert(fileIn.is_open() );
i = 0;
while (!(fileIn.eof()))
{
ch = fileIn.get();
s.insert(i, 1, ch); //insert character at position i
i++;
}
cout << s;
cout << "Enter a word/phrase you want to search for in the file you entered" << endl;
cin >> term;
cout << "The word/phrase " << term << " will have '***' before it and after it" << endl;
z = (int)term.length();
loc = s.find(term);
s.insert(loc, 1, '*');
s.insert(loc+1, 1, '*');
s.insert(loc+2, 1, '*');
s.insert(loc+3+z, 1, '*');
s.insert(loc+4+z, 1, '*');
s.insert(loc+5+z, 1, '*');
cout << s;
return 0;
}
Text output
Enter the name of a file of characters: text.txt
Repair- Determined by the difficulty of the fix, and the parts needed in order for it to work.
Enter a word/phrase you want to search for in the file you entered
the
The word/phrase by will have '***' before it and after it
Repair- Determined by the difficulty of the fix, and the parts needed in order for it to work.