我想讀取names.txt文件中的數據,並輸出每個人的全名和理想體重。使用循環從文件中讀取每個人的姓名,英尺和英寸。 讀取文件:使用getline從文件讀取多行?
Tom Atto 6 3 Eaton Wright 5 5 Cary Oki 5 11 Omar Ahmed 5 9
我使用這下面的代碼:
string name;
int feet, extraInches, idealWeight;
ifstream inFile;
inFile.open ("names.txt");
while (getline(inFile,name))
{
inFile >> feet;
inFile >> extraInches;
idealWeight = 110 + ((feet - 5) * 12 + extraInches) * 5;
cout << "The ideal weight for " << name << " is " << idealWeight << "\n";
}
inFile.close();
當我運行這個即時得到輸出:
The ideal weight for Tom Atto is 185 The ideal weight for is -175
你的問題是什麼? – arslan
爲什麼我得到錯誤的輸出 –