I'm working on my programming homework and it has to read names froma file and put them in order. I have been avoiding this as I would rather figure it out myself but I am running out of time. The coding I've started isn't reading from the file. I've been having difficulty with this class but have been able to figure it out. Any hints or advice you can offer would be greatly appreciated.
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
string Name, // Name of student
Front, // Front of line
End; // End of line
ifstream InputFile; // Define ifstream object
cout << "\n This program reports which student would be at the front of\n"
<< "the line and which one would be at the end of the line.\n"
<< "When lined up in single file according to their first name.\n";
InputFile.open("lineup.txt"); // To open file
if (!InputFile) // Check for errors
cout << "Error openning file!\n";
else
{
InputFile >> Name; // Read first name
Front = End = Name;
while (!InputFile.eof()) // Read till end of file
{ // To sort students by name
if (Name > End)
End = Name;
if (Name < Front)
Front = Name;
InputFile >> Name; // Read next student
}
}
// Display the student at the front of the line
// and which is at the back of the line.
cout << endl << Front << " is at the front of the line."
<< End << " is at the end of the line.\n";
First, please post code with code tags. See: http://www.cplusplus.com/articles/jEywvCM9/
Pay attention to indentation and whitespace to make the code more readable.
You can edit your previous post.
isn't reading from the file
Does it give the "Error openning file!" or what?
Do not use eof(). It doesn't quite do what one expects. This is reliable:
1 2 3 4
while ( InputFile >> Name )
{
// do something with the Name
}