title: Validating username and password in a seperate txt file
body: <p>My program asks for id and pw and is supposed to check it against an id and pw in a txt file but I'm not sure what's going wrong. the getLogin function works fine, I think the issue might be in the validateLogin function or showResult function.</p>
  <p>The txt file reads:
  tommy (enter)
  12345</p>
  <p>When I enter the same id and pw and go through the program I get the &quot;Invalid&quot; output from showResult.
  Any Suggestion?</p>
  <pre><code>#include &lt;iostream&gt;
  #include &lt;fstream&gt;
  using namespace std;
  
  void getLogin(string &amp; id, string &amp; pw);
  bool validateLogin(string id, string pw);
  void showResult(bool result);
  
  int main()
  {
      string id;
      string pw;
      bool result;
  
      result = validateLogin(id, pw);
  
      getLogin(id, pw);
      validateLogin(id, pw);
      showResult(result);
  
      return 0;
  }
  
  void getLogin(string&amp; id, string&amp; pw)
  {
      cout &lt;&lt; &quot;Enter your ID:  &quot;;
      cin &gt;&gt; id;
      cout &lt;&lt; &quot;Enter your password:  &quot;;
      cin &gt;&gt; pw;
  }
  
  bool validateLogin(string id, string pw)
  {
      string str1;
      string str2;
      bool result;
      ifstream inFile;
  
      inFile.open(&quot;c:\\login.txt&quot;);
  
      if (inFile.fail())
      {
          cout &lt;&lt; &quot;File failed to open.&quot;;
      }
  
      inFile &gt;&gt; str1 &gt;&gt; str2;
  
      inFile.close();
  
      if ((str1 == id) &amp;&amp; (str2 == pw))
      {
          result = true;
      }
  
      else
      {
          result = false;
      }
  
      return result;
  }
  
  void showResult(bool result)
  {
      if (result == true)
      {
          cout &lt;&lt; &quot;Valid.&quot;;
      }
  
      else
      {
          cout &lt;&lt; &quot;Invalid.&quot;;
      }
  
  }
  </code></pre>