Hello everyone,
I really need some help with this project I am working on. So I need to go through a text file to search for a particular string (say something like 'play') and then if that word exists in a line I need to find a string after that (say maybe 'try') and copy some numbers after that. I can do the rest but I am having problems searching for the initial string. The sstring 'try' exists in every line and I only want numbers following the string 'try' if the word 'play' also exists in that line. Here is what I have so far
string str_text;
const char searchT[] = "play";
size_t sizeT = strlen (searchT);
const char search[] = "try";
size_t size = strlen (search);
size_t pos = 0;
vector<double> Q;
while ( getline(infile, str_text).good () )
{
if( (pos = str_text.find (searchT, 0)) != string::npos ) //Find 'play'.
{
str_text = str_text.erase (pos, sizeT) ;
}
if ( (pos = str_text.find (search, 0)) != string::npos ) //Find 'try'.
{
str_text = str_text.erase (pos, size) ;
istringstream ins(str_text);
ins >> q;
Q.push_back(q);
}
}
My biggest problem is I just get the numbers after try every time. I dont want the numbers after try if the word play does not exist in the line.