A teacher has asked all her students to line up according to to their first name. For example, in one class Amy will be at the front of the line and Yolanda will be at the end. Write program that reads the number of students and their names from a file. Once all the names have been read, it reports which student would be at the front of the line and which would be at the end of the line.
#include <iostream>
#include <fstream>
#include <set>
#include <string>
int main()
{
std::ifstream file("names.txt");
if(!file)
{
std::cerr << "Error: open file failed\n";
return 1;
}
int namesCount;
file >> namesCount;
std::set<std::string> names;
for(int i = 0; i < namesCount; ++i)
{
std::string name;
file >> name;
names.insert(name);
}
std::cout << "Front of the line: " << *names.begin() << "\n";
std::cout << "End of the line: " << *names.rbegin() << "\n";
return 0;
}
Comments
Leave a comment