title: Access Violation Writing Location in my simple c++ code
body: <p>This is my code:</p>
  
  <pre><code>#include &lt;iostream&gt;
  #include &lt;fstream&gt;
  #include &lt;string&gt;
  
  
  using namespace std;
  
  struct car
  {
      string name, model;
      int year;   
  };
  
  void search_car(int CarYear)
  {
      cout&lt;&lt;"1";
      ifstream in;
      cout&lt;&lt;"2";
      car c1;
      cout&lt;&lt;"3";
      in.open("Cars.txt",ios::binary|ios::in);
      cout&lt;&lt;"4"&lt;&lt;endl;
      while(!in.eof())
      {
          cout&lt;&lt;" 5";
          in.read((char *) &amp;c1, sizeof(car));
          cout&lt;&lt;" 6.Car Year: "&lt;&lt;c1.year&lt;&lt;endl;
          if(c1.year == CarYear)
          {
              cout&lt;&lt;" 7&gt;&gt;&gt; ";
              cout&lt;&lt;c1.name&lt;&lt;" "&lt;&lt;c1.model&lt;&lt;" "&lt;&lt;c1.year;
              cout&lt;&lt;" &lt;&lt;&lt;8"&lt;&lt;endl;
          }
      }
      cout&lt;&lt;" 9";
      in.close();
      cout&lt;&lt;" 10";    
  }
  
  void main()
  {
      car c[100];
      int carNum, menuAct = 0, CarYear = -1, cycle = 1;
      ofstream out;
      while (cycle == 1)
      {
          //clrscr();
          cout&lt;&lt;endl&lt;&lt;endl&lt;&lt;"1.Enter New car"&lt;&lt;endl&lt;&lt;"2.Search"&lt;&lt;endl&lt;&lt;"3.Exit"&lt;&lt;endl;
          cin&gt;&gt;menuAct;
          cout&lt;&lt;"   Menu Action: "&lt;&lt;menuAct&lt;&lt;endl;
          if(menuAct == 1)
          {
              cout&lt;&lt;"Enter Num OF Cars: ";
              cin&gt;&gt;carNum;
              out.open("Cars.txt",ios::binary|ios::out|ios::app);
              for(int i = 0; i &lt; carNum; i++)
              {
                  cout&lt;&lt;"Enter Name OF Car: ";
                  cin&gt;&gt;c[i].name;
                  cout&lt;&lt;"Enter model OF Car: ";
                  cin&gt;&gt;c[i].model;
                  cout&lt;&lt;"Enter year OF Car: ";
                  cin&gt;&gt;c[i].year;     
                  out.write((char *) &amp;c[i], sizeof(car));
              }
              out.close();
          }
          else if(menuAct == 2)
          {
              cout&lt;&lt;"Enter Car Year: ";
              cin&gt;&gt;CarYear;
              cout&lt;&lt;" 0";
              //cout&lt;&lt;" Y: "&lt;&lt;CarYear;
              search_car(CarYear);
              cout&lt;&lt;" 11";
              //menuAct = 0;
          }
          else if(menuAct == 3)
          {
              cycle = 0;
          }
      }   
  }
  </code></pre>
  
  <p>Error:</p>
  
  <p><a href="http://s3.picofile.com/file/7580464836/cpp_err11.jpg" rel="nofollow">http://s3.picofile.com/file/7580464836/cpp_err11.jpg</a></p>
  
  <p><strong>What is happened?</strong> 
  I`m used some cout to trace what is happening and code is stopped at  number 10.</p>
  
  <p>Also last car is printed twice!!!</p>