我有一个文本文件,内容是这样的:从字符串中提取整数并转换为整数?
Department Max
Bob 3 5 6 2 8 F1
Frank 4 8 8 8 8 F2
Jerry 7 9 9 0 9 F3
William 0 0 12 13 14 F2
Buck 3 4 10 8 8 4 6 F4
Greg 1 2 1 4 2 F1
Mike 3 4 4 8 4 F2
而忽略了名字,我怎么读这些线和提取这些整数作为独立的整数,所以我可以把它们相加在一起?
到目前为止,我有这样的:
for (int i = 0; i < 10; i++) //Loop that checks the employee and their paygrade, 10 times for 10 lines of data
{
getline(inFile, s); //Gets next line in Salary.txt
string payGrade = s.substr(s.length() - 2, 2); //Checks the last two letters of the line to see paygrade
if (payGrade == "F1")
{
auto pos = s.find(' ', s.find('"'));
istringstream iss(s.substr(pos));
F1employeeCounter++;
}
if (payGrade == "F2")
{
F2employeeCounter++;
}
if (payGrade == "F3")
{
F3employeeCounter++;
}
if (payGrade == "F4")
{
F4employeeCounter++;
}
}
基本上我要检查什么类型的“薪级”每个员工。有四种不同类型的薪酬等级:F1,F2,F3,F4,每种薪酬等级的工作时间都有不同的支付方式。
你可以用'atoi'将字符串转换为整数。您当然仍然需要拆分字符串。 – 2014-10-30 21:37:05
http://stackoverflow.com/questions/7663709/convert-string-to-int-c + http://stackoverflow.com/questions/236129/how-to-split-a-string-in-c可能有帮助 – matsjoyce 2014-10-30 21:37:21
如果你有一个在线搜索很容易的问题,你可能需要先试着解决它。如果你之后仍然有问题,告诉我们你试过的东西 – PeterT 2014-10-30 21:45:31