I am trying to read in from firstLastAge.txt that contains:
Fred Smith 21
Tuyet Nguyen 18
Joseph Anderson 23
Annie Nelson 19
Julie Wong 17
I have made my write to file code to output and format it like so:
Name: Fred Smith, Age: 21
Name: Tuyet Nguyen, Age: 18
Name: Joseph Anderson, Age: 23
Name: Annie Nelson, Age: 19
Name: Julie Wong, Age: 17
Now that I have separated the text file into strings and age, I am trying to print the lowest and highest of this data. I'm not quite sure how I can integrate a sorting function into this.
Here's my code:
#include <fstream>
#include <string>
#include <iostream>
#include <algorithm>
int main()
{
std::string firstName;
std::string lastName;
std::string name;
int age;
std::ifstream fin;
fin.open("firstLastAge.txt");
while (!fin.eof())
{
fin >> firstName >> lastName >> age;
name = firstName + " " + lastName;
std::cout << "Name: " << name << ", ";
std::cout << " Age: " << age << std::endl;
}
fin.close();
}