whouton 0 Newbie Poster

Hi guys,

I'm trying to make a function that allows the user to enter in a file name and access that file; if the file name is wrong or the file is not there, I want to allow the user to enter in another file name. For some reason the while loop repeats infinitely even when correct file names are given. I have tried clearing the input stream and closing the file within the loop, but neither action solved the problem.
My code:

#include <iostream>
#include <iomanip>
#include <fstream>

using namespace std;


//Function Prototypes
void validateFile(ifstream& inputFile, char fileName[]);

int main()
{  
    char *name;
    name = new char[100];
    
    ifstream infile;
    ofstream outfile;
    
    cout << "Please enter the file name." << endl;
    cin >> name;
            
    validateFile(infile, name);
    
    system("pause");
    return 0;
}

//*****************************
//validateFile ensures that the file is valid
//*****************************
void validateFile(ifstream& inputFile, char fileName[])
{
    bool valid;
    
    inputFile.open(fileName); 
     
    if(!inputFile)
    {         
         valid = false;
     
         while (valid == false)
         {
               inputFile.close();
                
               cout << "File name: " << fileName << " is invalid" << endl;
               cout << "Please enter a valid file name." << endl;
               cin >> fileName;
               
               inputFile.open(fileName);
               
              if(!inputFile)
                valid = false;
              else
                 valid = true;
         }                
    }
    cout << "The file " << fileName << " is valid and open." << endl;
}