Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

mkii

macrumors newbie
Original poster
Jan 24, 2013
2
0
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
 

drsoong

macrumors member
Mar 24, 2008
56
1
Munich
cout and output

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

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
 

mkii

macrumors newbie
Original poster
Jan 24, 2013
2
0
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
 

ArtOfWarfare

macrumors G3
Nov 26, 2007
9,558
6,058
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

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.
 

ghellquist

macrumors regular
Aug 21, 2011
146
5
Stockholm Sweden
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
 

Mac_Max

macrumors 6502
Mar 8, 2004
404
1
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.
 

can.rules

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

-DrSoong

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).
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.