Ok, it might seem a little complicated.
Please concentrate on the most inner part of the loop.
Once i get to else(), i would like to break out of while() and from the for() loop too.
But if i put a second "break;" outside of while(), it will break me out of for() even when i come out of if(), which i dont want. Once out of if() i want it to go back to for().
Maybe this is the case to use the unfamous "goto" ?
Code:
#include <string>
#include <cctype>
#include <vector>
#include <fstream>
#include <iostream>
using namespace std;
void openFile(ifstream& file){
file.clear();
for(;;)
{
string fileName;
cout << "Enter the name of your file: " ;
cin >> fileName;
file.open(fileName.c_str());
if (file.good())
{
break;
}
cerr << "\nThere is no file, named --> " << fileName << " <-- in your folder.\n\n" << endl;
file.clear();
}
}
int main ()
{
ifstream file1;
openFile(file1);
ifstream file2 ("List of proper nouns.txt");
ifstream file3 ("List of proper nouns preceeding words.txt");
ofstream TheCopy;
TheCopy.open ("Your_Uppercased_Proper_Nouns_Copy.srt",ios::app);
string str;
vector<string> vProperNouns;
vector<string> vPreceeding; //vProperNounsPrecedingWords
while (getline(file2, str))
{
vProperNouns.push_back(str);
}
file2.close();
while (getline(file3, str))
{
vPreceeding.push_back(str);
}
file3.close();
//write a line into str
while (getline(file1, str))
{
BreakLoop:
for ( string::size_type i = 0; i < vProperNouns.size(); i++)
{ //search for proper noun in str
string::size_type pos = 0;
while (( pos = str.find(vProperNouns[i], pos)) != string::npos)
{
for ( string::size_type j = 0; j < vPreceeding.size(); j++)
{ //search for preceeding word before proper noun
string::size_type loc = 0;
while((loc = vProperNouns[i].rfind(vPreceeding[j],pos)) != string::npos)
{
// the one it found is just not before the proper noun: go back to for().
if ( loc < (pos - (vPreceeding[j].size()+ 1)))
{
break;
}
//the one it found is just before the proper noun: break out of while() and out of for() too.
else
{
pos += str.size();
goto BreakLoop;
}
}
loop: break;
}
str[pos] = toupper (str[pos]);
}
}
cout<<str<<endl;
//TheCopy << str << endl;
}
file1.close();
TheCopy.close();
system("pause");
return 0;
}