Thread: Using stringstream to convert numeric types to strings

  1. #1
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663

    Using stringstream to convert numeric types to strings

    In the following code, I'm trying to figure out why when I output the string "data" it's empty:
    Code:
    #include<iostream>
    #include<string>
    #include<sstream>
    
    using namespace std; 	
    
    int main()
    {
    	string data;
    	stringstream outString(data);
    
    	int num = 50;
    	outString<<num;
    	
    	cout<<data<<endl;
    
    	return 0;
    }
    My book says the parameter to the stringstream constructor is a reference so write operations for stringstream objects act directly on the string object(e.g. data):

    stringstream outString(data);
    Last edited by 7stud; 08-06-2003 at 11:53 PM.

  2. #2
    Registered User
    Join Date
    May 2003
    Posts
    1,619
    I believe that "stringstream outString(data);" only copies the current contents of data (which is nothing) into outString.

  3. #3
    Registered User
    Join Date
    Apr 2002
    Posts
    1,571
    Look at what you are doing. Declare a string that contains "what" and then a stringstream that you pass data to the constructor. Then you write 50 to the stringstream which will overwrite it in the beginning. Then your last line you simply output the string not the stringstream. Its doing just what you asked, outputting the string. If you want to output the stringstream just do

    cout << outString.str();
    "...the results are undefined, and we all know what "undefined" means: it means it works during development, it works during testing, and it blows up in your most important customers' faces." --Scott Meyers

  4. #4
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    Cat,

    "I believe that "stringstream outString(data);" only copies the current contents of data (which is nothing) into outString."

    I agree with that statement, but what about the line that follows:

    outString<<num;

    Mr.Wizard,

    I can't follow what you're saying. Based on what my book says, I assume something like this is happening:
    Code:
    #include<iostream>
    #include<string>
    
    using namespace std; 	
    
    void func(string& S)
    {
    	S="Hello World";
    }
    
    int main()
    {
    	string data;
    	func(data);
    
    	cout<<data<<endl;
    
    	return 0;
    }
    You can't change a reference parameter in a function and leave the original unchanged because the reference is just an alias for the original, so a change in the reference is a change in the original.

  5. #5
    Registered User
    Join Date
    Nov 2002
    Posts
    1,109
    a look at the FAQ may help.

  6. #6
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    alpha,

    Thanks I'm aware of the str() function of the stringstream class. My book says you can use that function to get at the string inside the ostringstream object when you use the default ostringstream constructor. In that case, you don't have a string object name, like data, to refer to:

    ostringstream outString;
    outString<<10;

    cout<<outString.str();//No object name to use, so have to use str()

    But in my case, I have the string object name:

    ostringstream outString(data);

    My book seems to say I should be able to do this:

    cout<<data;
    ----------------------------------------

    Mr. Wizard,

    Below is a simple stringstream class I created that has a reference to the string object as the parameter to its constructor, and as the code demonstrates, I can use the string object's name to output what was written to the stream:
    Code:
    #include<iostream>
    #include<string>
    
    using namespace std; 	
    
    class ss_stream
    {
    public:
    	ss_stream(string& rhs)
    	{
    		s = &rhs;
    	}
    
    	void operator<<(string a)
    	{
    		*s=a;
    	}
    		
    private:
    	string* s;
    };
    
    
    
    int main()
    {
    	string data;
    	ss_stream outString(data);
    	
    	outString<<"hello world";
    
    	cout<<data<<endl;
    
    	return 0;
    }
    I believe my book is saying the stringstream class works similarly.
    Last edited by 7stud; 08-07-2003 at 12:50 AM.

  7. #7
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    Maybe I am mis-interpreting what my books says. Here it is:

    You can use an ostringstream object to format data into a string. For instance, you could create a string object and an ouput string stream with the statements:

    string outBuffer;
    ostringstream outStr(outBuffer);

    You can now use the insertion operators to write to outBuffer via outStr:

    double number = 2.5;
    outStr << "number = "<< (number/2);

    As a result of the write to the string stream, outBuffer will contain "number = 1.25". The string outBuffer will automatically expand to accomodate however many characters you write to the stream, so it is a very flexible way of forming strings or complex output messages.

    The string parameter to the string stream constructor is a reference in each case, so write operations for the ostringstream and stringstream objects act directly on the string object. There is also a default constructor for each of the string stream classes. When you use these, the string stream object will maintain a string object internally, and you can obtain a copy of this using the str() member. For example,

    ostringstream outStr;
    double number = 2.5;
    outStr<< "number = " << (3*number/2);
    string ouput = outStr.str();

    After these statements, output will contain the string "number = 3.75".

    (Ivor Horton's Beginning C++)

  8. #8
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    2,718
    Additionally, in case the second constructor syntax is used, the stream's buffer is initialized with the content of the STL string object str as if a call to member str.
    http://cplusplus.com/ref/iostream/os...ingstream.html
    Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah

    You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie

  9. #9
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    ...and what conclusion would someone brighter than me draw from that statement?

    That must mean that the string is copied into the internal buffer of the ostringstream object, and not itself used as the internal buffer as my book seems to say:

    "You can now use the insertion operators to write to outBuffer(the string object) via outStr(the ostringstream object):"

    I'm not sure what functionality that constructor supplies, then.
    Last edited by 7stud; 08-07-2003 at 02:30 AM.

  10. #10
    Registered User
    Join Date
    Oct 2002
    Posts
    291
    Originally posted by 7stud
    In the following code, I'm trying to figure out why when I output the string "data" it's empty:
    Code:
    #include<iostream>
    #include<string>
    #include<sstream>
    
    using namespace std; 	
    
    int main()
    {
    	string data;
    	stringstream outString(data);
    
    	int num = 50;
    	outString<<num;
    	
    	cout<<data<<endl;
    
    	return 0;
    }
    My book says the parameter to the stringstream constructor is a reference so write operations for stringstream objects act directly on the string object(e.g. data):

    stringstream outString(data);
    I dont see anything wrong with this code. Think about what the program is doing,
    1. Create an empty string variable.
    2. Create a stringstream with an empty string variable as a parameter. So the stringstream will also be empty.
    3. Create in an integer variable and set it to 50.
    4. Insert the integer variable into the outString stream.
    5 You output the first variable you create, which btw is still just as empty now as when you created it.

    This code will print '50'
    Code:
    int main()
    {
    	string data;
    	stringstream outString(data);
    
    	int num = 50;
    	outString<<num;
    	
    	cout<<outString.str()<<endl;
    	system("PAUSE");
    	return 0;
    }
    or

    Code:
    int main()
    {
       	string data;
    	stringstream outString(data);
    
    	int num = 50;
    	outString<<num;
    	data = outString.str();
    
    	cout << data << endl;
    
    	system("PAUSE");
    	return 0;
    }

  11. #11
    Registered User
    Join Date
    May 2003
    Posts
    148
    Originally posted by 7stud
    That must mean that the string is copied into the internal buffer of the ostringstream object, and not itself used as the internal buffer as my book seems to say:
    Correct.
    https://www.dinkumware.com/manuals/r...h=sstream.html
    explicit basic_stringstream(
    const basic_string<Elem, Tr, Alloc>& str,
    ios_base:: openmode mode =
    ios_base::in | ios_base:: out);

    The second constructor initializes the base class by calling basic_iostream(sb). It also initializes sb by calling basic_stringbuf<Elem, Tr, Alloc>(str, mode)
    ...and basic_strinbbuf
    basic_stringbuf(const basic_string<Elem, Tr, Alloc>& str,
    ios_base:: openmode mode =
    ios_base::in | ios_base:: out);

    The second constructor allocates a copy of the sequence controlled by the string object str.
    I'm not sure what functionality that constructor supplies, then.
    Example
    Code:
    ...
    string str = "1 2 3";
    stringstream stream(str);
    int a,b,c;
    stream >> a >> b >> c;
    Maybe the author is confused with the deprecated strstream
    Code:
    #include <iostream>
    #include <strstream>
    
    using namespace std;
    
    
    int main()
    {
    	char buffer[100] = {0};
    	strstream str(buffer,100);
    
    	str << "Hello World";
    
    	cout << buffer << endl;
    	cout << str.str() << endl;
    }

  12. #12
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    Wledge,

    Thanks. I noticed the constant specifier for the reference parameter in the ostringstream constructor in the link XSquared provided, which if added to the simple stringstream class I posted would cause an error. I'm just surprised my books detailed explanation is about something that is dead wrong:

    "...write operations for the ostringstream and stringstream objects act directly on the string object. "

    That's totally false.
    Last edited by 7stud; 08-07-2003 at 05:24 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. convert numbers to strings
    By Unregistered in forum C++ Programming
    Replies: 8
    Last Post: 07-08-2002, 03:43 PM
  2. damn strings
    By jmzl666 in forum C Programming
    Replies: 10
    Last Post: 06-24-2002, 02:09 AM
  3. menus and strings
    By garycastillo in forum C Programming
    Replies: 3
    Last Post: 04-29-2002, 11:23 AM
  4. Replies: 13
    Last Post: 01-22-2002, 04:38 AM
  5. Replies: 8
    Last Post: 09-04-2001, 09:15 PM