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