A teacher has asked all her students to line up single file according the their
ID: 3544198 • Letter: A
Question
A teacher has asked all her students to line up single file according the 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 a program to read names from the attached LineUp.txt. Names should be read in until there is no more data to read. Then report which student would be at the front of the line and which one would be at the end of the line.
Note: in the attached LineUp.txt file, names are in random order, you are to find the name less than all others (Adam is less than Bob), and the name larger than all others (Tom is larger than Jack).
These are the names attatched to the file we were given. I can get the program to read the names, but I don't know how to put it in order. Also, we pretty much have only been taught up to loops, so keeping this very simple and basic would help the most, thanks.
Explanation / Answer
#include <iostream>
#include <fstream>
#include <vector>
#include <algorithm>
using namespace std;
int main(){
vector<string> names;
ifstream infile;
infile.open("LineUp.dat");
if(!infile){
cout<<"couldn't open file";
return 1;
}
while(!infile.eof()){
string temp;
getline(infile, temp);
names.push_back(temp);
}
infile.close();
sort(names.begin(), names.end());
cout<< "First: "<<names[0] << endl;
cout<< "Last : "<<names[names.size()-1] << endl;
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.