Register FAQ / Rules Forum Spy Search Today's Posts Mark Forums Read
Go Back   MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Reply
 
Thread Tools Search this Thread Display Modes
Old Jan 24, 2013, 11:58 AM   #1
mkii
macrumors newbie
 
Join Date: Jan 2013
cout << complex weird problem!

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 = (

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
mkii is offline   0 Reply With Quote
Old Jan 24, 2013, 12:14 PM   #2
drsoong
macrumors member
 
Join Date: Mar 2008
Location: Munich
cout and output

Quote:
Originally Posted by mkii View Post
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 = (

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
drsoong is offline   0 Reply With Quote
Old Jan 24, 2013, 01:24 PM   #3
mkii
Thread Starter
macrumors newbie
 
Join Date: Jan 2013
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
mkii is offline   0 Reply With Quote
Old Jan 24, 2013, 06:58 PM   #4
ArtOfWarfare
macrumors 601
 
ArtOfWarfare's Avatar
 
Join Date: Nov 2007
Send a message via Skype™ to ArtOfWarfare
Quote:
Originally Posted by mkii View Post
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.
__________________
Battery Status - On the Mac App Store
The only app that'll estimate when your wireless devices will need their batteries changed.
Including the ones paired with other Macs on your network.
ArtOfWarfare is online now   0 Reply With Quote
Old Jan 25, 2013, 04:03 AM   #5
ghellquist
macrumors regular
 
Join Date: Aug 2011
Location: 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
ghellquist is offline   0 Reply With Quote
Old Jan 25, 2013, 08:57 PM   #6
Mac_Max
macrumors 6502
 
Join Date: Mar 2004
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.
Mac_Max is offline   0 Reply With Quote
Old Jan 25, 2013, 09:13 PM   #7
can.rules
macrumors member
 
Join Date: Dec 2008
Quote:
Originally Posted by drsoong View Post

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).
can.rules is offline   0 Reply With Quote

Reply
MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -5. The time now is 06:26 AM.

Mac Rumors | Mac | iPhone | iPhone Game Reviews | iPhone Apps

Mobile Version | Fixed | Fluid | Fluid HD
Copyright 2002-2013, MacRumors.com, LLC