我有一個文本文件,內容是這樣的:從字符串中提取整數並轉換爲整數?
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