Creating a vector of strings : C++
<p>I am trying to create a vector which should input <code>strings (white space included)</code> until user enters <code>'!'</code>.</p>

<p>Below is my code :</p>

<pre><code>#include &lt;iostream&gt;
#include &lt;vector&gt;
#include &lt;string&gt;
#include &lt;iterator&gt;

using namespace std;


int main()
{

   char buffer[100];
   string temp;
   vector &lt;string&gt; vec;


    while(1)//Shows Segmentation fault error while running,
    //compiles succesfully
    {
    cout &lt;&lt; "Enter String : ";
    cin.get(buffer,100,'\n');//To ensure that whitespaces are also input
    cout &lt;&lt; "@@@@ " &lt;&lt; buffer &lt;&lt; endl ;//Shows correct input

    cin.ignore(std::numeric_limits&lt;std::streamsize&gt;::max(),'\n');
    //Clearing cin stream for any residual characters
    temp.assign(buffer);//Assigning string the input "phrase"
    cout &lt;&lt; "###" &lt;&lt; temp &lt;&lt; endl;//Shows correct output

    if (temp == "!") //if input is '!' character do not push on the vector a
    //come out of the loop
    break;

    vec.push_back(temp);//push on the vector
    temp.assign(NULL); //flush temp string

    }


  vector &lt;string&gt;::iterator p = vec.begin();
  //display thre vector
  while( p != vec.end())
   {
       cout &lt;&lt; *p &lt;&lt; endl;
       p++;
       }

    return 0;
    }
</code></pre>

<p>It compiles successfully but at run-time throws <code>Segmentation fault</code> error.</p>

<p>Not able to figure out why ? Can any one point out?</p>

<p>Also smarter solution for this, is appreciated along side pointing out what is wrong with my code.</p>

<p>Thanks</p>
