First off, I know I have a few other errors besides the sorting code. I am working on figuring those out as well. I need to sort the names in the file alphabetically. I did put a code which I thought would work, but doesn't. Lines 39 through 50.
#include <fstream>
#include <iomanip>
#include <iostream>
#include <string>
using namespace std;
int main()
{
ifstream fin;
// opening file
string fileName;
cout << "What file do you want to open?: ";
getline(cin, fileName);
fin.open(fileName.c_str());
if (!fin.good()) throw "I/O error";
// creating empty list
const int MAX_NAMES = 8;
int aName = 0;
int names[MAX_NAMES];
// read and save names
while (fin.good())
{
// read from file
int aName;
getline(fin, aName);
// skip blank lines
if (aName.length() > 0)
{
// add name to list if not full
if (aName < MAX_NAMES)
names[aName++] = aName;
}
}
for (int i =0; i < aName; i++)
{
for (int j = i + 1; j < aName; j++)
{
if (names[i] > names[j])
{
names temp = names[i];
names[i] = names[j];
names[j] = temp;
}
}
}
cout << aName << endl;
fin.close();
return 0;
}