title: fstream is writing on file against an if else statement
body: <p>I have a file named &quot;password.txt&quot; which has 2 rows of usernames and passwords</p>
  <p>2 string vectors named, user &amp; password</p>
  <p>This function uses a new username and password and checks if the username already exists in the user vector using a for loop</p>
  <p>If it does exist, it breaks out of the loop
  If it does not exist, it will add the username and password to the vectors and write them both on the file</p>
  <pre><code>void PasswordFile::addpw(string newuser, string newpassword) {
      fstream file;
      file.open(&quot;password.txt&quot;, ios::app);
      
      //if the vectors is empty it automatically adds a new user
      //is working
      if (user.size() &lt; 1){
          user.push_back(newuser);
          password.push_back(newpassword);
          file &lt;&lt; newuser &lt;&lt; &quot; &quot; &lt;&lt; newpassword &lt;&lt; &quot;\r&quot;;
      }
  
      //the loop (is not working)
  
      for (int i=0; i &lt; user.size(); i++){
          string name = user[i];
          if (name == newuser) {
              break;
          } //it shouldn't continue past this point if the name is in the vector but it does
  
          else {
              cout &lt;&lt; newuser;
              cout &lt;&lt; &quot; is being written onto the file\n&quot;;
              user.push_back(newuser);
              password.push_back(newpassword);
              file &lt;&lt; newuser &lt;&lt; &quot; &quot; &lt;&lt; newpassword &lt;&lt; &quot;\r&quot;;
          }
      }
      
      file.close();
  }
  </code></pre>
  <p>the problem is that it is writing the second name twice and the third name three times</p>
  <p>I know also that it is pushing the newuser and newpassword multiple times onto the vector, but I can't figure out where or why its looping more than it should or why its ignoring the break and writing onto the file anyway</p>
  <p>&quot;password.txt&quot; 1 run</p>
  <pre><code>dbotting 123qwe
  egomez qwerty
  tongyu liberty
  tongyu liberty
  </code></pre>
  <p>&quot;password.txt&quot; second run</p>
  <pre><code>dbotting 123qwe
  egomez qwerty
  tongyu liberty
  tongyu liberty
  egomez qwerty
  tongyu liberty
  tongyu liberty
  </code></pre>
  <p>it keeps adding the last 2 every time its run</p>