Problem with Converting numbers to strings tutorial

I was working through the tutorial posted here http://www.cplusplus.com/articles/numb_to_text/ in the Articles section.

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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <iostream>
#include <sstream>
#include <string>
using namespace 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 
Last edited on
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
1
2
./a.out
Result: 123

baffled by the Xcode issue.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <iostream>
#include <sstream>
#include <string>
using namespace 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;
}
Last edited on
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.
Last edited on
Topic archived. No new replies allowed.