title: C++ check if file exists, if yes, change output
body: <p>I don't know if its relevant, but I'm using Windows 7 and Microsoft Visual Studio 2010.</p>
  
  <p>All I want to do, is ask the user for a filename, check if a file of that name exists, while it does exist, ask them for a different one</p>
  
  <p><strong>My Attempt</strong></p>
  
  <p><em>Main.cpp</em></p>
  
  <pre><code>std::cout &lt;&lt; std::endl &lt;&lt; "You must create a username" &lt;&lt; std::endl;
  std::cin &gt;&gt; username;
  user.checkFile(username);
  </code></pre>
  
  <p><em>User.cpp</em></p>
  
  <pre><code>void User::checkFile(std::string &amp;username)
  {
  
  std::ifstream fin (username + ".txt");
      while (fin.good)
      {
      std::cout &lt;&lt; "This username already exists, please choose another.";
      std::cin &gt;&gt; username;
       if (fin.bad)
          {
              break;
              fin.close();
          }
      }
  }
  </code></pre>
  
  <p>This correctly identifies if a file of that name exists, but then even when I type a name that doesn't exist, it still tells me it does exist</p>