using functions as arguments to cout?

I'm sure there's a way to do this, but it escapes me at the moment. I'd like to compose a cout statement that uses multiple function calls as arguments, like:
cout << a.get(); << b.get(); << c.get(); << endl

Obviously, the first semicolon throws off the compiler. Can this be done, or do I need multiple cout statements?
Why not just not use the semicolon?
You can use them in a single cout but the one at the rightmost is called first.

I pushed 1,2,3,4 in the stack.
If I pop them and cout like this:
cout<<mystack.pop();
cout<<mystack.pop();
cout<<mystack.pop();
cout<<mystack.pop();

output order is : 4,3,2,1.

But if I pop them in a single cout:

cout<<mystack.pop()<<" "<<mystack.pop()<<" "<<mystack.pop()<<" "<<mystack.pop();

The output is :
1 2 3 4
That compiles for you? My compiler doesn't like it:

cout << a.get() << ' ' b.get() << ' ' c.get() ' ' << d.get() << endl;

gives this:

error: expected `;' before 'b'


Am I missing a dumb mistake here?
It depends on how your getter is defined, but most probably this is how you want it:
std::cout << a.get() << " " << b.get() << std::endl;

which gives the output:
(what a.get() returns)<space>(what b.get() returns)
Try like this :
cout << a.get() << " "<< b.get() << " "<<c.get()<< " "<< d.get() << endl;
Last edited on
Thanks, guys...I think I need to make a rule not to code (or post questions) after a certain time of night -- I just get too careless by then. What a dumb mistake.

I appreciate the help.
You can use them in a single cout but the one at the rightmost is called first.

I pushed 1,2,3,4 in the stack.
If I pop them and cout like this:
cout<<mystack.pop();
cout<<mystack.pop();
cout<<mystack.pop();
cout<<mystack.pop();

output order is : 4,3,2,1.

But if I pop them in a single cout:

cout<<mystack.pop()<<" "<<mystack.pop()<<" "<<mystack.pop()<<" "<<mystack.pop();

The output is :
1 2 3 4


What? I am pretty sure that shouldn't be the case... are you sure this is what happens?
I'm 100% certain that operations are evaluated left-to-right. It has to.
What? I am pretty sure that shouldn't be the case... are you sure this is what happens? 
I'm 100% certain that operations are evaluated left-to-right. It has to.


Im not claming that it must be so,just shown the result I got.(note: I was also surprised with the result,was expecting the same output as calling them individually.)
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
#include <iostream>

using namespace std;

class Stack
{
private:
        int arr[20];
        int top;
public:
       Stack():top(-1)
       {}
       void push(int num)
       {
       arr[++top]=num;
       }
       int pop()
       {
       return arr[top--];
       }
};



int main()
{
Stack s1;

s1.push(11); // push 3 items onto stack
s1.push(12);
s1.push(13);

cout << s1.pop() << endl; // pop 3 items and display them
cout << s1.pop() << endl;
cout << s1.pop() << endl<<endl<<endl;

s1.push(11); // push again
s1.push(12);
s1.push(13);


cout<<"Now call in a single cout :\n";


cout<<s1.pop()<<" "<<s1.pop()<<" "<<s1.pop();

return 0;
}



13
12
11


Now call in a single cout:
11 12 13


I tried one too and it really does seem to evaluate the expressions right to left. Unless I'm doing something wrong.

1
2
3
4
5
6
7
8
9
#include <iostream>

int main()
{
	int i = 1;
	std::cout << ++i << ' ' << i++;
	std::cin.get();
	return 0;
}



3 1


The second output value is 1 so the second expression i++ seems to have been evaluated first.
Last edited on
C++ requires left to right evaluation only for && || and , (comma) operators.
Said that, std::cout << ++i << ' ' << i++; is still bad since you are modifying i twice in the same expression ( and the result of that is undefined according to the C++ standard )
Yeah, I take my statement back. The functions can be called in any order.
Right then. Thanks for the advice.
Topic archived. No new replies allowed.