问题
I need to find average salary from employees of file
John Harris $50000.00
Lisa Smith $75000.00
Adam Johnson $68500.00
Sheila Smith $150000.00
Tristen Major $75800.00
Yannic Lennart $58000.00
Lorena Emil $43000.00
Tereza Santeri $48000.00
How can I access the salaries of the employees so that i can find the average? I have managed to get each line of a file into a string but I dont know how to access the salaries of each employee my code is:
#include<iostream>
#include<fstream>
#include<cstring>
#include<cstdlib>
using namespace std;
int main()
{
ifstream in;
in.open("HW6Prob2.txt");
if(in.fail())
{
cout<<"ERROR: File could not open."<<endl;
exit(1);
}
string word[8];
int i=0;
for(i=0;i<8;i++)
{
getline(in,word[i]); //get line string
out<<word[i]<<endl;
}
string a=word[0];
string b=word[1];
string d=word[3];
string e=word[4];
string f=word[5];
string g=word[6];
string h=word[7];
cout<<a[13]<<endl;
string sum=
cout<<sum<<endl;
return 0;
}
回答1:
I would suggest that you keep adding the average while you read the lines, so you just iterate once through the salaries list.
int i = 0;
float avg_salary = 0;
string line;
// get the sum while you read the lines
while(getline(in, line)) {
// find the first salary digit position (just after the $ sign)
int salaryStartPos = line.find('$') + 1;
// Convert the salary string to a float with the atof method
avg_salary += atof(line.substr(salaryStartPos, line.size()-1)
++i;
}
// Finally calculate the average
avg_salary = avg_salary / i;
回答2:
This looks like a school assignment, so I'll give you some tips on how to approach the challenge with pseudo-code:
sum = 0
numberOfPersons = 0
for each line in "HW6Prob2.txt"
pos = find position of $
salary = cut the string from pos and parse as double
sum = sum + salary
numberOfPersons = numberOfPersons + 1
loop
average = sum / numberOfPersons
I hope you'll find this helpful!
回答3:
You can use the stof function to get the float value from the string. All you need is to figure out the starting point of the float. In your case, you can use position of $ + 1
as the starting point. Use find function for that.
回答4:
First you should iterate through the file lines (until end) to read all the data:
std::string line;
while(std::getline(file, line))
{
// tokenize to get the last item and store it
}
If you have your file structure strictly defined as presented: [first_name] [last_name] $[salary] you can read each salary entry like:
const string salaryText = line.substr(line.find_last_of('$') + 1);
The extracted salary text should be converted to a number and either stored in a vector<float>
or aggregated for each line. This depends on whether you also need to access particular salaries at some point. If you'd go with the vector option you could write smth like:
salaryList.push_back(std::stof(salaryText));
Afterwards you can calculate mean salary with:
const double salarySum = std::accumulate(salaryList.begin(), salaryList.end(), 0.0);
const double salaryMean = salarySum / salaryList.size();
The advantage of having the salary list is that you can further calculate other statistics, not only the mean value.
来源:https://stackoverflow.com/questions/41161547/find-average-salaries-from-file-in-c