0

I was learning about getline and istringstream and got stuck with this code giving the output "Hello World World" while I was expecting just "Hello World". Can someone tell me why is it printing that and how can it be fixed? Thank you for your help in advance!

#include <iostream>
#include <sstream>

using namespace std;

int main()
{
string a="Hello world",b;
istringstream x(a);
while(x!=nullptr)
{
    cout<<x;
    getline(x,b,' ');
cout << b<< " ";
}
return 0;
}
  • 3
    Compiler does not read istringstream at all: it's your program that does reading. – Sergey Kalinichenko Aug 29 '16 at 17:41
  • While I have never seen `iosteam != nullptr` it is the same as using `!iostream.eof()`. – NathanOliver Aug 29 '16 at 17:42
  • Ohhh! My bad thank you for helping :P – Abhimanyu Kaushal Aug 29 '16 at 17:49
  • 2
    what surprises me is that the `istream` to `nullptr` comparison *compiles*. The conversion from `istream` to `void*` was removed in the same version of the standard that introduced `nullptr`. In fact, [both clang and gcc reject the comparison](http://coliru.stacked-crooked.com/a/89128ecfc191703c). What compiler and version are you using, OP? – jaggedSpire Aug 29 '16 at 18:03
  • actually, the latest VC++ [rejects it too](http://rextester.com/OEVQN66664). Maybe one of the early 201x versions of MSVC? – jaggedSpire Aug 29 '16 at 18:27
  • I'm running mingw32-g++. And can anyone tell me why and how this program is working? – Abhimanyu Kaushal Aug 29 '16 at 18:31
  • what version does it say it has when you do `g++ --version` in the console? (the first line of the output is the really relevant one there) – jaggedSpire Aug 29 '16 at 18:32
  • The version is 4.6.2 – Abhimanyu Kaushal Aug 29 '16 at 18:38
  • That would probably do it--it's a really old version, and probably supports an in-between state of c++03 and c++11. For reference, the current latest release of gcc is 6.1. – jaggedSpire Aug 29 '16 at 18:39
  • I assume you're not enabling warnings? [g++4.6 does compile the comparison without the std=c++0x flag](http://coliru.stacked-crooked.com/a/d7c751418b2a5d64), but issues a warning that `nullptr` is only in C++0x (what they were calling C++11 before it came out). It looks like the comparison stops compiling in 4.8. Anyway, thanks for indulging my curiosity :-) – jaggedSpire Aug 29 '16 at 18:44
  • No. It does not show any warnings and I have enabled all warnings. And also can you tell me how this code is working or being manipulated and why it don't run for infinite? – Abhimanyu Kaushal Aug 29 '16 at 18:51
  • You may want to upgrade to a newer version of mingw32, if you don't have to work with exactly the features of that compiler and version. Newer compilers tend to give more comprehensible error messages, and most answers on here tend to assume you're working with a compiler with full c++14 support. If you have the choice it'll probably be less painful to learn c++ on a newer compiler. – jaggedSpire Aug 29 '16 at 18:51
  • @AbhimanyuKaushal in C++03, `istream`s are convertible to `void*` to allow them to be used by themselves in boolean expressions. If the stream is in a fail state, it'll convert to a null pointer. Thus, in your code the while loop will stop when the stream enters a failure state, as will happen when it fails to read input from `x` in the `getline` line. – jaggedSpire Aug 29 '16 at 18:56

0 Answers0