I'm having trouble displaying a string and integers through the use of stringstream. The code below displays fine as is but say I put in << endl after name. When I compile this it only displays the name string and thats it. If I add a space in any of the strings it will also only display the previous variables and end at the space. Some of the variables include doubles, integers and strings in the out variable of type stringstream. Tell me if I have my logic of stringstream incorrect because this is my first time using this technique.

//Opens a stringstream and allows integers to be displayed as a string

	stringstream out;

	string dateString;

	//stores integers and strings into a string out stream

	out << "Name:" << name  << "Temperament:" << tendency <<
			"Weight:" << weight << "Height:" << height <<
			"Location:" << location << "DOB:" << dateInception <<
			"Death:" << dateDeath;

	//displays the string using the command out instead of cout

	out >> dateString;

	cout << dateString;

why are you using endl with stringstream? Doesn't make any sense because all endl does is '\n' followed by stream.flush(), which is intended to flush data to hard drive.

So what you are really writing is this: out << name << '\n' << flush << ...

ok. If I type in out << name << "is the person's name"; What should be the result? Will it be the same as out << "is" << flush?

Edited 6 Years Ago by Peter_morley: n/a

If the value of name is "Peter" then the result should be "Peteris the person's name". Note that there is no space between Peter and "is" because you didn't put one there. If you want a space there then use << " is ..." (space before is).

Edited 6 Years Ago by Ancient Dragon: n/a

If I write out << name << " is the person's name";
I get only Peter
If I write out << name << "is the person's name";
I get only Peteris
I'm confused

There is probably something else wrong with your program. Try this:

#include <iostream>
#include <sstream>
#include <string>
using std::cout;
using std::cin;
using std::string;
using std::stringstream;

int main()
{
//Opens a stringstream and allows integers to be displayed as a string
    string name = "Peter";
    int weight = 100;
    int height = 6;
    string location = "Anywhere USA";
    string dateInception = "2010/10/15";
    string tendency = "Happy";
    string dateDeath = "Still Alive";
	stringstream out;

	string dateString;

	//stores integers and strings into a string out stream

	out << "Name:" << name  << "Temperament:" << tendency <<
			"Weight:" << weight << "Height:" << height <<
			"Location:" << location << "DOB:" << dateInception <<
			"Death:" << dateDeath;

	//displays the string using the command out instead of cout

	out >> dateString;

	cout << dateString;
}

Hi i think its the cout call, I think that cout<<oss would result in the first item from the oss being sent to cout. if you want the whole thing try using cout<<oss.str(); which prints the entire contents of the oss at once.

Edited 6 Years Ago by Kanoisa: n/a

>>doesn't the << operator on stringstream parse the input using white-spaces as the delimeter

No, but the >> operator does.

change out >> dateString
to getline(out,dateString);

Edited 6 Years Ago by Ancient Dragon: n/a

Thanks Kanoisa and Ancient Dragon. I used cout << out.str(); and it printed the entire string. Now I have to learn pointers with other classes ahhh shoot me!

This article has been dead for over six months. Start a new discussion instead.