views:

365

answers:

5

Hi,

I would expect the following code to output hello5. Instead, it only outputs hello. It seems to be a problem with trying to output an int to the ostringstream. When I output the same directly to cout I receive the expected input. Using XCode 3.2 on Snow Leopard.

Thanks!

#include <iostream>
#include <string>
#include <sstream>

using namespace std;

int main(){
 int myint = 5;
 string mystr = "hello";
 string finalstr;
 ostringstream oss;

 oss << mystr << myint;
 finalstr = oss.str();

 cout << finalstr;


 return 0;
}

EDIT: See the answer I posted below. This seems to be created by a problem in the Active Configuration 'Debug' in XCode 3.2 on Snow Leopard

+3  A: 

Your code is correct, it writes hello5 on my Windows 7 machine. Maybe the problem is rather that you don't write a std::endl or something which might confuse your OS.

Patrick Daryll Glandien
no go, but thanks for testing
yuval
Your problem seems to be related to your compiler or your shell. Try attaching a debugger and see what happens behind the scenes.
Patrick Daryll Glandien
how is that done?
yuval
Can you test with debug enabled? ie `-D_GLIBCXX_DEBUG=1` like on a unix box? Curious if is's a Mac only problem
epatel
+1  A: 

I just tested and it worked just fine on my Mac with Xcode 3.2.1 and Snow Leopard. It not that your prompt is shadowing the output? Try add an endl to the cout line?

-- Edit --

My test suite

  • c++ test.cpp -- works fine
  • c++ -D_GLICXX_DEBUG=1 test.cpp -- fail
  • c++ -arch i386 -D_GLICXX_DEBUG=1 test.cpp -- works fine

What can we say about this? In short, Debug version of 64 bit stdc++ seem to be broken.

epatel
nope! tried and no cigar, but get this: if I flip the output (int first, string second), then NOTHING shows up. ( oss << myint << mystr;) in fact, anything after outputting an int doesn't show up.
yuval
How do you compile it? I did a plain `c++ text.cpp`
epatel
that's what I have
yuval
+2  A: 

Yep, tested on this end (windows XP Pro) and it works swimmingly

Brendan
Can you test with debug enabled? ie `-D_GLIBCXX_DEBUG=1` like on a unix box? Curious if is's a Mac only problem
epatel
I ran it through a debugger and didn't see anything unusual...sorry
Brendan
I can imagine MS have their own implementation of the stream classes. Mac relies on the GLIB sources. Wonder if it's isolated to the Mac...
epatel
+4  A: 

Changing the Active Configuration in XCode from 'Debug' to 'Release' works as a workaround.

Does anybody know why this, and several other unexplainable errors pop-up in 'Debug' mode in XCode 3.2 on Snow Leopard?

yuval
I can only concur...when testing within an Xcode c++ std++ 10.6 template with DEBUG the int is not outputted. 10.5 with DEBUG does work ok. I suggest you file a bug report. http://bugreport.apple.com
epatel
It definitely has something to do with the `-D_GLIBCXX_DEBUG=1` option. When given to my previous command line test it now showed the same problem.
epatel
+2  A: 

http://stackoverflow.com/questions/1416096/c-debug-builds-broke-in-snow-leopard-x-code http://stackoverflow.com/questions/1603300/xcode-3-2-1-and-c-string-fails

cdespinosa
This is the correct solution. It says to switch the compiler from GCC 4.2 to GCC 4.0 (in Project Settings, DEBUG configuration). Program runs correctly after that.
Erik Olson