Steva Steva - 8 months ago 55
C++ Question

String stream output compare

I want to compare output of stringstream with some string.

Problem is when I use fill and width on stringstream I cant compare resulting string with preloaded string.

std::stringstream sstr;
sstr.fill(' ');
sstr << 4 << std::endl;
if(" 4" == sstr.str()){
std::cout << "Equal" << std::endl;

It's not equal. My educated guess would be that width somehow use some kind of flag or other kind of indicator to replace bunch of spaces in string. But I am not sure and didn't find anything useful on google. Does anyone know why I cannot compare that (sstream.str() and targeted string)?

Goal is to test what will stringstream (which is heart of my component) print on console.

Answer Source

You also inserted a std::endl into the string. That's going to add a newline character to the string.

Remove the std::endl from your output.