平均工资我需要从文件找到文件在C++中
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
我如何可以访问员工的工资的员工找到平均工资,这样我可以求其平均值?我已经得到了一个文件的每一行成一个字符串,但我不知道如何访问每个员工的工资 我的代码是:
#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;
}
尝试在C++ 11中使用'regex'。它只适合你的问题。 http://stackoverflow.com/questions/27400131/extract-numbers-from-string-regex-c http://stackoverflow.com/questions/11627440/regex-c-extract-substring – Yves