title: C++ Sequential search not finding the last element
body: <p>So I have an input file. It consists of 40 numbers. The first 20 numbers are input into an array (I've checked this, they're actually there). I then close and re-open the input file. I compare the first 20 numbers in the input file against my array using sequential search. This means they should all be successful. I then compare the next 20 numbers against the numbers in my array, they should all be unsuccessful searches. My array is unsorted at this point.</p>
  
  <p>The problem I'm running into is that the last number for successful is never found using sequential. I'm not sure how to fix this.</p>
  
  <p>Here is the sequential search function: </p>
  
  <pre><code>length = 19;
  
  void Search::sequential(ItemType item, bool&amp; found)
  { 
    int place = 0;
    while (place &lt; length &amp;&amp; item != list[place])
      place++;
    found = (place &lt; length); 
  }
  </code></pre>
  
  <p>And here is my successful/unsuccessful loops</p>
  
  <pre><code>outFile &lt;&lt; "\n\n ************Sequential Successful ********** \n";
  outFile &lt;&lt; endl &lt;&lt; "ID" &lt;&lt; endl;
  
  inFile &gt;&gt; num;
  for(int i=0; i&lt;=length &amp;&amp; inFile; i++)
  {
    search.sequential(num, found);
    if (found)
      outFile &lt;&lt; num &lt;&lt; endl; 
  
    inFile &gt;&gt; num;
  } 
  
  
  //sequential unsuccessful
  outFile &lt;&lt; "\n\n ************Sequential unsuccessful ********** \n";
  outFile &lt;&lt; endl &lt;&lt; "ID" &lt;&lt; endl;
  
  for(int i=0; i&lt;=length &amp;&amp; inFile; i++)
  {
    search.sequential(num, found);
    if (!found)
      outFile &lt;&lt; num &lt;&lt; endl; 
  
    inFile &gt;&gt; num;
  }
  </code></pre>
  
  <p>However, my output is: </p>
  
  <pre><code> ************Sequential Successful ********** 
  
   ID
   1111
   3352
   4567
   5678
   6789
   7890
   8901
   9012
   1223
   2113
   8546
   2374
   4723
   9573
   3284
   7474
   8594
   3589
   5858
   //THERE SHOULD BE 1925 HERE BUT THERE ISN'T
  
    ************Sequential unsuccessful ********** 
  
   ID
   9456
   3584
   2222
   4319
   4477
   5710
   5497
   1502
   1599
   1504
   1506
   9943
   8833
   9944
   6678
   5555
   5660
   9911
   6130
   1613
  </code></pre>
  
  <p>If I remove the "if (found)" statement everything works perfectly, but how do I get around this without removing that?</p>
  
  <p>Thanks in advance</p>
  
  <p>---------------edit---------------</p>
  
  <p>Okay, when I changed length to 20 it still didn't seem to work. I'm so lost.</p>
  
  <p>Here is where I create the array</p>
  
  <pre><code>inFile &gt;&gt; num;
  for (int i=0; i&lt;length &amp;&amp; inFile; i++)
  {
    search.addToList(num);
    inFile &gt;&gt; num;
  }
  </code></pre>
  
  <p>and here is the addToList function</p>
  
  <pre><code> void Search::addToList(ItemType num)
   {
     if (index &lt; length)  //ive tried taking out this statement just to see if it makes a difference and it didn't
     {
       list[index] = num;
       index++;
     }
   }
  </code></pre>
  
  <p>I initialize index to 0 in the constructor</p>
  
  <p>This is how I declare the array</p>
  
  <pre><code>    ItemType list[length]; 
  </code></pre>
  
  <p>IT WORKS!!!! Thank you all SO much! I really appreciate it so much.</p>