C++ Program to Demonstrate Use of Input String Stream

This C++ program illustrates use of input string stream. The class template std::basic_istringstream implements output operations on memory based streams. This is used to attach stream to a string, that is we can read from it using this stream.

Here is the source code of the C++ program which illustrates use of input string stream. The C++ program is successfully compiled and run on a Linux system. The program output is also shown below.

  1. /*
  2.  * C++ Program to illustrate use of input string stream
  3.  */
  4. #include <iostream>
  5. #include <string>
  6. #include <sstream>
  7. using namespace std;
  8.  
  9. void print(const string& places)
  10. {
  11.     string s;
  12.     istringstream is(places);
  13.     int count = 0;
  14.  
  15.     while(is >> s)
  16.         cout << (++count) << ". " << s << endl;
  17.     if(count > 0)
  18.         cout << "You have visited " << count << " places!" << endl;
  19.     else
  20.         cout << "You went nowhere!" << endl;
  21. }
  22.  
  23. int main()
  24. {
  25.     string places;
  26.  
  27.     cout << "Enter the places you visited    : ";
  28.     getline(cin, places);
  29.     cout << "Printing the places you visited :-" << endl;
  30.     print(places);
  31. }

$ a.out
Enter the places you visited    : USA Germany London India
Printing the places you visited :-
1. USA
2. Germany
3. London
4. India
You have visited 4 places!

Sanfoundry Global Education & Learning Series – 1000 C++ Programs.

advertisement
advertisement
If you wish to look at all C++ Programming examples, go to C++ Programs.

If you find any mistake above, kindly email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.