title: FCFS CPU Scheduling program in C++
body: <p>I am trying to implement the FCFS CPU scheduling in c++. I have the following code which runs error free but gives me "Rubbish data" on some of the numbers such as the values of the Waiting time, response time and turn around time for all of the processes except the first one calculated</p>
  
  <pre><code>#include "stdafx.h"
  #include &lt;iostream&gt;
  #include &lt;string&gt;
  using namespace std;
  
  class ProcessInfo
  {
  public:
      string ProcessID;
      int BurstTime;
      int Arrival;
  
      int WaitingTime;
      int ResponseTime;
      int TurnaroundTime;
  };
  
  int main()
  {
      ProcessInfo P1, P2, P3 = ProcessInfo();
  
      ProcessInfo Array[3] = {P1, P2, P3};
  
      for(int i = 0; i &lt; 3; i++)
      {
          cout&lt;&lt;"Please enter Process ID for process "&lt;&lt;i&lt;&lt;endl;
          cin&gt;&gt;Array[i].ProcessID;
  
          cout&lt;&lt;"Please enter Burst Time for process "&lt;&lt;i&lt;&lt;endl;
          cin&gt;&gt;Array[i].BurstTime;
  
          cout&lt;&lt;"Please enter Arrival Time for process "&lt;&lt;i&lt;&lt;endl;
          cin&gt;&gt;Array[i].Arrival;
      }
  
      if (Array[0].Arrival &lt; Array[1].Arrival &amp;&amp; Array[0].Arrival &lt; Array[2].Arrival)
      {
          if (Array[1].Arrival &lt; Array[2].Arrival)
          {
              P1.WaitingTime = 0;
              P1.ResponseTime = 0;
              P1.TurnaroundTime = P1.BurstTime;
  
              P2.WaitingTime = P1.TurnaroundTime - (P2.Arrival - P1.Arrival);
              P2.ResponseTime = 0;
              P2.TurnaroundTime = P2.WaitingTime + P2.BurstTime;
  
              P3.WaitingTime = P2.TurnaroundTime - (P3.Arrival - P2.Arrival);
              P3.ResponseTime = P2.TurnaroundTime - (P3.Arrival - P2.Arrival) ;
              P3.TurnaroundTime = P3.WaitingTime + P3.BurstTime;
  
              cout&lt;&lt;"Order of processes is: P1, P2, P3"&lt;&lt;endl;
  
              cout&lt;&lt;"The waiting time for P1: "&lt;&lt; P1.WaitingTime&lt;&lt;endl;
              cout&lt;&lt;"The response time for P1: "&lt;&lt;P1.ResponseTime&lt;&lt;endl;
              cout&lt;&lt;"The Turnaround time for P1: "&lt;&lt;P1.TurnaroundTime&lt;&lt;endl;
  
              cout&lt;&lt;"The waiting time for P2: "&lt;&lt; P2.WaitingTime&lt;&lt;endl;
              cout&lt;&lt;"The response time for P2: "&lt;&lt;P2.ResponseTime&lt;&lt;endl;
              cout&lt;&lt;"The Turnaround time for P2: "&lt;&lt;P2.TurnaroundTime&lt;&lt;endl;
  
              cout&lt;&lt;"The waiting time for P3: "&lt;&lt; P3.WaitingTime&lt;&lt;endl;
              cout&lt;&lt;"The response time for P3: "&lt;&lt;P3.ResponseTime&lt;&lt;endl;
              cout&lt;&lt;"The Turnaround time for P3: "&lt;&lt;P3.TurnaroundTime&lt;&lt;endl;
          }
          else
              if (Array[2].Arrival &lt; Array[1].Arrival)
              {
                  P1.WaitingTime = 0;
                  P1.ResponseTime = 0;
                  P1.TurnaroundTime = P1.BurstTime;
  
                  P3.WaitingTime = P1.TurnaroundTime - (P3.Arrival - P1.Arrival);
                  P3.ResponseTime = P3.WaitingTime;
                  P3.TurnaroundTime = P3.WaitingTime + P3.BurstTime;
  
                  P2.WaitingTime = P3.TurnaroundTime - (P2.Arrival - P3.Arrival);
                  P2.ResponseTime = P2.WaitingTime;
                  P2.TurnaroundTime = P2.WaitingTime + P2.BurstTime;
  
                  cout&lt;&lt;"Order of processes is: P1, P3, P2"&lt;&lt;endl;
  
                  cout&lt;&lt;"The waiting time for P1: "&lt;&lt; P1.WaitingTime&lt;&lt;endl;
                  cout&lt;&lt;"The response time for P1: "&lt;&lt;P1.ResponseTime&lt;&lt;endl;
                  cout&lt;&lt;"The Turnaround time for P1: "&lt;&lt;P1.TurnaroundTime&lt;&lt;endl;
  
                  cout&lt;&lt;"The waiting time for P3: "&lt;&lt; P3.WaitingTime&lt;&lt;endl;
                  cout&lt;&lt;"The response time for P3: "&lt;&lt;P3.ResponseTime&lt;&lt;endl;
                  cout&lt;&lt;"The Turnaround time for P3: "&lt;&lt;P3.TurnaroundTime&lt;&lt;endl;
  
                  cout&lt;&lt;"The waiting time for P2: "&lt;&lt; P2.WaitingTime&lt;&lt;endl;
                  cout&lt;&lt;"The response time for P2: "&lt;&lt;P2.ResponseTime&lt;&lt;endl;
                  cout&lt;&lt;"The Turnaround time for P2: "&lt;&lt;P2.TurnaroundTime&lt;&lt;endl;
              }
      }
  
      else
          if(Array[1].Arrival &lt; Array[0].Arrival &amp;&amp; Array[1].Arrival &lt; Array[2].Arrival)
          {
              if (Array[2].Arrival &lt; Array[0].Arrival)
              {
                  P2.WaitingTime = 0;
                  P2.ResponseTime = 0;
                  P2.TurnaroundTime = P2.BurstTime;
  
                  P3.WaitingTime = P2.TurnaroundTime - (P3.Arrival - P2.Arrival);
                  P3.ResponseTime = P3.WaitingTime;
                  P3.TurnaroundTime = P3.WaitingTime + P3.BurstTime;
  
                  P1.WaitingTime = P3.TurnaroundTime - (P1.Arrival - P3.Arrival);
                  P1.ResponseTime = P1.WaitingTime;
                  P1.TurnaroundTime = P1.WaitingTime + P1.BurstTime;
  
                  cout&lt;&lt;"Order of processes is: P2, P3, P1"&lt;&lt;endl;
  
                  cout&lt;&lt;"The waiting time for P2: "&lt;&lt; P2.WaitingTime&lt;&lt;endl;
                  cout&lt;&lt;"The response time for P2: "&lt;&lt;P2.ResponseTime&lt;&lt;endl;
                  cout&lt;&lt;"The Turnaround time for P2: "&lt;&lt;P2.TurnaroundTime&lt;&lt;endl;
  
                  cout&lt;&lt;"The waiting time for P3: "&lt;&lt; P3.WaitingTime&lt;&lt;endl;
                  cout&lt;&lt;"The response time for P3: "&lt;&lt;P3.ResponseTime&lt;&lt;endl;
                  cout&lt;&lt;"The Turnaround time for P3: "&lt;&lt;P3.TurnaroundTime&lt;&lt;endl;
  
                  cout&lt;&lt;"The waiting time for P1: "&lt;&lt; P1.WaitingTime&lt;&lt;endl;
                  cout&lt;&lt;"The response time for P1: "&lt;&lt;P1.ResponseTime&lt;&lt;endl;
                  cout&lt;&lt;"The Turnaround time for P1: "&lt;&lt;P1.TurnaroundTime&lt;&lt;endl;
              }
              else
                  if (Array[0].Arrival &lt; Array[2].Arrival)
                  {
                      P2.WaitingTime = 0;
                      P2.ResponseTime = 0;
                      P2.TurnaroundTime = P2.BurstTime;
  
                      P1.WaitingTime = P2.TurnaroundTime - (P1.Arrival - P2.Arrival);
                      P1.ResponseTime = P1.WaitingTime;
                      P1.TurnaroundTime = P1.WaitingTime + P1.BurstTime;
  
                      P3.WaitingTime = P1.TurnaroundTime - (P3.Arrival - P1.Arrival);
                      P3.ResponseTime = P3.WaitingTime;
                      P3.TurnaroundTime = P3.WaitingTime + P3.BurstTime;
  
                      cout&lt;&lt;"Order of processes is: P2, P1, P3"&lt;&lt;endl;
  
                      cout&lt;&lt;"The waiting time for P2: "&lt;&lt; P2.WaitingTime&lt;&lt;endl;
                      cout&lt;&lt;"The response time for P2: "&lt;&lt;P2.ResponseTime&lt;&lt;endl;
                      cout&lt;&lt;"The Turnaround time for P2: "&lt;&lt;P2.TurnaroundTime&lt;&lt;endl;
  
                      cout&lt;&lt;"The waiting time for P1: "&lt;&lt; P1.WaitingTime&lt;&lt;endl;
                      cout&lt;&lt;"The response time for P1: "&lt;&lt;P1.ResponseTime&lt;&lt;endl;
                      cout&lt;&lt;"The Turnaround time for P1: "&lt;&lt;P1.TurnaroundTime&lt;&lt;endl;
  
                      cout&lt;&lt;"The waiting time for P3: "&lt;&lt; P3.WaitingTime&lt;&lt;endl;
                      cout&lt;&lt;"The response time for P3: "&lt;&lt;P3.ResponseTime&lt;&lt;endl;
                      cout&lt;&lt;"The Turnaround time for P3: "&lt;&lt;P3.TurnaroundTime&lt;&lt;endl;
  
                  }
          }
          else
  
              if(Array[2].Arrival &lt; Array[1].Arrival &amp;&amp; Array[2].Arrival &lt; Array[0].Arrival)
              {
                  if (Array[1].Arrival &lt; Array[0].Arrival)
                  {
                      P3.WaitingTime = 0;
                      P3.ResponseTime = 0;
                      P3.TurnaroundTime = P1.BurstTime;
  
                      P2.WaitingTime = P3.TurnaroundTime - (P2.Arrival - P3.Arrival);
                      P2.ResponseTime = P2.WaitingTime;
                      P2.TurnaroundTime = P2.WaitingTime + P2.BurstTime;
  
                      P1.WaitingTime = P2.TurnaroundTime - (P1.Arrival - P2.Arrival);
                      P1.ResponseTime = P1.WaitingTime;
                      P1.TurnaroundTime = P1.WaitingTime + P1.BurstTime;
  
                      cout&lt;&lt;"Order of processes is: P3, P2, P1"&lt;&lt;endl;
  
                      cout&lt;&lt;"The waiting time for P3: "&lt;&lt; P3.WaitingTime&lt;&lt;endl;
                      cout&lt;&lt;"The response time for P3: "&lt;&lt;P3.ResponseTime&lt;&lt;endl;
                      cout&lt;&lt;"The Turnaround time for P3: "&lt;&lt;P3.TurnaroundTime&lt;&lt;endl;
  
                      cout&lt;&lt;"The waiting time for P2: "&lt;&lt; P2.WaitingTime&lt;&lt;endl;
                      cout&lt;&lt;"The response time for P2: "&lt;&lt;P2.ResponseTime&lt;&lt;endl;
                      cout&lt;&lt;"The Turnaround time for P2: "&lt;&lt;P2.TurnaroundTime&lt;&lt;endl;
  
                      cout&lt;&lt;"The waiting time for P1: "&lt;&lt; P1.WaitingTime&lt;&lt;endl;
                      cout&lt;&lt;"The response time for P1: "&lt;&lt;P1.ResponseTime&lt;&lt;endl;
                      cout&lt;&lt;"The Turnaround time for P1: "&lt;&lt;P1.TurnaroundTime&lt;&lt;endl;
                  }
                  else
                      if (Array[0].Arrival &lt; Array[1].Arrival)
                      {
                          P3.WaitingTime = 0;
                          P3.ResponseTime = 0;
                          P3.TurnaroundTime = P1.BurstTime;
  
                          P1.WaitingTime = P3.TurnaroundTime - (P1.Arrival - P3.Arrival);
                          P1.ResponseTime = P1.WaitingTime;
                          P1.TurnaroundTime = P1.WaitingTime + P1.BurstTime;
  
                          P2.WaitingTime = P1.TurnaroundTime - (P2.Arrival - P1.Arrival);
                          P2.ResponseTime = P2.WaitingTime;
                          P2.TurnaroundTime = P2.WaitingTime + P2.BurstTime;
  
                          cout&lt;&lt;"Order of processes is: P3, P1, P2"&lt;&lt;endl;
  
                          cout&lt;&lt;"The waiting time for P3: "&lt;&lt; P3.WaitingTime&lt;&lt;endl;
                          cout&lt;&lt;"The response time for P3: "&lt;&lt;P3.ResponseTime&lt;&lt;endl;
                          cout&lt;&lt;"The Turnaround time for P3: "&lt;&lt;P3.TurnaroundTime&lt;&lt;endl;
  
                          cout&lt;&lt;"The waiting time for P1: "&lt;&lt; P1.WaitingTime&lt;&lt;endl;
                          cout&lt;&lt;"The response time for P1: "&lt;&lt;P1.ResponseTime&lt;&lt;endl;
                          cout&lt;&lt;"The Turnaround time for P1: "&lt;&lt;P1.TurnaroundTime&lt;&lt;endl;
  
                          cout&lt;&lt;"The waiting time for P2: "&lt;&lt; P2.WaitingTime&lt;&lt;endl;
                          cout&lt;&lt;"The response time for P2: "&lt;&lt;P2.ResponseTime&lt;&lt;endl;
                          cout&lt;&lt;"The Turnaround time for P2: "&lt;&lt;P2.TurnaroundTime&lt;&lt;endl;
                      }
              }
  
  
  
      }
  </code></pre>