title: Restaurant Queue Simulation marking error in function
body: <p>I'm creating a restaurant simulation for my data structures class using a queue, and have a good idea for how to do this, but my program keeps throwing an error and marking my function that increments the "wait time" variable of all "customers" in the queue. </p>
  
  <p>Sometimes my program will run all the way through and generate a value for the average wait time, and other times it will break after the first or second customer is generated. I thought that my conditionals that I made my <code>incrementWaitTime</code> function with would prevent it from reading from a NULL memory location, but apparently they haven't. Could someone glance over my source code and help me figure out what's broken? </p>
  
  <p>Edit: Here's the exact error that my program is throwing:</p>
  
  <blockquote>
    <p>"Unhandled exception at 0x010419aa in Homework 2 Restaurant Queue.exe: 0xC0000005: Access violation reading location 0x00000018."</p>
  </blockquote>
  
  <pre><code>#include &lt;iostream&gt;
  #include &lt;time.h&gt;
  #include &lt;stdlib.h&gt;
  using namespace std;
  
  class Customer{
  public:
      int WaitTime, ServiceTime, OrderCounter, ServiceCounter, OrderTime, test;
      Customer *next;
  
      Customer(){
          test = 0;
          OrderCounter = 0;
          WaitTime = 0;
          ServiceTime = 0;
          OrderTime = 0;
          next = NULL;
      }
  };
  
  class Queue{
  public:
      Customer *top, *bottom;
      int totalWaitTime, totalCustomers; 
      Queue(){
          bottom = NULL;
          top = NULL;
          totalWaitTime = 0;
          totalCustomers = 0;
      }
  
  void incrementWaitTime(){
      Customer *temp;
      temp = top;
      if(top == NULL){
          return;
      }
      else{
          if(temp-&gt;next == bottom){
              temp-&gt;WaitTime++;
          }
          else{
              while(temp-&gt;next != bottom){
              temp-&gt;WaitTime = temp-&gt;WaitTime + 1;
              temp = temp-&gt;next;
              }
          }
      }
  }
  
  void displayContents(){
      Customer *temp;
      temp = top;
      while(temp!= NULL){
          cout &lt;&lt; temp-&gt;test &lt;&lt; "----&gt;"; 
          temp = temp-&gt;next;
      }
      cout &lt;&lt; endl;
  }
  
  void newCustomer(int x){
      Customer *temp = new Customer;
      temp-&gt;OrderTime = x;
      if(top == NULL){ //No customers in line
          top = bottom = temp;
          totalCustomers++;
          cout &lt;&lt; "There is a new customer." &lt;&lt; endl;
      }
      else{
          temp-&gt;next = top;
          top = temp;
          totalCustomers++;
          cout &lt;&lt; "There is a new customer." &lt;&lt; endl;
      }
  }
  
  void removeCustomer(){
      Customer *chase, *follow;
      chase = follow = top;
      if(top == NULL){
          //No customers in queue.
          cout &lt;&lt; "No customers are in line.. there's nothing to remove." &lt;&lt; endl;
          return;
      }
      else{
          if(top-&gt;next == NULL){
              //Only one customer
              delete top;
              top = NULL;
              bottom = NULL;
              return;
          }
          while(chase-&gt;next != NULL){
              follow = chase;
              chase = chase-&gt;next;
          } 
          delete chase;
          bottom = follow;
          follow-&gt;next = NULL;
      }
  }
  
  void checkStatus(){
      if(top == NULL){
          bottom = NULL;
          return;
      }
      else if(bottom-&gt;OrderCounter != bottom-&gt;OrderTime){
          bottom-&gt;OrderCounter++;
          bottom-&gt;WaitTime++;
      }
      else{
          totalWaitTime = totalWaitTime + bottom-&gt;WaitTime;
          removeCustomer();
      }
  }
  };
  
  
  
  int main(){
  Queue Restaurant;
  int Clock = 1, totalCustomers = 0;
  float probArrival = 0;
  srand(time(NULL)); //seeds random number generator
  int number, orderTime, TotalWaitTime, TotalServiceTime;
  
  while(Clock &lt; 1140){
      while(Clock &lt; 120){
          number = rand();
          number = number%10+1; //Generates a number between 1 and 10
          if(number &gt;=1 &amp;&amp; number &lt;= 3){
              orderTime = rand();
              orderTime = orderTime%6 + 1; //Creates orderTime between 1 and 6
              Restaurant.newCustomer(orderTime);
              cout &lt;&lt; "The time is: " &lt;&lt; Clock &lt;&lt; " minutes." &lt;&lt; endl;
              Clock++;
          }
          else{
              Restaurant.checkStatus();
              Restaurant.incrementWaitTime();
              cout &lt;&lt; "The time is: " &lt;&lt; Clock &lt;&lt; " minutes." &lt;&lt; endl;
              Clock++;
          }
      }
      Clock = 1140;
  }
  cout &lt;&lt; "There were " &lt;&lt; Restaurant.totalCustomers &lt;&lt; " customers. " &lt;&lt; endl;
  cout &lt;&lt; "Average wait time was: " &lt;&lt; Restaurant.totalWaitTime / Restaurant.totalCustomers &lt;&lt; " minutes per customer." &lt;&lt; endl;
  
  
  system("pause");
  return 1;
  }
  </code></pre>