So far, I have found several ways to read a text file in an array and have been able to successfully display it on the screen. I have a problem with how I sort an array from there. Sorting a text file was simple enough, but when I tried to combine both, I could not get it to work. Here is what I got so far:
int main()
{
string players[30];
ifstream inData("chessplayers.txt");
if (inData.is_open())
{
for (int i = 0; i < 30; i++)
{
sort( players, players+i);
if (getline(inData, players[i]))
{
cout << players[i] << endl;
}
else
{
break;
}
}
inData.close();
}
else
{
cerr << "Failed to open file.\n";
}
system("pause");
return 0;
}
Can anyone help point me in the right direction? My job is to read the text in an array and then sort this array.
source
share