New to C++

This is a discussion on New to C++ within the C++ Programming forums, part of the General Programming Boards category; So, recently deciding that I want try and learn C++, I picked up "Teach Yourself C++ in 24 Hours" for ...

  1. #1
    Registered User
    Join Date
    May 2006
    Posts
    3

    New to C++

    So, recently deciding that I want try and learn C++, I picked up "Teach Yourself C++ in 24 Hours" for a starting point. All works well, until Hour 2 ( ) where I come to a problem - I type in the code exactly as it is in the book, compile it, and when I run the program, the output is different than what is shown in the book.

    Here is the code:
    Code:
    #include <iostream>
    
    int Add (int x, int y)
    {
    	std::cout << "In Add(), recieved " << x << " and " << y << "\n";
    	return (x+y);
    }
    
    int main()
    {
    	std::cout << "I'm in main()!\n";
    	std::cout << "\nCalling Add() \n";
    	std::cout << "The value returned is: " << Add(3,4);
    	std::cout << "\nBack in main().\n";
    	std::cout << "\nExiting...\n\n";
    	return 0;
    }
    Here is what is supposed to come out:
    I'm in main()!

    Calling Add()
    In Add(), received 3 and 4
    The value returned is: 7
    Back in main().

    Exiting...

    And, finally, here is what I get:
    Calling Add()
    The value returned is: In Add(), received 3 and 4
    7
    Back in main().

    Exiting...


    A few notes - I use Mac OS X 10.4.6, write the code in Xcode, and use the Terminal to compile. I thought maybe the output was different because I am running OS X, but that does not make any sense to me. I have read the code over and over, just to make sure I typed everything correctly.

    Sorry for the long post, and not sure if I broke any rules by posting code on here, if I did, just left me know. Any help at all on this issue would be greatly appreciated!

    Thanks,
    ~eros
    Last edited by erosforgotten; 05-18-2006 at 01:16 AM.

  2. #2
    Registered User stuart_cpp's Avatar
    Join Date
    Apr 2006
    Location
    UK
    Posts
    31
    My output was

    I'm in main()!

    Calling Add()
    In Add(), received 3

    Im running Windows and copyed and pasted the code in Dev C++. I dont know why your having a different output 2 mine

  3. #3
    Registered User
    Join Date
    Jan 2005
    Posts
    847
    Your impimentation is going from right to left and not left to right.

  4. #4
    Registered User
    Join Date
    May 2006
    Posts
    3
    Quote Originally Posted by Quantum1024
    Your impimentation is going from right to left and not left to right.
    Any way to fix this?

    And any idea why stuart_cpp would be getting a different output?

  5. #5
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,755
    > std::cout << "The value returned is: " << Add(3,4);
    Welcome to the world of side effects.

    Your book shows one plausable answer - the function is called first, then the string and result is printed.

    Your compiler shows another plausable answer - the first string is printed, then the function is called and then the result printed.

    Neither is right or wrong. If the Add() function didn't call cout, it would not matter which order things were done in.

    To guarantee the order, you would need to do this
    Code:
    int answer = Add(3,4);
    std::cout << "The value returned is: " << answer;
    > Sorry for the long post
    Not a problem - I regard this as a model post.
    It's got
    - the question
    - the code
    - the output
    - the OS
    - the compiler
    perfect IMO.

    It's a lot better than many of the cryptic "plz hlp" illiterati who post one line of code and no information as to what they have, what they see etc etc.
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  6. #6
    Registered User
    Join Date
    May 2006
    Posts
    3
    Awesome! Thanks Salem - worked perfectly!

    I'm even a bit proud of myself, for the "int answer = Add(3,4);", I wasn't sure where to add it, but apparently I put it in the right spot (or one of the right spots) and it worked! Ahh, to appreciate the small things in life . . .

    Thanks for the help! I'm sure I will become a familiar face around here, but hopefully not an annoyance, LOL.

    ~eros

  7. #7
    Registered User
    Join Date
    Apr 2006
    Posts
    132
    Hmmm, whats the std:: for?

    u dont need to use std:: u can just use cout <<" "

  8. #8
    Dae
    Dae is offline
    Deprecated Dae's Avatar
    Join Date
    Oct 2004
    Location
    Canada
    Posts
    1,034
    Quote Originally Posted by Hugo716
    Hmmm, whats the std:: for?

    u dont need to use std:: u can just use cout <<" "
    Only if you declare the using preprocessor (using namespace std;), which he isn't doing. There are reason's not to use it, and use std:: instead, but the purpose isn't relevent until further than intermediate stages.
    Warning: Have doubt in anything I post.

    GCC 4.5, Boost 1.40, Code::Blocks 8.02, Ubuntu 9.10 010001000110000101100101

  9. #9
    Registered User
    Join Date
    May 2006
    Location
    Berkshire, UK
    Posts
    29
    I wonder if using std::endl instead of "\n" would sort the problem out, as ti would cause the buffer to be flushed immediately.

    Michael

  10. #10
    Registered User
    Join Date
    Jun 2005
    Posts
    6,528
    Quote Originally Posted by Dae
    Only if you declare the using preprocessor (using namespace std;), which he isn't doing.
    "using" is not related to the preprocessor. It is a compiler keyword used (ahem, employed) to give an instruction to the compiler on how it find candidate things (functions, objects, types) that correspond to name.

  11. #11
    Registered User
    Join Date
    Jan 2005
    Posts
    7,344
    >> I wonder if using std::endl instead of "\n" would sort the problem out, as ti would cause the buffer to be flushed immediately.

    Not in this case. The issue is not with the flushing of the buffer, it is with the order that the text is sent to the buffer.

Popular pages Recent additions subscribe to a feed

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