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;
}

You are storing the names as int's which doesn't really make sense. I suggest using a std::vector<std::string> .

I suggest breaking your problem into two programs for the moment. One which reads your data, and the other which sorts a list of names.

1) For the input, checkout these examples:
http://programmingexamples.net/index.php?title=CPP/IO/ReadingLines
http://programmingexamples.net/index.php?title=CPP/IO/FileInput

2) For the sorting, check this out:
http://programmingexamples.net/index.php?title=CPP/STL/Sort

Good luck,

David

They all involve std, and we have not done that in my class so far. In the text book example of sorting with names, int is used which is why I used it as well.

I've fixed the issue however now it is only doing 5 names and sorting them. I have the capacity set to 8 and more than 8 are in the list.I don't understand why it is not doing the full 8.

#include <iomanip>
#include <iostream>
#include <string>
using namespace std; 

struct Student
{
	string name;
};

void printNames(Student* student, int nStudents)
{
	int i;
	for (i = 0; i < nStudents; i++)
	{
		cout << student[i].name << endl;
	}
}
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 nStudents = 0;
	Student student[MAX_NAMES];

	// read and save names
	while (fin.good())
	{
		Student aStudent;
		getline(fin, aStudent.name);

		fin.ignore(1000, 10);

			// add name to list if not full
			if (nStudents < MAX_NAMES)
				student[nStudents++] = aStudent;
	}
    
	fin.close();

	for (int i =0; i < nStudents; i++)
	{
		for (int j = i + 1; j < nStudents; j++)
		{
			if (student[i].name > student[j].name)
			{
				Student temp = student[i];
				student[i] = student[j];
				student[j] = temp;
			}
		}
	}

	printNames(student, nStudents);

	return 0;
}
const int MAX_NAMES = 8;
    int nStudents = 0;
	Student student[MAX_NAMES];

you set MAX_NAMES to 8 and used it to make an array of size 8. Arrays are static, you can't change its size after you initialize it. If you want it to be dynamic look at david's comment. BTW you are already using "std::" with the line

using namespace std;

What I'm saying is there are about 9 names in the text file, but it is only printing 5 names even though the max is 8. I don't understand why it is cutting off sooner.

That is the only bit of std I've used. What he suggested isn't in my textbook.

#include <iomanip>
#include <iostream>
#include <fstream>
#include <string>
using namespace std; 

struct Student
{
	string name;
};

void printNames(Student* student, int nStudents)
{
	int i;
	for (i = 0; i < nStudents; i++)
	{
		cout << student[i].name << endl;
	}
}
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 nStudents = 0;
	Student student[MAX_NAMES];

	// read and save names
	while (fin.good())
	{
		Student aStudent;
		getline(fin, aStudent.name);

// 		fin.ignore(1000, 10);

			// add name to list if not full
			if (nStudents < MAX_NAMES)
				student[nStudents++] = aStudent;
	}
    
	fin.close();

	for (int i =0; i < nStudents; i++)
	{
		for (int j = i + 1; j < nStudents; j++)
		{
			if (student[i].name > student[j].name)
			{
				Student temp = student[i];
				student[i] = student[j];
				student[j] = temp;
			}
		}
	}

	printNames(student, nStudents);

	return 0;
}

Why

// fin.ignore(1000, 10); ?

and

#include <fstream>

I don't know why I had the first bit.
I have to have that include because I am gathering from a file and its required. I use elements from the library.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.