How can I fix the infinite loop for input incorrect .txt file

Hello, I am making a project and this part where I need to ask the user to input the .txt file name and read each words to array. but before that when I enter the correct file name there's nothing wrong.**but if I enter incorrect filename and re-enter a correct file name but it won't continue and keeps asking to enter the correct file name

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
// this is function
  string read_file(string& fileName) //option2
{
        //ofstream outData;
        cin.clear();
        cout << "Enter the data file Name : " << endl;//asks user to input filename
        cin >> fileName; //inputs user input into fileName
        fileName = fileName+".txt";
        cout << "reading file now : "<< fileName <<endl;


        return fileName;
}

//this is in main();
case 2:
{       system("cls");
        read_file(fileName);
        inData.open(fileName.c_str());// opens the file with the users input
        //outData.open(fileName.c_str());



        while(inData.fail())
    {
            inData.clear();
            cout<<"Incorrect filename, please enter again";
            cin>>fileName;

            inData.open(fileName.c_str());
    }
        while (!inData.eof() && !inData.fail() && i<100)
    {
            string filewords[100];
            for (int i=0;i<100;i++)
            {
                inData>>filewords[i];
            }
    }


Please help me fix this. Thank you.
Topic archived. No new replies allowed.