C++ Class

Can someone out there shed some light with regards to the output of the following code. I have a queue object with two elements a.q[1]=10; a.q[2]=20;
but when I print it out to screen as in the last line it prints "20 10". What is the explanation, how can I fix it.

Regards,
Remus

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
#include <iostream>
using namespace std;

class queue{
int q[100];
int sloc,rloc;
public:
	void init();
	void qput(int i);
	int qget();
};

//Init the queue
void queue::init()
{
rloc=sloc=0;
}
// Put integer in queue
void queue::qput(int i)
{
if (sloc==100){
	cout<<"Queue is full.\n";
	return;
}
sloc++;
q[sloc]=i;
cout<<"sloc="<<sloc<<" ;q[sloc]="<<q[sloc]<<"\n";
}
//get an integer from the queue
int queue::qget()
{
	if (rloc==sloc){
	cout<<"Queue underflow.\n";
	return 0;
	}
	rloc++;
	cout<<"rloc="<<rloc<<" ;q[rloc]="<<q[rloc]<<"\n";
	return q[rloc];
}


void main()
{
queue a,b;
a.init();

a.qput(10);
a.qput(20);

cout<<"Queue a: "<<a.qget()<<" "<<a.qget()<<"\n";

system("PAUSE");
}
To get the required order split this statement

cout<<"Queue a: "<<a.qget()<<" "<<a.qget()<<"\n";

into two statements

cout<<"Queue a: "<<a.qget() <<" ";
cout <<a.qget()<<"\n";
Topic archived. No new replies allowed.