title: segmentation fault on bool variable if statement c++
body: <p>I have a function which returns a bool variable. Then I used 2 if statements to print something depending on if the bool variable is true or false. The system prints out segmentation fault instead which is strange since I declared the variable at the beggining as false, so it would make more sense that the program always printed the false statement. The only thing I can think of is that maybe I write the if statement incorrectly?? is there a certain way to write an if statement for a boolean variable?</p>
  
  <p>Here is the input </p>
  
  <p>Please enter name of the input file: 40.txt
  Please enter the prefered sum 1000</p>
  
  <p>Here is the output</p>
  
  <p>There were 1600 checks made to find the products of your desired sum and
  Segmentation fault (core dumped)</p>
  
  <p>Here are the if statements but I will add the whole code below</p>
  
  <pre><code>if(!found)
  {
      cout &lt;&lt; " the program did not find any matches. try again. ";
  }
  if(found)
  {
  cout &lt;&lt; "the two numbers that add to your sum are " &lt;&lt; n1 &lt;&lt; " and " &lt;&lt; n2 &lt;&lt; ". These were found in position " &lt;&lt; pos1 &lt;&lt; " and " &lt;&lt; pos2;
  }
  </code></pre>
  
  <p>Here's the whole code</p>
  
  <pre><code>#include &lt;iostream&gt;
  #include &lt;fstream&gt;
  using namespace std;
  
  int x, i, n1, n2, pos1 = 0, pos2 = 0, accesses = 0;
  
  int readSortedArray(int array[20], int count, istream&amp; infile)
  {
      count = 0;
      while (infile &gt;&gt; array[count])
      {
          count++;
  
      }
  
      return count;
  }
  
  bool findproducts(int array[20],
                    int&amp; n1,
                    int&amp; n2,
                    int count,
                    int&amp; accesses,
                    bool found,
                    int&amp; pos1,
                    int&amp; pos2)
  {
      int sum;
      cout &lt;&lt; "Please enter the prefered sum ";
      cin &gt;&gt; sum;
      for (i = 0; i &lt; count; i++)
      {
          pos1++;
          n1 = array[i];
          pos2 = 0;
          for (x = 0; x &lt; count; x++)
  
          {
              pos2++;
              accesses++;
              n2 = array[x];
              if (sum == n1 + n2)
              {
                  found = true;
                  i = count;
                  x = count;
                  return true;
              }
  
          }
      }
  }
  
  int main()
  {
      int array[20];
      int answer, count = 0;
      bool found = false;
  
      std::string input_filename;
      ifstream infile;
  
      cout &lt;&lt; "Please enter name of the input file: ";
      cin &gt;&gt; input_filename;
      infile.open(input_filename.c_str());
      if (!infile)
      {
          cout &lt;&lt; "Could not open input file one\n";
          return 0;
      }
  
      count = readSortedArray(array, count, infile);
  
      found = findproducts(array, n1, n2, count, accesses, found, pos1, pos2);
  
      cout &lt;&lt; "There were " &lt;&lt; accesses &lt;&lt; " checks made to find the products of your desired sum and" &lt;&lt; endl;
  
      if (!found)
      {
          cout &lt;&lt; " the program did not find any matches. try again. ";
      }
      if (found)
      {
          cout &lt;&lt; "the two numbers that add to your sum are " &lt;&lt; n1 &lt;&lt; " and " &lt;&lt; n2 &lt;&lt; ". These were found in position " &lt;&lt; pos1 &lt;&lt; " and " &lt;&lt; pos2;
      }
  
      return 0;
  }
  </code></pre>