Printing the largest number from a .txt file.

Hello,
I need to print the largest/smallest number (age) from a text file along with the Name of the person it is associated to.
The .txt looks like this:

Duck Donald

45.0 3.50 60

Mouse Mickey

35 3.50 55
There's a lot more names than that. The last number in the row is age.
I have tried multiple things, but I can't figure out how to specifically target the file when trying to find the largest number. Everything that I found online has to do with the user putting in the data. Any hints would be appreciated.
The actual piece of code i need help with is at the end. I included everything else, so that you guys could see which variables i've used.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95

#include <iostream>
#include <cstdlib>
#include <cmath>
#include <fstream>

using namespace std;

int main()
{
    //print heading
    cout << "                           Miser Corporation Payroll" << endl;

    //open file
    ifstream infile;
    infile.open("misercorp.txt");

    //check if file is open
    if (!infile.is_open() )
    {
        //print error message if file is not open and end program
        cout << endl << "ERROR: Unable to open file" << endl;
        system ("PAUSE");
        exit(1);
    }

    //read from file NAME, HOURS, RATE AND AGE
    //read until all data is read
    while (infile.good())
    {
        //declare variables
        string lastName;
        string firstName;
        double hoursWorked;
        double rate;
        int age;
        int maxAge;
        int lowAge;
        double basePay;
        double overtime;
        int additionalHrs;
        double tax;
        double netPay;
        //assign variables
        infile >> lastName;
        infile >> firstName;
        infile >> hoursWorked;
        infile >> rate;
        infile >> age;

        //print data
        cout << "Employee's name is " << lastName << " " << firstName << "\n" << "Hours worked: "
        << hoursWorked << "     Rate of Pay: " << rate << "     Age: " << age << endl;

        //compute base pay ( VARIABLES: BASEPAY, OVERTIME, additionalHrs)
        //assign additionalHrs = ((if hoursWorked > 40) - 40)
                            //otherwise = 0
        if (hoursWorked > 40)
            additionalHrs = hoursWorked - 40;
        else
            additionalHrs = 0;
        //assign overtime = additionalHrs * rate * 1.5
        overtime = additionalHrs * rate * 1.5;
        //assign basepay = (hoursWorked - additionalhours)*rate+overtime
        basePay = (hoursWorked - additionalHrs) * rate + overtime;
        //print base pay
        cout << "The Base Pay is: " << basePay << endl;

        //compute and assign tax according to age
            //if 55+ tax = 50% of base pay
            if (age >= 55)
                tax = basePay / 2;
            //if <55 tax = 10% of base pay
            else
                tax = basePay / 10;
        //compute and assign net pay
        netPay = basePay - tax;

        //print tax and net pay
        cout << "The Tax is: " << tax <<  "     The Net Pay is: " << netPay << endl << endl;

        //print age and name of oldest person
        while(!infile.eof())
        {
        maxAge = age;
        if (age > maxAge)
            maxAge = age;
        cout << "max is: " << maxAge << endl;

//this gives me a constant loop where it says max is: 60. 60 is not ever the largest (age)
        //print age and name of youngest person

  }      //print message saying program is complete
}
Last edited on
Topic archived. No new replies allowed.