这是我目前的代码。我正在从输入文件中获取数据。该文件包含我正在调用的每个变量所需的所有数据,但是当我运行它时,我只进行了一次迭代,然后它退出了循环。我需要它一直运行到文件末尾。
#include <iostream>
#include <fstream>
#include <string>
#include <cmath>
using namespace std;
int main ()
{
ifstream pTemp("PlanetCelsius.dat");
int pNumber, pSize;
string pName;
double cTemp, fTemp;
cout << "Number\t" << "Planet Name\t" << "Diameter\t" << "Celsius\t" << "Fahrenheit" << endl;
while (pTemp >> pNumber)
{
while (pTemp >> pName)
{
while (pTemp >> pSize)
{
while (pTemp >> cTemp)
{
pTemp >> pNumber;
cout << pNumber << " ";
}
pTemp >> pName;
cout << pName << " ";
}
pTemp >> pSize;
cout << pSize << " ";
}
pTemp >> cTemp;
fTemp = 9 * (cTemp) / 5 + 32;
cout << cTemp << " " << fTemp << endl;
}
system ("PAUSE");
return 0;
}
最佳答案
while (cin.good)
{
pTemp >> pNumber >> pName >> pSize >> cTemp >> pNumber;
cout << pNumber << " " << pName << " " << pSize << " ";
fTemp = 9 * (cTemp) / 5 + 32;
cout << cTemp << " " << fTemp << endl;
}
return 0;
我还没有看到你的文件结构,但我想这个代码片段可以工作。
关于c++ - 为什么 C++ io 流的循环在第一次迭代后中断?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15625939/