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.
#include <iostream>
#include <cstdlib>
#include <cmath>
#include <fstream>
usingnamespace 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
}