title: Seg Fault on cin. Tried the endl thing. Lousy at gdb
body: <p>I getting a strange segfault I don't understand.  </p>
  
  <pre><code>will@will-mint ~/code/byun-sp15 $ g++ -g all_pairs.cpp 
  will@will-mint ~/code/byun-sp15 $ ./a.out
  Please enter filename: 
  table.txt
  Segmentation fault
  </code></pre>
  
  <p>As you can see from the cout staements, it is segfaulting right in <code>cin&gt;&gt;filename;</code>  Here's the main function:</p>
  
  <pre><code>#include &lt;iostream&gt;
  #include &lt;fstream&gt;
  #include &lt;vector&gt;
  #include &lt;sstream&gt;
  
  using std::cin;
  using std::cout;
  using std::endl;
  using std::string;
  using std::ifstream;
  using std::vector;
  
  int main()
  {
          int n;
  
          string filename;
          cout &lt;&lt; "Please enter filename: " &lt;&lt; endl;
          cin &gt;&gt; filename;
          cout &lt;&lt; "FLAG FLAG FLAG";
  
          ifstream fin(filename.c_str());
          if (!fin.is_open()) {
                  cout &lt;&lt; "ERROR: file not found.  Exiting..." &lt;&lt; endl;
                  return -1;
          }
  
          fin &gt;&gt; n;
          vector&lt;Table&gt; tables;
          int temp;
          for (int i = 0; i &lt; n; i++) {
                  for (int j = 0; j &lt; n; j++) {
                          fin &gt;&gt; temp;
                          tables[0].data[i][j] = temp;
                  }
          }
  
          for (int i = 1; i &lt; n-1; i++) {
                  tables.push_back(calc_next_table(tables, i, n));
          }
  
          return 0;
  }
  </code></pre>
  
  <p>I really feel like a moron, I have never come across this before.  I found <a href="https://stackoverflow.com/questions/16227680/why-am-i-getting-a-segfault-on-cin">this</a>, but it didn't solve it.  I even tried gdb (which I don't really know how to use) and all I got was this:</p>
  
  <pre><code>Program received signal SIGSEGV, Segmentation fault.
  0x0000000000401ae8 in std::vector&lt;std::vector&lt;int, std::allocator&lt;int&gt; &gt;, std::allocator&lt;std::vector&lt;int, std::allocator&lt;int&gt; &gt; &gt; &gt;::operator[] (this=0x0, __n=0) at /usr/include/c++/4.8/bits/stl_vector.h:771
  771       { return *(this-&gt;_M_impl._M_start + __n); }
  (gdb) list
  766        *  out_of_range lookups are not defined. (For checked lookups
  767        *  see at().)
  768        */
  769       reference
  770       operator[](size_type __n)
  771       { return *(this-&gt;_M_impl._M_start + __n); }
  772 
  773       /**
  774        *  @brief  Subscript access to the data contained in the %vector.
  775        *  @param __n The index of the element for which data should be
  </code></pre>
  
  <p>This looks like a vector is doing something funky, but I haven't even declared my vectors yet!  Any ideas?</p>
  
  <p>EDIT:  I like @neilson's answer, but the following is still throwing segfault:</p>
  
  <pre><code>        fin &gt;&gt; n;
          vector&lt;Table&gt; tables;
          Table t;
          tables.push_back(t);
  
          int temp;
          for (int i = 0; i &lt; n; i++) {
                  for (int j = 0; j &lt; n; j++) {
                          fin &gt;&gt; temp;
                          tables[0].data[i][j] = temp;
                  }
          }
  
          cout &lt;&lt; "FLAG FLAG FLAG" &lt;&lt; endl;
  </code></pre>
  
  <p>Table has one element, a <code>vector&lt;vector&lt;int&gt;&gt;</code> called <code>data</code>.  Should I post that as well?</p>