Unexplainable Error

I have this program where I m trying to stimulate a queue. The program goes into an infinite loop, I dunno why. There's nothing wrong with any of my while loops.
Main Program:
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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
#include <iostream>
#include <iomanip>
#include<ctime>
#include <stdlib.h>
using namespace std;
#include"Bank.h"

struct Teller_s {
	bool active;
	int time_At;
};


void wait( int seconds )
{
  clock_t eWait;
  eWait = clock () + seconds * CLOCKS_PER_SEC ;
  while (clock() < eWait) {}
}

int main() {
	//Min/Max values for user input
	const int    MIN_SIM_TIME   = 0, MAX_SIM_TIME   = 10000,
	             MIN_TRANS_TIME = 0, MAX_TRANS_TIME = 100,
				 MIN_NUM_SERV   = 0, MAX_NUM_SERV   = 10,
				 MIN_ARRIV_TIME = 0, MAX_ARRIV_TIME = 100;

	char runAgain = 'Y'; //Set runAgain so the program runs
	int sim_Time, num_Serv, arriv_Time; //User input variables
	int trans_Time = 3;
	int trans_Time_flag = 0;
	int remaining_count = 0;
	int track_count;
	int i,j,c_Time; //Counters
	int customers, wait_Time; //Total customers and integer for customer waiting time
    int num; int max =16, min = 3; //random number within limits
    int cust_ID; int ID_max =9000, ID_min =1000;
    int counter0, counter7, counter14, counterID;
    int ID_time_store[200][2];
    bool flag1=false;

	while (runAgain != 'N') {
		i = 0; c_Time = 0;
		customers = 0; wait_Time = 0;
        Queue bankQ;
        counter0 = 0; counter7 = 0; counter14 = 0; counterID =0;

		cout << "\n------------------------------------------";
		cout << "\n- Welcome to the Bank Simulation Program -";
		cout << "\n------------------------------------------";

		//Menu information
		cout << "\n\nPlease input the following data(Time in minutes):\n";
		cout << "\nLength of the Simulation: ";
		cin >> sim_Time;
		while (sim_Time <= MIN_SIM_TIME || sim_Time > MAX_SIM_TIME) {
			cout << "Invalid input. Please re-enter: ";
			cin >> sim_Time;
		}
		sim_Time = sim_Time * 60;

		//cout << "Average Transaction Time: ";
		/*cin >> trans_Time;
		while (trans_Time <= MIN_TRANS_TIME || trans_Time > MAX_TRANS_TIME) {
			cout << "Invalid input. Please re-enter: ";
			cin >> trans_Time;
		}*/
	/*	 srand(time(NULL));
         num = ((rand()%(max-min))+min);
         trans_Time = num;
         cout << num << endl;*/

		cout << "Average Number of Servers: ";
		cin >> num_Serv;
		while (num_Serv <= MIN_NUM_SERV || num_Serv > MAX_NUM_SERV) {
			cout << "Invalid input. Please re-enter: ";
			cin >> num_Serv;
		}
		cout << "Average Time Between Arrivals: ";
		/*cin >> arriv_Time;
		while (arriv_Time <= MIN_ARRIV_TIME || arriv_Time > MAX_ARRIV_TIME) {
			cout << "Invalid input. Please re-enter: ";
			cin >> arriv_Time;
		}*/
		arriv_Time = 3;
		cout << arriv_Time << endl;

		//Dynamically allocate an array for the teller structure
		Teller_s * tellArray = new Teller_s[num_Serv];

		//Set all tellers to empty
		for (i = 0; i < num_Serv; i++) {
			tellArray[i].active = false;
			tellArray[i].time_At = trans_Time;
		}

       for(i=0;i<200;i++)
        for(j=0;j<2;j++)
        {
            ID_time_store[i][j] = 0;
        }


		while (c_Time < sim_Time) { //Run until <strong class="highlight">simulation</strong> time is reached
			if (c_Time % arriv_Time == 0) { //Check if a customer should be enqueued
                srand(time(NULL));
                cust_ID = ((rand()%(ID_max-ID_min))+ID_min);
				bankQ.enqueue(cust_ID);
				customers++;
			}

			for (i = 0; i < num_Serv; i++) {
				if (tellArray[i].active == false && bankQ.getSize() != 0) { //Dequeue if a teller is open
					cust_ID = bankQ.dequeue();
					cout << "Cutomer ID checked out" << cust_ID << endl;
					tellArray[i].active = true;

					srand(time(NULL));
                    num = ((rand()%(max-min))+min);
                    trans_Time = num;

                    for (i=0; i < counterID; i++)
                    {
                        if ((cust_ID == ID_time_store[i][0]) && (ID_time_store[i][1] != 0))
                        {
                            flag1 = true;
                            track_count = i;
                        }
                    }

                    if (flag1 == true)
                    {
                        trans_Time = ID_time_store[track_count][1];
                        if (trans_Time > 7)
                        {
                            trans_Time = 7;
                        }
                        ID_time_store[track_count][1] = (ID_time_store[track_count][1] - 7);
                        if (ID_time_store[track_count][1] <= 0)
                        {
                            ID_time_store[track_count][1] =0;
                        }
                    }
                    else
                    {
                    if ((trans_Time > 7) && (trans_Time < 15))
                        {
                            trans_Time_flag = 1;
                            counter7 ++;
                            ID_time_store[counterID][0] = cust_ID;
                            ID_time_store[counterID][1] = (trans_Time-7);
                            cout << " Debugging !!!! " << counterID << endl;
                            counterID++;
                            trans_Time = 7;
                            cout << "trans_time " << trans_Time << "counterID  " << counterID << endl;
                        }
                    else if (trans_Time > 14)
                        {
                            trans_Time_flag = 2;
                            counter14 ++;
                            ID_time_store[counterID][0] = cust_ID;
                            ID_time_store[counterID][1] = (trans_Time - 7);
                       //     counterID ++;
                        }
                    else
                        {
                            trans_Time_flag = 0;
                            counter0++;
                        }
                    }
                //   tellArray[i].time_At = trans_Time;
                    tellArray[i].time_At = 6;
                    cout << "counter0 " << counter0 << "counter7 " << counter7 << "counter14 "<< counter14 << endl;
				}

			}



			for (i = 0; i < num_Serv; i++) {
				if (tellArray[i].active == true) {
					tellArray[i].time_At--;  //Decrement time spent at teller
				}


				if (tellArray[i].time_At == 0) {
					tellArray[i].active = false; //Set teller to open if time limit is reached
				}
			}

			wait_Time += bankQ.getSize(); //Set wait time to persons left in <strong class="highlight">queue</strong>
			wait(1);
			c_Time++;
			cout << "wait_time = "<< wait_Time << "c_time = " << c_Time << "queue size = " << bankQ.getSize() << endl;
		}
		//Output user input data
		cout << "\n---------------"
			 << "\n- Data Output -"
			 << "\n---------------\n";

		cout << setw(31) << left << "Simulation Time: ";
		cout << sim_Time << endl;

		cout << setw(31) << left << "Average Transaction Time: ";
		cout << trans_Time << endl;

		cout << setw(31) << left << "Average Number of Servers: ";
		cout << num_Serv << endl;

		cout << setw(31) << left << "Average Time Between Arrivals: ";
		cout << arriv_Time << endl << endl;

		//Output calculated data
		cout << "Average Total Wait Time: ";
		cout << fixed << setprecision(2)
			 << (float)wait_Time/customers;
		cout << "\nCustomers in line at end of simulation: "
	         << bankQ.getSize() << endl;

		//Ask to run again
		cout << "\nRun the program again? (y/n): ";
		cin >> runAgain;
		runAgain = (char)toupper(runAgain);
		while (runAgain != 'Y' && runAgain != 'N') {
			cout << "Invalid Entry, please re-enter: ";
			cin >> runAgain;
			runAgain = (char)toupper(runAgain);
		}
		//Deallocate teller structure array
		delete [] tellArray;
	}
	system("pause");
	return 0;
}
Last edited on
My Queue:
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
#include <iostream>
using namespace std;
#include "Bank.h"

Queue::Queue() {
	mySize = 0;
	first = NULL;
	last = NULL;
}

void Queue::enqueue(ElementType i) {
	NodePointer nPtr = new Node(i);
	NodePointer predPtr = first;

	if (first == NULL) { //Insert if <strong class="highlight">queue</strong> is empty
		nPtr->next = first;
		first = nPtr;
	} else { //Insert into the <strong class="highlight">queue</strong> at the end
		while (predPtr->next) {
			predPtr = predPtr->next;
		}
		nPtr->next = predPtr->next;
	}
	mySize++;
	last = nPtr; //Set last to new pointer
}

ElementType Queue::dequeue() {
	ElementType temp1 = 0;
	if (first) {
		NodePointer dPtr = first;
		temp1 = dPtr->data;
		first = first->next; //Set first to the second node in the list
		delete dPtr; //Delete the node that has been dequeued
	}
	mySize--;
	return temp1;
}

ElementType Queue::front() {
	if (first) {
	    NodePointer ptr = first;
        return ptr->data;
	}
}

int Queue::getSize() {
	return mySize;
}

Queue::~Queue() {
	if (first) {
		//Deallocate all nodes in <strong class="highlight">queue</strong>
		NodePointer current = first;
		NodePointer temp = first;

		temp = current->next;
		delete current;
		current = temp;
	}
}
My Header:
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
#ifndef QUEUE
#define QUEUE

#include <iostream>
using namespace std;

typedef int ElementType;

class Queue  {

	public:

		//Default constructor
		Queue();

		//Add to the back of the <strong class="highlight">queue</strong>
		void enqueue(ElementType);

		//Remove from the front of the <strong class="highlight">queue</strong>
		ElementType dequeue();

		//Returns the front of the <strong class="highlight">queue</strong>
		ElementType front();

		//Return size of the <strong class="highlight">queue</strong>
		int getSize();

		//Destructor
		~Queue();

	private:

		class Node {

			public:

				ElementType data;
				Node *next;

				Node(ElementType i) { // Node constructor
					data = i;
					next = NULL;
				}

		}; //--- end of Node class

		typedef Node *NodePointer;

		Node *first;
		Node *last;
		int mySize;

}; //--- end of <strong class="highlight">Queue</strong> class

#endif 
The program goes into an infinite loop, I dunno why. There's nothing wrong with any of my while loops.


so that means you should have a idea about where should my program terminate?
so are you talking about this?
 
	while (runAgain != 'N') {

so add the watch variable and inspect it.

The error I get is I am accesing a memory space beyond the allocated memory but the array tellArray is a dynamic array so it should increase in memory naturally. Can u see any other mistake in my code?
I revised the code & realise that allocation to the dynamic array tellArray is impossible. Compiler doesn allow me to do an integer comparison with a value in that array either. I replace all instances of int ID_time_score with tellArray. Compiler gives this erro.

100 C:\Documents and Settings\Desktop\program.cpp no match for 'operator[]' in '*((+(((unsigned int)i) * 8u)) + tellArray)[j]'

I think its saying I shouldn't convert from unsigned int to int. My qn is tellArray is a dynamic array which is an instance of the structure Teller_s so can't I do integer allocation to tellArray?
Topic archived. No new replies allowed.