title: C++. how to read from file and match with input
body: <p>I am trying to read a textfile and matching the fruit to what I have type(e.g I type apple and it will search the textfile for the word apple and match it and output that it's been found) but I am struggling to achieve the results I wanted and thus need help in it.</p>
  
  <p>I have a text file (fruit.txt) with contents shown below</p>
  
  <p>apple,30</p>
  
  <p>banana,20</p>
  
  <p>pear,10 </p>
  
  <hr>
  
  <p>this is my code</p>
  
  <pre><code>string fruit;
  string amount;
  string line = " ";
  ifstream readFile("fruit.txt");
  fstream fin;
  
  cout &lt;&lt; "Enter A fruit: ";
  cin &gt;&gt; fruit;
  
  fin &gt;&gt; fruit &gt;&gt; amount;
  while (getline(readFile, line,','))
      {
          if(fruit != line) {
              cout &lt;&lt;"the fruit that you type is not found.";
          }
  
         else {
             cout &lt;&lt;"fruit found! "&lt;&lt; fruit;
         }
  }
  </code></pre>
  
  <p>please advise 
  thanks.</p>