cout << complex weird problem!

Discussion in 'Mac Programming' started by mkii, Jan 24, 2013.

  1. macrumors newbie

    Joined:
    Jan 24, 2013
    #1
    Hi,
    The following segment:

    Code:
    #include <iostream>
    #include <complex>
    
    int main (int argc, char * const argv[]) 
    {
        std::complex<double> a(1.,1.);
        std::cout << "a = "<< a << "\n";
        return 0;
    }
    using Xcode 3.2 (the project setting is 'Command Line tool') on OSX 10.6.8 compiles fine and produces the following output:

    a = (

    :confused: when of course it should produce:

    a = (1.,1.)

    Yes, I can display the real and imaginary parts using a.real(), a.imag() but the above should work.

    Any ideas why Xcode / OSX has this weird behaviour? Has anyone else seen this, any suggestions?
    Cheers,
    m
     
  2. macrumors member

    Joined:
    Mar 24, 2008
    Location:
    Munich
    #2
    cout and output

    I tried your code compiled with

    Code:
    g++ main.cc -o cout
    in Terminal, and it works fine. I am also using 10.6.8. At best this could be a XCode problem.

    You could try to force a "flush" of the output buffer, using std::cout.flush(); after you print the complex number, i.e.

    Code:
    #include <iostream>
    #include <complex>
    
    int main (int argc, char * const argv[]) 
    {
        std::complex<double> a(1.,1.);
        std::cout << "a = "<< a << "\n";
        std::cout.flush();
        return 0;
    }
    It can be the case that the cout is not written fast enough to the terminal window, before it terminates.

    -DrSoong
     
  3. thread starter macrumors newbie

    Joined:
    Jan 24, 2013
    #3
    Thanks drsoong,

    I tried

    std::cout.flush();

    but it made no difference.

    Like you say it is almost certainly an Xcode problem, I would use a terminal and g++ but it is a largish project so Xcode is useful. For portability reasons I hope to get to the bottom of it rather than writing
    cout << "a=" << a.real() << a.imag()
    everywhere.
    mk ii
     
  4. macrumors 603

    ArtOfWarfare

    Joined:
    Nov 26, 2007
    #4
    You could write an inline function that prints out that or a define that will sub in the code so that you don't have to write that out everywhere.

    Also, I believe I have encounter a similar problem with cout not displaying everything it should when run in Xcode on Snow Leopard. The issue doesn't show up in Xcode on Lion/Mountain Lion, nor does the bug manifest itself in Terminal for any version of OS X that I have.
     
  5. macrumors regular

    Joined:
    Aug 21, 2011
    Location:
    Stockholm Sweden
    #5
    Works on latest Xcode

    Tried it on latest xcode (download today) / latest os. Works without problems inside xcode. Not tried in terminal (tell me if I should)

    My guess is that it is a bug that has been fixed. Might not help OP though.

    // Gunnar
     
  6. macrumors 6502

    Joined:
    Mar 8, 2004
    #6
    When I was in College taking C++ classes I had a lot of trouble with XCode's output window so I stuck to compiling and then running my programs in the terminal. This likely has gotten better over the past two years or so but when in doubt, run the program in the terminal.
     
  7. macrumors member

    Joined:
    Dec 29, 2008
    #7
    This should work as expected without the need for an explicit flush. This is likely an Xcode bug. I would recommend upgrading to a newer version or reporting the issue to Apple (and pray for a fix).
     

Share This Page