cout and function call....strange

This is a discussion on cout and function call....strange within the C++ Programming forums, part of the General Programming Boards category; Ok we did a program in class thats pretty self explanatory. I wrote it two ways, our main focus is ...

  1. #1
    RoD
    RoD is offline
    Redundantly Redundant RoD's Avatar
    Join Date
    Sep 2002
    Location
    Missouri
    Posts
    6,331

    cout and function call....strange

    Ok we did a program in class thats pretty self explanatory. I wrote it two ways, our main focus is this line:

    Code:
    cout<<"From function: "<<make_chng(value)
           <<"\nFrom main: "<<value<<endl;
    Putting this all in one line, the output is:

    Enter integer: 10
    From function: 100
    From main: 10

    This output is wrong because value was changed, as you can clearly see on the previous line of the cout, where it was 100.

    when i change this line to

    Code:
    cout<<"From function: "<<make_chng(value);
    cout<<"\nOriginal: "<<value<<endl;
    it outputs:

    Enter integer: 10
    From function: 100
    From main: 100

    That output is correct according to the program. Now my question is this: Why does having it in one cout make it give correct value for "value" on the first part then the wrong number ont he second part?? According to logical program flow, should it not output two 100's even tho its in one cout because its been changed??? I know this is because of how i write the cout, but i don't know why. Heres the full program with the cout line that is working wrong being used:

    Code:
    #include <iostream.h>
    
    int make_chng(int &changed);
    
    int main()
    {
    	int value;
    
    	cout<<"Enter a integer: ";
    	cin>>value;
    
    	cout<<"From function: "<<make_chng(value)
    		<<"\nFrom main: "<<value<<endl;
    
    	return 0;
    }
    
    int make_chng(int &changed)
    {
    	changed *= 10;
    	return changed;
    }

  2. #2
    Registered User
    Join Date
    Nov 2002
    Posts
    1,109
    hmm...thats weird...to be honest, since it works if you split it up, i would split it up...

    i'm not really sure why though...
    processor speed? (although it doesn't really make sense)

  3. #3
    RoD
    RoD is offline
    Redundantly Redundant RoD's Avatar
    Join Date
    Sep 2002
    Location
    Missouri
    Posts
    6,331
    Its almost as tho its flowing into the cout, calling the function, returning the right number, backing up before the cout, skipping the call,a nd outputting the original value. It makes no sense, the teacher didn't have an explanation either.

  4. #4
    Registered User
    Join Date
    Nov 2002
    Posts
    1,109
    i think it has something to do with the fact that it is on the same cout line. although i really dont see why it doesn't work, it looks like it would.

    it might not be able to see the fact that the value has been changed...?

  5. #5
    I lurk
    Join Date
    Aug 2002
    Posts
    1,361
    I *think* I know why. At first I was stumped, but let me try to explain it.

    You can chain your calls to cout together with <<, each call to the operator << returns a refrence to the cout object. So, the last thing couted would be the first evaluated.

    Oh, and there's a very good chance i'm wrong.
    Last edited by Eibro; 11-22-2002 at 05:09 PM.

  6. #6
    RoD
    RoD is offline
    Redundantly Redundant RoD's Avatar
    Join Date
    Sep 2002
    Location
    Missouri
    Posts
    6,331
    eib i thought the same thing, but i mean its AFTER the value change in the cout, so if the first output of the value is correct, then the second should be. If i am correct the reference parameter "&" would give the value it returns to the variable it got the original value from, as it does, gives 100 to value.

    Anybody got another idea?

  7. #7
    I lurk
    Join Date
    Aug 2002
    Posts
    1,361
    Run this, it should be self explanitory
    Code:
    #include <iostream>
    using namespace std;
    
    int eval1();
    int eval2();
    
    int main()
    {
    	cout << eval1() << eval2();
    	return 0;
    }
    
    int eval1()
    {
    	cout << "Hello " << endl;
    	return 1;
    }
    
    int eval2()
    {
    	cout << " world!" << endl;
    	return 2;
    }

  8. #8
    RoD
    RoD is offline
    Redundantly Redundant RoD's Avatar
    Join Date
    Sep 2002
    Location
    Missouri
    Posts
    6,331
    can u explain why it does that for me? I'm just confused by the whole situation with my program.

  9. #9
    I lurk
    Join Date
    Aug 2002
    Posts
    1,361
    Like I said, the last thing in your cout stream is the first evaluated. eval2() is called before eval1(), yet their return values are printed out in order.

    In your situation, the value of value is stored, the function is evaluated, its return value is stored... then they get printed out in order.

    If it's still not clear try this:
    Code:
    int make_chng(int &changed);
    
    int main()
    {
    	int value;
    	cin >> value;
    
    	cout << "From function: " << make_chng(value)
    		 << "\nFrom main: " << value << endl;
    	return 0;
    }
    
    int make_chng(int &changed)
    {
    	cout << endl << "make_chng()" << endl;
    	changed = changed * 10;
    	return changed;
    }

  10. #10
    RoD
    RoD is offline
    Redundantly Redundant RoD's Avatar
    Join Date
    Sep 2002
    Location
    Missouri
    Posts
    6,331
    I think you solved it, thank you.

  11. #11
    Registered User
    Join Date
    Nov 2002
    Posts
    1,109
    wow, thats really weird. i learned something new today...thank you.

  12. #12
    RoD
    RoD is offline
    Redundantly Redundant RoD's Avatar
    Join Date
    Sep 2002
    Location
    Missouri
    Posts
    6,331
    Originally posted by alpha
    wow, thats really weird. i learned something new today...thank you.
    Same here, as well as my CS2 teacher lol.

  13. #13
    Registered User
    Join Date
    Nov 2002
    Posts
    1,109
    hehe...I also learned something new yesterday, a way to call cout that I didn't know was possible

    I took a test yesterday in my APCS class, thats when I learned it, my teacher also learned it too.

    the question was similar to this:

    you have a class, in the .h file there was a function prototype like this:

    void Print (ostream &os);

    and calling it from main and cout could be done this way:

    object.Print(cout);

    it looked weird at first, but makes sense.

    keep in mind, I haven't been programming that long, and I know its not anything big, but I didn't know you could do it that way until yesterday.

  14. #14
    RoD
    RoD is offline
    Redundantly Redundant RoD's Avatar
    Join Date
    Sep 2002
    Location
    Missouri
    Posts
    6,331
    i saw that once or twice in different books, haven't used it yet tho.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. call base class function or derived class function
    By George2 in forum C++ Programming
    Replies: 4
    Last Post: 03-18-2008, 06:23 AM
  2. Calling a Thread with a Function Pointer.
    By ScrollMaster in forum Windows Programming
    Replies: 6
    Last Post: 06-10-2006, 09:56 AM
  3. Noob problem - function call
    By Ultraman in forum C++ Programming
    Replies: 4
    Last Post: 05-20-2006, 03:28 AM
  4. help with a source code..
    By venom424 in forum C++ Programming
    Replies: 8
    Last Post: 05-21-2004, 01:42 PM
  5. Replies: 5
    Last Post: 02-08-2003, 07:42 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21