0

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();


}

cigien
  • 57,834
  • 11
  • 73
  • 112
evanparial
  • 45
  • 2
  • 7
  • 4
    I don't see any question here, except "could you please do my homework for me?" – Russ Schultz Dec 04 '20 at 19:36
  • 1
    https://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-i-e-while-stream-eof-cons – Yamahari Dec 04 '20 at 19:38
  • @RussSchultz lol just a part of it – evanparial Dec 04 '20 at 19:39
  • We usually don't provide much help for parts of the assignment that are not attempted in the code provided. Related: [https://meta.stackoverflow.com/questions/334822/how-do-i-ask-and-answer-homework-questions](https://meta.stackoverflow.com/questions/334822/how-do-i-ask-and-answer-homework-questions) – drescherjm Dec 04 '20 at 19:45
  • Declare two variables `minAge` and `maxAge` outside of your loop. Every time you read `age`, set `minAge` to `min(age,MinAge)` and maxAge to `max(age,maxAge)`. When your loop completes, `minAge` and `maxAge` will contain the minimum and maximum age values. There is no need to sort the data and, in this case, it would be less efficient to do so since you're already iterating over all of it. – 3Dave Dec 04 '20 at 19:48

2 Answers2

2

What you can do is save each age into a std::vector Then you can call std::sort using the vector. After this you can get the last and first index values of the vector and print it out

Snippet taken from: https://www.cplusplus.com/reference/algorithm/sort/

This is a great example of how vectors work with sort.

  int myints[] = {32,71,12,45,26,80,53,33};
  std::vector<int> myvector (myints, myints+8);
  // using default comparison (operator <):
  std::sort (myvector.begin(), myvector.end()); 
  • Thanks for posting an answer. Could you add a few snippets of code to illustrate your point? It doesn't have to be a full program, just a few lines that show a `vector` and how `sort` works would make this a nice answer. – cigien Dec 04 '20 at 19:38
  • @cigien Try googling std::vector or std::sort There's loads of examples out there. Stack Overflow is not for solving your homework assignments. – Russ Schultz Dec 04 '20 at 19:45
  • @RussSchultz I think you might have misunderstood the purpose of the site. On SO, we judge whether the question is clear, and answerable, and whether it satisfies certain other criteria. Whether it's a HW assignment or not, is completely irrelevant. – cigien Dec 04 '20 at 19:50
  • Thanks for editing the answer to show some examples. The answer is much better now :) – cigien Dec 04 '20 at 19:56
2

Just record the highest and lowest as you loop, this requires minimal memory:

std::string hiName;
std::string loName;
int hiAge = 0;
int loAge = 10000;

while (!fin.eof())
{

    fin >> firstName >> lastName >> age;
    name = firstName + " " + lastName;

    std::cout << "Name: " << name << ", ";
    std::cout << " Age: " << age << std::endl;

    if(age > hiAge)
    {
        hiAge = age;
        hiName = name;
    }

    if(age < loAge)
    {
        loAge = age;
        loName = name;
    }
}

std::cout << "Max: " << hiName << " " << hiAge << std::endl;
std::cout << "Min: " << loName << " " << loAge << std::endl;
user2205930
  • 1,046
  • 12
  • 26
  • Hopefully `age` isn't in milliseconds. :D – 3Dave Dec 04 '20 at 19:50
  • You may want to address the `while (!fin.eof())` bug. [https://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-i-e-while-stream-eof-cons](https://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-i-e-while-stream-eof-cons) – drescherjm Dec 04 '20 at 20:00