I am supposed to use a txt file and print out the names in alphabetical order.
Below is what I have put together so far, but I do not get any output. If someone could put of what I am missing that would be great.
//This program will read a file with names and
//put them in alphabetical order
#include <iostream>
#include <string>
#include <fstream> //needed to input files
using namespace std;
int main()
{
//variables
string names, first, last;
//opens the file
ifstream file;
//Tells the user what the program does
cout << " Line up! \n";
cout << "Is a program that will put your student's names in alphabetical order.\n";
//opens the file
file.open("lineUp.txt");
//test if the file open correctly
if (!file)
{
cout << "Error: Unable to read file.\n";
cout << "Please check file and try again.\n";
return 1;
}
//Sets the initial value first inline and last inline
file >> names;
first = names;
//loops through the file
while (file >> names)
{
if (names < first)
first = names;
if (names > last)
last = names;
}
cout << first << endl;
cout << last << endl;
file.close();
return 0;
}
Here is the txt file
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
The lack of output is a regular stock problem here and the best bet is to forget about sorting and data structures for a while and just get some output.
So, go to http://www.cplusplus.com/doc/tutorial/files/ and use the third example and get that running with your data file.
After that start incorporating the aspects forgotten about.
Note that instead of getline( ...) you can have myFile >> name etc
Note that one of the problems with your program is the if else and testing of the file being open etc is not right.
Thank for the input, but the issue is where I put the txt file. I did add existing item in visual studio and my program did not know where the file was, but after I added to the root of the C++ file I was able to run my program and after talking to my teacher all I needed to do is get the first and last not soft.