Variable is undeclared?

It says that the variables at int main (line 77) are undeclared.
it is a simple program so just help me understand that silly yet confusing mistake.

Appreciated.

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
#include<iostream>
using namespace std;
class queue {
public:
	void queue_init(int size);
	void push(char x);
	void pop();
	int size();
	char front();
	char back();
	int Empty();
	int full();


private:
	char* queue_ptr;
	int max_len;
	int rear_cnt;
	int front_cnt;
};

int queue::size() {
	return (rear_cnt - front_cnt);
}

void queue::queue_init(int arr_size) {
	queue_ptr = new char[arr_size];
	max_len = arr_size - 1;
	front_cnt = 0;
	rear_cnt = 0;
}

int queue::Empty() {
	return (rear_cnt == front_cnt);
}

int queue::full() {
	return (rear_cnt == max_len + 1);
}
char queue::front() {
	return (queue_ptr[front_cnt]);
}

char queue::back() {
	return (queue_ptr[rear_cnt - 1]);
}

void queue::push(char X) {
	if (rear_cnt == max_len + 1)
		cout << "Error queue is full \n";
	else
	{
		queue_ptr[rear_cnt] = X;


		rear_cnt++;
	}
}

void queue::pop() {

	if (Empty())
		cout << "Error queue is empty\n";
	else
	{
		queue_ptr[front_cnt] = 0;
		front_cnt++;
	}
}



int main(){
	queue Q;
	int time = 810;
	Q.queue_init(10);
	char a, b, c, d, e, f, g, h, j, k, l;
	cout << "This program will calculate the customer service throughout the next 30 minutes.\n"
		"Since a customer enters Rajhi Bank every 3 minutes, therefore the size of the queue should be 10 or a possible 11." << endl;
	cout << "The customer service opens at 8:10 am" << endl;
	for (int i = 0; i <= 30; i++)
	{
		if (i = 0) {
			cout << "it is 8:10 am. The first customer just arrived." << endl;
			Q.push(a);
			cout << "There are: "; Q.size(); cout << "waiting on line currently" << endl;
			cout << " ************************************************************************* " << endl;
		}
		if (i = 3) {
			cout << "it is 8:13 am. The second customer just arrived." << endl;
			Q.push(b);
			cout << "There are: "; Q.size(); cout << "waiting on line currently." << endl;
			cout << "currently serving: "; Q.front(); cout << endl;
			cout << " ************************************************************************* " << endl;

		}
		if (i = 5){
			cout << "it is 8: 15 am. The first customer is served." << endl;
			Q.pop();
			cout << "There are: "; Q.size(); cout << "waiting on line currently" << endl;
			cout << "currently serving: "; Q.front(); cout << endl;
			cout << " ************************************************************************* " << endl;
		}

		if (i = 6){
			cout << "it is 8: 16 am. The third customer just arrived." << endl;
			Q.push(c);
			cout << "There are: "; Q.size(); cout << "waiting on line currently" << endl;
			cout << "currently serving: "; Q.front(); cout << endl;
			cout << " ************************************************************************* " << endl;
		}
		if (i = 9){
			cout << "it is 8: 19 am. The fourth customer just arrived." << endl;
			Q.push(d);
			cout << "There are: "; Q.size(); cout << "waiting on line currently" << endl;
			cout << "currently serving: "; Q.front(); cout << endl;
			cout << " ************************************************************************* " << endl;
		}
		if (i = 10){
			cout << "it is 8: 20 am. The second customer is served." << endl;
			Q.pop();
			cout << "There are: "; Q.size(); cout << "waiting on line currently" << endl;
			cout << "currently serving: "; Q.front(); cout << endl;
			cout << " ************************************************************************* " << endl;
		}
		if (i = 12){
			cout << "it is 8: 22 am. The fifth customer just arrived." << endl;
			Q.push(e);
			cout << "There are: "; Q.size(); cout << "waiting on line currently" << endl;
			cout << "currently serving: "; Q.front(); cout << endl;
			cout << " ************************************************************************* " << endl;
		}
		if (i = 15){
			cout << "it is 8: 25 am. The sixth customer just arrived." << endl;
			Q.push(f);
			cout << "and the third customer just got served."; Q.pop(); cout << endl;
			cout << "There are: "; Q.size(); cout << "waiting on line currently" << endl;
			cout << "currently serving: "; Q.front(); cout << endl;
			cout << " ************************************************************************* " << endl;
		}
		if (i = 18){
			cout << "it is 8: 28 am. The seventh customer just arrived." << endl;
			Q.push(g);
			cout << "There are: "; Q.size(); cout << "waiting on line currently" << endl;
			cout << "currently serving: "; Q.front(); cout << endl;
			cout << " ************************************************************************* " << endl;
		}
		if (i = 20){
			cout << "it is 8: 30 am. The fourth customer just got served." << endl;
			Q.pop();
			cout << "There are: "; Q.size(); cout << "waiting on line currently" << endl;
			cout << "currently serving: "; Q.front(); cout << endl;
			cout << " ************************************************************************* " << endl;
		}
		if (i = 21){
			cout << "it is 8: 31 am. The eighth customer just arrived." << endl;
			Q.push(h);
			cout << "There are: "; Q.size(); cout << "waiting on line currently" << endl;
			cout << "currently serving: "; Q.front(); cout << endl;
			cout << " ************************************************************************* " << endl;
		}
		if (i = 24){
			cout << "it is 8: 34 am. The ninth customer just arrived." << endl;
			Q.push(j);
			cout << "There are: "; Q.size(); cout << "waiting on line currently" << endl;
			cout << "currently serving: "; Q.front(); cout << endl;
			cout << " ************************************************************************* " << endl;
		}
		if (i = 25){
			cout << "it is 8: 35 am. The fifth customer just got served." << endl;
			Q.pop();
			cout << "There are: "; Q.size(); cout << "waiting on line currently" << endl;
			cout << "currently serving: "; Q.front(); cout << endl;
			cout << " ************************************************************************* " << endl;
		}
		if (i = 27){
			cout << "it is 8: 37 am. The tenth customer just arrived." << endl;
			Q.push(j);
			cout << "There are: "; Q.size(); cout << "waiting on line currently" << endl;
			cout << "currently serving: "; Q.front(); cout << endl;
			cout << " ************************************************************************* " << endl;
		}
		if (i = 30){
			cout << "it is 8: 40 am. The eleventh customer just arrived." << endl;
			Q.push(l);
			cout << "and the sixth customer just got served."; Q.pop(); cout << endl;
			cout << "There are: "; Q.size(); cout << "waiting on line currently" << endl;
			cout << "currently serving: "; Q.front(); cout << endl;
			cout << " ************************************************************************* " << endl;
		}
	}
	return 0;
}
Last edited on
Strange I don't seem to get any "undeclared" variable errors. However there are quite a few other problems you should look into fixing.

It appears that you don't know the difference between the assignment operator= and the comparison operator== for the first several problems.

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
In function 'int main()':
83:12: warning: suggest parentheses around assignment used as truth value [-Wparentheses]
89:12: warning: suggest parentheses around assignment used as truth value [-Wparentheses]
97:12: warning: suggest parentheses around assignment used as truth value [-Wparentheses]
105:12: warning: suggest parentheses around assignment used as truth value [-Wparentheses]
112:12: warning: suggest parentheses around assignment used as truth value [-Wparentheses]
119:13: warning: suggest parentheses around assignment used as truth value [-Wparentheses]
126:13: warning: suggest parentheses around assignment used as truth value [-Wparentheses]
133:13: warning: suggest parentheses around assignment used as truth value [-Wparentheses]
141:13: warning: suggest parentheses around assignment used as truth value [-Wparentheses]
148:13: warning: suggest parentheses around assignment used as truth value [-Wparentheses]
155:13: warning: suggest parentheses around assignment used as truth value [-Wparentheses]
162:13: warning: suggest parentheses around assignment used as truth value [-Wparentheses]
169:13: warning: suggest parentheses around assignment used as truth value [-Wparentheses]
176:13: warning: suggest parentheses around assignment used as truth value [-Wparentheses]
183:13: warning: suggest parentheses around assignment used as truth value [-Wparentheses]
75:6: warning: unused variable 'time' [-Wunused-variable]
77:34: warning: unused variable 'k' [-Wunused-variable]
91:12: warning: 'b' may be used uninitialized in this function [-Wmaybe-uninitialized]
107:12: warning: 'c' may be used uninitialized in this function [-Wmaybe-uninitialized]
114:12: warning: 'd' may be used uninitialized in this function [-Wmaybe-uninitialized]
128:12: warning: 'e' may be used uninitialized in this function [-Wmaybe-uninitialized]
135:12: warning: 'f' may be used uninitialized in this function [-Wmaybe-uninitialized]
143:12: warning: 'g' may be used uninitialized in this function [-Wmaybe-uninitialized]
157:12: warning: 'h' may be used uninitialized in this function [-Wmaybe-uninitialized]
164:12: warning: 'j' may be used uninitialized in this function [-Wmaybe-uninitialized]
185:12: warning: 'l' may be used uninitialized in this function [-Wmaybe-uninitialized]
 


Thanks for the quick reply.

well, yes. you can see the problem of "uninitialized" variables at the second half of the problems you are showing me.

How do I fix them?
If you're having trouble with initializing a variable, the solution would simply be to initialize them.

You are pushing the char variables a,b,c etc into the queue, but they're garbage. They have no value, you havent given them any value so they're value of garbage. when you do Q.push(c); what are you pushing into the queue? what is "c" equal to?
c is of type char. are you saying I should initialize it as char c = c;

?
char c = c;
Right idea, but not exactly like this. You need to put the char quotes around it.

char c = 'c';
Topic archived. No new replies allowed.