Thread: Cout is printing from right to left

  1. #1
    The Autodidact Dante Wingates's Avatar
    Join Date
    Apr 2010
    Location
    Valhalla
    Posts
    56

    Cout is printing from right to left

    Im experiencing some pretty strange behavior here. I have a stack class, and I was testing it, but I cant really tell whats wrong with it. Actually, the class seems to be working, but cout is behaving strangely.

    Heres the problem. Suppose we push a, b and c. If you pop 3 times, you will get c, b and a. So far so good. This works as expected,

    Code:
    cout << stack.pop();
    and it will print the last value we pushed(c). It works, but this one line doesnt work at all:

    Code:
    cout << stack.pop() << ' ' << stack.pop();
    it will print "b c", in that order. As far as I know, it should print "c b". Strangly enough, this one works ok:

    Code:
    cout << stack.pop() << ' ';
    cout << stack.pop();
    It prints "c b".

    Then I used this:

    Code:
    int i;
    cout << (i = stack.pop()) << ' ' << stack.pop();
    And yes, it prints "c b". It gives the value of c to i, even though without i = stack.pop() it will print "b c".

    if you use this

    Code:
    cout << (i = stack.pop()) << ' ' << stack.pop() << ' ' << stack.pop();
    it will print "c a b".

    Its a funny behavior, and though its not a big problem I thought about asking what Im doing wrong. Why is cout behaving like this? Am I missing something obvious here?

    I don't think my stack class is wrong, but then again... We usually think our code is ok. Im aware of that, but using int i = stack.pop() will get the last pushed value, as it should be. So this works:

    Code:
    int i = stack.pop();
    cout << i;
    One thing that should be noticed, it looks like its printing from right to left. It will always print the last pushed value, anteceded by any other value we "pop". So if we push a, b, c and d and then pop three times, we get b, c and d, in that order, not "a, b, c" nor "d, c, b".

    Now a piece of the actual code. This prints "3 2 1"

    Code:
    int main()
    {
    	DStack stack;
    	stack.push16(0);
    	stack.push16(1);
    	stack.push16(2);
    	stack.push16(3);
    	for(int i = 0; i < 3; i++)
    		cout << stack.pop16() << ' ';
    	cin.get();
    	return(0);
    }
    And this, "1 2 3"

    Code:
    int main()
    {
    	DStack stack;
    	stack.push16(0);
    	stack.push16(1);
    	stack.push16(2);
    	stack.push16(3);
    	cout << stack.pop16() << ' ' << stack.pop16() << ' ' << stack.pop16();
    	cin.get();
    	return(0);
    }
    Is this what should be happening? Because "cout << 2 << ' ' << 1;" prints 2 1, not 1 2.

    Thanks for taking the time.
    2B OR !2B? That is the question!

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > Am I missing something obvious here?
    You mean like undefined behaviour?

    You need to understand sequence points.
    Sequence point - Wikipedia, the free encyclopedia

    You also need to know that << is NOT a sequence point, and that your pop() function has a side effect.

    So if you have two pop() calls in a single cout statement, there is NO way for you to know which one of them will be called first. Note that this is DIFFERENT to which will be displayed first.

    The answer is of course, as you discovered, to output only one pop() per sequence point.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    The Autodidact Dante Wingates's Avatar
    Join Date
    Apr 2010
    Location
    Valhalla
    Posts
    56
    Now that was some useful information.

    It will be very useful, thank you very much.
    2B OR !2B? That is the question!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. implementing property in c++ just like c#?
    By Masterx in forum C++ Programming
    Replies: 17
    Last Post: 11-24-2010, 01:35 AM
  2. Need help with program.
    By olgirl4life in forum C++ Programming
    Replies: 3
    Last Post: 12-11-2006, 11:06 PM
  3. Cant find the error
    By Coder87C in forum C Programming
    Replies: 8
    Last Post: 06-19-2005, 01:57 AM
  4. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  5. Redirecting cout stream
    By Arrow Mk84 in forum C++ Programming
    Replies: 1
    Last Post: 10-08-2002, 04:17 PM