Here's this program i write to uppercase proper nouns in a file.
It opens three files:
1. is the file to uppercase.
2. is a list of proper nouns to uppercase.
3. is a list for proper nouns preceeding words that excludes proper nouns to be uppercased.
I rechecked it a hundred times, for some reason it wont even get in the while() loop with rfind()
I put "cout<< "loc: "<<loc<< endl;" there to see whats going on.
Any hint would be highly appreciated. Thanks!
#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)
{ cout<< "loc: "<<loc<< endl;
// 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;
}
}
}
str[pos] = toupper (str[pos]);
}
}
cout<<str<<endl;
//TheCopy << str << endl;
}
file1.close();
TheCopy.close();
system("pause");
return 0;
}
Sorry for the messed up code, apparently the edit box is larger than the template on the site.