Code:
string str[] = 100;
I think you meant:
Sorting is pretty easy with the help of the standard library. You can start by writing a function object that compares the first characters of two strings:
Code:
class CompareByFirstChar
{
public:
bool operator()(const std::string& x, const std::string& y) const
{
return x[0] < y[0]; // Assume all strings have at least 1 character.
}
};
Then, in say, your main() function you can call std::sort() from the <algorithm> header, instantiating a CompareByFirstChar function object as you do so:
Code:
sort(str, str + 100, CompareByFirstChar());
Incidentally, it looks like you are not handling your files properly. You have one myfil fstream object in the scope of main(), and another in the scope of a while loop in main(). You are probably looking to write a loop along these lines:
Code:
string file;
// ... read into file ...
fstream myfil(file.c_str());
while (!myfil)
{
cout << "file could not be opened. enter the name of another file in the format filename.txt:";
cin >> file;
myfil.clear();
myfil.open(file.c_str());
}