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.
/*
* C++ Program to illustrate use of input string stream
*/
#include <iostream>
#include <string>
#include <sstream>
using namespace std;
void print(const string& places)
{
string s;
istringstream is(places);
int count = 0;
while(is >> s)
cout << (++count) << ". " << s << endl;
if(count > 0)
cout << "You have visited " << count << " places!" << endl;
else
cout << "You went nowhere!" << endl;
}
int main()
{
string places;
cout << "Enter the places you visited : ";
getline(cin, places);
cout << "Printing the places you visited :-" << endl;
print(places);
}
$ 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.
Related Posts:
- Apply for C++ Internship
- Check Programming Books
- Check C++ Books
- Practice Computer Science MCQs
- Check Computer Science Books