I was hoping someone might take a moment and tell me the error of my ways. I find that outputting the variable Resultin the code below prints out an empty string of one blank space. I compiled my code using the latest version of Xcode on Snow Leopard as a c++ std library terminal app.
#include <iostream>
#include <sstream>
#include <string>
usingnamespace std;
int main (int argc, char * const argv[]) {
int Number = 123; // number to be converted to a string
string Result; // string which will contain the result
ostringstream convert; // stream used for the conversion
convert << Number; // insert the textual representation of 'Number' in the characters in the stream
Result = convert.str(); // set 'Result' to the contents of the stream
// 'Result' now is equal to "123"
cout << "Result: " << Result << endl; // ERROR HERE output
return 0;
}
Later in the tutorial a shortened version is suggested with the same issues.
1 2
int Number = 123;
string String = static_cast<ostringstream*>( &(ostringstream() << Number) )->str();
Try to flush the stringstream before getting str():
12 13 14 15 16
convert << Number; // insert the textual representation of 'Number' in the characters in the stream
convert.flush(); // see http://www.cplusplus.com/reference/iostream/ostream/flush/
Result = convert.str(); // set 'Result' to the contents of the stream
I went ahead and gave that a try but still the same issue. Edit: I went ahead and tried two more blocks of code form that tutorial and neither function correctly. I'm going to try it in on Linux machine and see what happens. Another Edit: Worked just fine on a Linux machine at school
#include <iostream>
#include <sstream>
#include <string>
usingnamespace std;
int main (int argc, char * const argv[]) {
int Number = 123; // number to be converted to a string
string Result; // string which will contain the result
ostringstream convert; // stream used for the conversion
convert << Number; // insert the textual representation of 'Number' in the characters in the stream
convert.flush();
Result = convert.str(); // set 'Result' to the contents of the stream
// 'Result' now is equal to "123"
cout << "Result: " << Result << endl;
return 0;
}
Thanks for your help Bazzy. The information in that link helped me solve my issue. I edited the project setting in Xcode to make the base SDK Mac OS X 10.5 rather then 10.6. Seems to have cleared up all the issues for now.