title: C++ - run-time crashing of linear probing program
body: <p>I wrote a program in C++ for hashing using linear probing. The code is showing no error at the time of compilation but when I run it, computer shows the notification that the program has stopped working. I am giving the entire code below. Please help me out. </p>
  
  <pre><code>#include&lt;iostream&gt;
  #include&lt;vector&gt;
  using namespace std;
  class Acc
  {
      public:
          int iData;
          double dData;
          Acc(int id,double dd)
          {
              iData = id;
              dData = dd;
          }
          void displayAcc()
          {
              cout&lt;&lt;"iData = "&lt;&lt;iData&lt;&lt;"\n";
              cout&lt;&lt;"dData = "&lt;&lt;dData&lt;&lt;"\n";
          }
  };
  class Linear_Hash
  {
      private:
          vector&lt;Acc*&gt; hashArray;
          int nElem;
          Acc* noElem;
      public:
          Linear_Hash(int max)
          {
              nElem = max;
              hashArray.resize(nElem);
              noElem = new Acc(-1,1.1);
              for(int i = 0;i&lt;max;i++)
              {
                  hashArray[i] = NULL;
              }
          }
          int hashfunc(int key)
          {
              return key%nElem;
          }
          void insertAcc(int id,double dd)
          {
              Acc* newacc = new Acc(id,dd);
              int hashVal = hashfunc(id);
              while(hashArray[hashVal]-&gt;iData!=-1&amp;&amp;hashArray[hashVal]!=NULL)
              {
                  hashVal++;
                  hashVal = hashVal%nElem;
              }
              hashArray[hashVal] = newacc;
          }
          Acc* search(int key)
          {
              int hashVal = key%nElem;
              while(hashArray[hashVal]-&gt;iData!=key&amp;&amp;hashArray[hashVal]!=NULL)
              {
                  hashVal++;
                  hashVal = hashVal%nElem;
              }
              if(hashArray[hashVal]-&gt;iData==key)
              {
                  return hashArray[hashVal];
              }
              else
                  return NULL;
          }
          bool deleteAcc(int key)
          {
              int hashVal = hashfunc(key);
              while(hashArray[hashVal]-&gt;iData!=key&amp;&amp;hashArray[hashVal]!=NULL)
              {
                  hashVal++;
                  hashVal = hashVal%nElem;
              }
              if(hashArray[hashVal]==NULL)
                  return false;
              else
              {
                  Acc* pTemp = hashArray[hashVal];
                  hashArray[hashVal] = noElem;
                  delete pTemp;
                  return true;
              }
          }
  };
  int main(void)
  {
      int key;
      char val;
      Linear_Hash lh(20);
      lh.insertAcc(100,100.1);
      lh.insertAcc(204,204.204);
      lh.insertAcc(105,105.10);
      lh.insertAcc(237,348.23);
      lh.insertAcc(209,923.23);
      lh.insertAcc(230,230.23);
      lh.insertAcc(403,348.34);
      lh.insertAcc(405,938.50);
      lh.insertAcc(450,348.23);
      lh.insertAcc(945,495.409);
      while(val!='x')
      {
          cout&lt;&lt;"Enter the key to be searched\n";
          cin&gt;&gt;key;
          if(lh.search(key)==NULL)
              cout&lt;&lt;key&lt;&lt;" could not be found\n";
          else
              lh.search(key)-&gt;displayAcc();
          cout&lt;&lt;"Enter the key to be deleted\n";
          cin&gt;&gt;key;
          if(lh.deleteAcc(key))
              cout&lt;&lt;key&lt;&lt;" has been deleted\n";
          else
              cout&lt;&lt;"Invalid request\n";
          cout&lt;&lt;"Do you want to continue\n";
          cin&gt;&gt;val;
      }
      return 0;
  }
  </code></pre>
  
  <p>I am unable to use the debugger in this case as I don't know where the error is. I have also tried dry-running it on paper but wasn't able to pin point the bug. </p>