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

I get an infinite loop and i am not sure why or how to fix it

本文关键字:不确定 为什么 何修复 循环 无限 一个      更新时间:2023-10-16
ifstream olympicsStream;
        olympicsStream.open("olycs105.txt");

这是打开文件的地方

        olympicsStream >> firstcountry; // string variable for countries
        while (firstcountry!="END_OF_FILE")//

无法弄清楚在这里为条件放置什么

        {
            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];
        }

循环5行需要,但无限地使用相同的行,而不是通过文件的其余部分读取

您忘了在循环结束时更新变量'firstCountry'。

只需在关闭WILE循环之前添加olympicsStream >> firstcountry;,并且您的代码应正常运行

假设文件中的最后一个单词是" end_of_file"