user9037468
user9037468

Reputation:

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

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

Upvotes: 0

Views: 72

Answers (1)

penguin2048
penguin2048

Reputation: 1343

You forgot to update the variable 'firstcountry' at the end of while loop.

Just add olympicsStream >> firstcountry; before closing the while loop and your code should run fine

Assuming the last word in the file is "END_OF_FILE"

Upvotes: 3

Related Questions