Need help sorting String values (People's Names) from an Input File (Already created Text file) in ascending order (A - Z)

I'm working an optional section in my textbook, and have no idea how to do it. The textbook doesn't really give me much info on how to do this, and I'm completely lost. Here is the question:
______________________________________________________________________
"A teacher has asked all her students to line up single file according to their first name.
For example, in one class Amy will be in front of the line and Yolanda will be at the end.

Write a program that will read in a file of names.
Names should be read until there are no more names to be read.
Use LineUp.txt as a test file for your program.

Once all the names have been read in display which student will be at the front of the line and which one would be at the end of the line.
You may assume that no two students have the same name."
_________________________________________________________________________

The Lineup.txt file the book gives is just a list of names, all on different lines, saved as a .Txt file (notepad) in the project location

Names are here in this order:

Cooper
Gavin
Joseph
Sierra
Kacie
Dylan
Kaylee
Will
Cameron
Kieron
Samuel
Alexis
Eryn
Emily
McKenna
Brandon
Thomas
Luke
Carlee
Matthew
Elizabeth
Danny
Rachel
Haley
Colleen
Brian
Trey
Noah
Rena
Gillan
Caroline
Cassidy
Kevin
Jason
Zach
Hannah
Dalton
Ian
Sarah
Brandy
Brittany
Jessica
Mia
Victoria
Mark
Michael


Any assistance would be extremely appreciated. I really want to learn this stuff, but I feel like not enough was information was given to me, and I've been searching everywhere and can't find anything on how to do this.

This is the code the book started us off with:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#include<iostream>
#include<string>
#include<fstream>
using namespace std;

int main()
{
	//variables
	string firstInLine;			//holds the current value of the first person in line
	string lastInLine;			//holds the current value of the last person in line
	string name;				//name read in from the file

	ifstream inNames;			//input file

	//open the file for reading
	inNames.open("lineup.txt");

	if (!inNames)		//file did not open properly
	{
		cout << "The lineup.txt file did not open properly" << endl;
		return 1000;	//exit the program
	}

	//the file did open properly

	//set initial values to the first name in the file
	inNames >> name;   //read in the first name in the file

	firstInLine = name;
	lastInLine = name;

	while (inNames >> name)		//read the next name in the file
	{
		if (name < firstInLine)
			firstInLine = name;

		//more goes here

	}
	
	return 0;
}
Last edited on
So before I proceed, what the question is asking for is the first and last names that appear in the file?
The names on the lineup.txt file are only first names (no last names at all). But the order of the names in the actual lineup.txt file is out of order. So I need to know how to display the list of names on the lineup.txt file to the console in alphabetical order. The ideal Output would look like this (used excel): (BTW thank you so much for taking a look)

Alexis
Brandon
Brandy
Brian
Brittany
Cameron
Carlee
Caroline
Cassidy
Colleen
Cooper
Dalton
Danny
Dylan
Elizabeth
Emily
Eryn
Gavin
Gillan
Haley
Hannah
Ian
Jason
Jessica
Joseph
Kacie
Kaylee
Kevin
Kieron
Luke
Mark
Matthew
McKenna
Mia
Michael
Noah
Rachel
Rena
Samuel
Sarah
Sierra
Thomas
Trey
Victoria
Will
Zach
Last edited on
Oooh... I should've probably read the title better to begin with. :P Anywho, you're using an operator that isn't defined legal for strings in C++. You can't check of a string is "greater than" or "less than" another as you did in line 34. My suggestion would be to use the strcmp function in the cstring library.

You would need to also use the .c_str() function for your strings which turns your string into a valid cstring which can be used as a function parameter. A sample of the code you need is like so:
1
2
if ( strcmp( name.c_str(), firstInLine.c_str() ) < 0)
    firstInLine = name;

Read about the functions return value to know why I tested to see if it was less than 0.
http://www.cplusplus.com/reference/cstring/strcmp/
I'm such an idiot....The question is literally only asking me for the First name that would be on the list and Last name that would be on the list (Alexis/Zach), NOT the whole list in alphabetical order. I was able to do this as that is easy. Thank you for taking a look, and thank you for providing me with this reading material for future learning. I apologize for wasting you time :(

Thank you!
Topic archived. No new replies allowed.