hard logic error in queue manipulation..help!

I want to read from the text file and want to add and remove the data in queue depending on arrival time and total time to server. I can do this for some only but the last values are not being calculated.

My file:
ID--arrival time--time given to customer
1
2
3
4
5
1 1.3 0.10
2 0.9 2
3 2.0 0.5
4 5.0 1.5
5 1.5 2

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
My header file
#include <queue>

struct Customer {

    Customer(){
        id=0;
        arrival_time=0.0;
        serving_time=0.0;
        waiting_time=0.0;
    }
    int id;
    float arrival_time;
    float serving_time;
    float waiting_time;//Service end time of previous job - arrival time of this job

};

struct Server {

    Server (){
        total_time_to_serve=0.0;
        serverBusy=0;
    }

    float total_time_to_serve;//arrival time + serving time
    int serverBusy;

}SVR;



//initialize simulation : initialize all variables

//declare processArrival()
int processArrival(std::queue<Customer>&,std::queue<Customer>&, Customer&,Server&);


//define processServiceCMP
int processServiceCMP(std::queue<Customer>&cstQ1,std::queue<Customer>&cstQ2,Customer& cst,Server&srv);


//readData() : will get the next service time and arrival time
int readData(std::ifstream&, Customer&);


My implementation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108

#include<iostream>
#include <cstdio>
#include <fstream>
#include <queue>
#include "Servers.h"
using namespace std;
//define all functions
int main (){

   queue<Customer> custQ1;//queue1 of customers
   queue<Customer> custQ2;//queue2 of customers
   Customer cust;//a customer
   Server server;//servers
   ifstream rFile;
   int custInfo[3]={0};
   Server previous;

    rFile.open("C:\\Users\\rahman-pc\\Desktop\\arrivals.txt");

    do/*(!rFile.eof() && server.serverBusy==0)*/{
        readData(rFile,cust);
        cout<<"ID "<<cust.id<<endl;
        cout<<"Service customer "<<cust.id<<" \n"<<"Qsize "<<custQ1.size()<<"\n"<<"total service time? "<<server.total_time_to_serve<<endl;

        if(server.serverBusy==1){
           if(server.total_time_to_serve<cust.arrival_time){

                processServiceCMP(custQ1,custQ2,cust,server);


        }
          else{
                processArrival(custQ1,custQ2,cust,server);

        }
        }
        else{
            //process arrival
            processArrival(custQ1,custQ2,cust,server);
            }




    }while(!rFile.eof()&& server.serverBusy==1);

    rFile.close();
    return 0;
}


//define processArrivale()
int processArrival(queue<Customer>& cstQ1,std::queue<Customer>&cstQ2,Customer& cst, Server& srv)

    {
        //***process arrival***//

       //if server busy enQ customer

       if(srv.serverBusy==1){

            cstQ1.push(cst);


       }

       else{
            //set busy to true
            srv.serverBusy=1;

            //set service_end_time=arrival_time+service_time
            srv.total_time_to_serve=cst.arrival_time+cst.serving_time;



       }
    //readData(rFile, cst);

    }

//define processServiceCMP
int processServiceCMP(queue<Customer>& cstQ1,queue<Customer>&cstQ2,Customer& cst,Server&srv)
    {

        //***process service***//
        //if Q empty set busy to false
        if (cstQ1.empty())
            srv.serverBusy=0;

        //else service_end_time=service_end_time+deQ which must return the service end time of this job
        else{
            srv.total_time_to_serve=cstQ1.front().arrival_time+cstQ1.front().serving_time;
            cstQ1.pop();

        }

    }

int readData(ifstream& rFile, Customer& cst)  //will get the next id, service time and arrival time
{
    (rFile>>cst.id>>cst.arrival_time>>cst.serving_time);


}
       


My output



ID 1
Service customer 1
Qsize 0
total service time? 0
ID 2
Service customer 2
Qsize 0
total service time? 1.4
ID 3
Service customer 3
Qsize 1
total service time? 1.4
ID 4
Service customer 4
Qsize 0
total service time? 2.9
First of all, white space is your friend. It is a whole lot easier to debug problems when you can clearly read the code. For instance, line 102 of your source file is difficult to read. With all of the '>' and '.' and '_' characters, it's difficult to see that there are actually 3 reads on this line. Go ahead and put spaces on either side of the ">>" operators and give yourself a break. A space before and after every binary operator and a space after every comma are fairly common guidelines.

I'm suspect that at line 46, serverBusy is 0 after you read and process customer 4. Because that is 0, your while statement is false, and you drop out.

I would recommend using a debugger (your development environment should have one built in) to step through your code and see what values are being set and when. Then you can see what's happening.

If you do not have access to a debugger, try to get one. If you still can't find a debugger, print out values of interest at critical points. For instance, print out serverBusy at line 45. But, knowing how to use a debugger will make life so much easier down the road.
Even commenting /*&& server.serverBusy==1*/ prints out similar values...just can't calculate line 4 and from file above...
Topic archived. No new replies allowed.