ifstream olympicsStream;
olympicsStream.open("olycs105.txt");
This is where the file is opened
olympicsStream >> firstcountry; // string variable for countries
while (firstcountry!="END_OF_FILE")//
Can't figure out what to put here for condition
{
cout << firstcountry << endl;
olympicsStream >> a;
cout << a << endl;
for (i = 0; i < 5; i++)
{
olympicsStream >> namelast[i];
olympicsStream >> namefirst[i];
olympicsStream >> b[i];
olympicsStream >> c[i];
olympicsStream >> d[i];
getline(olympicsStream, Asport[i]);
}
cout << namelast[i - 1] << endl;
cout << namefirst[i - 1] << endl;
cout << b[i - 1] << endl;
cout << c[i - 1] << endl;
cout << d[i - 1] << endl;
cout << Asport[i - 1];
}
loops 5 lines needed but the same line infinitely instead of reading through the rest of the file
最佳答案
您忘记在 while 循环结束时更新变量“firstcountry”。
只需在关闭 while 循环之前添加 olympicsStream >> firstcountry;
,您的代码就可以正常运行
假设文件中的最后一个词是“END_OF_FILE”
关于c++ - 我得到一个无限循环,我不确定为什么或如何修复它,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47587498/