Answer to Question #184687 in C++ for Shuvo

Question #184687

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.


1
Expert's answer
2021-04-23T07:45:00-0400
#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;
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS