4

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

epatel
  • 45,805
  • 17
  • 110
  • 144
Yuval Karmi
  • 26,277
  • 39
  • 124
  • 175
  • 1
    I get `hello5` using both Microsoft Visual C++ 9.0 and Intel C++ Compiler 11.1. – James McNellis Dec 06 '09 at 02:10
  • 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 Dec 07 '09 at 05:16
  • 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 Dec 07 '09 at 20:03

4 Answers4

4

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

Brian Webster
  • 30,033
  • 48
  • 152
  • 225
Yuval Karmi
  • 26,277
  • 39
  • 124
  • 175
  • 2
    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 Dec 06 '09 at 02:41
  • 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 Dec 06 '09 at 02:47
3

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 Glandien
  • 7,791
  • 5
  • 41
  • 47
  • Your problem seems to be related to your compiler or your shell. Try attaching a debugger and see what happens behind the scenes. – Patrick Glandien Dec 06 '09 at 02:13
  • 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 Dec 06 '09 at 02:50
2

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

Brendan Lesniak
  • 2,271
  • 4
  • 24
  • 48
  • 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 Dec 06 '09 at 02:51
  • I ran it through a debugger and didn't see anything unusual...sorry – Brendan Lesniak Dec 06 '09 at 04:14
  • 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 Dec 06 '09 at 11:24
1

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.

Community
  • 1
  • 1
epatel
  • 45,805
  • 17
  • 110
  • 144
  • 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 Karmi Dec 06 '09 at 02:11
  • How do you compile it? I did a plain `c++ text.cpp` – epatel Dec 06 '09 at 02:12