title: Asking for an input file until it is correct in c++
body: <p>I am trying to read a file in c++. I ask the filename to user, if file exists i open it but if it does not exist i keep asking until a valid filename is entered. But when user enters a wrong file first, even though it enters a valid filename after that my program recognizes it as false. Here is my code:</p>
  
  <pre><code>ifstream input;
  string filename;
  cout&lt;&lt;"Enter the file name";
  cin&gt;&gt;filename;
  input.open(filename.c_str());
  while(input.fail())
  {
      cout&lt;&lt;"Incorrect filename, please enter again";
      cin&gt;&gt;filename;
      input.open(filename.c_str());
  }
  </code></pre>
  
  <p>Can anyone help? Thanks</p>