我在編寫程序的一部分時會遇到問題,該程序將從文件中讀取名稱和10個數字。外商投資企業被稱爲grades.dat數據文件的結構是:如何在C++中分割一行並從中提取值?
Number One
99 99 99 99 99 99 99 99 99 99
John Doe
90 99 98 89 87 90.2 87 99 89.3 91
Clark Bar
67 77 65 65.5 66 72 78 62 61 66
Scooby Doo
78 80 77 78 73 74 75 75 76.2 69
這是我的所有的函數來獲取數據,我甚至不知道這是正確的。
void input (float& test1, float& test2, float& test3, float& test4, float& test5, float& test6, float& test7, float& test8, float& test9, float& test10, string& studentname)
{
ifstream infile;
infile.open ("grades.dat");
if (infile.fail())
{
cout << "Could not open file, please make sure it is named correctly (grades.dat)" << "\n" << "and that it is in the correct spot. (The same directory as this program." << "\n";
exit(0);
}
getline (infile, studentname);
return;
}
'90.2'不是一個整數。你的意思是它應該是一個名字,然後是十個*數字*?如果不是,你的程序如何處理畸形輸入? – meagar
是的,是我的錯。編輯的問題。謝謝 –