c++ - 我得到一个无限循环,我不确定为什么或如何修复它

标签 c++

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/

相关文章:

c++ - 使用for循环在基于数组的列表中实现删除

c++ - 无法在 wxWIdgets 3.0 C++ 中连接焦点事件

c++ - 如何使用 initializer_list

c++ - C++ istream 中的行号?

c++ - 与设备串行对话的生产者/消费者

c++ - 在类中重载运算符

c++ - 使 visual studio 编译,以便最终用户不需要 c++ 运行时库

带有文件而不是用户输入的 C++ cin

C++11 std::function 比虚拟调用慢?

c++ - C++11的多线程模型