C++ Program to Demonstrate Use of Input String Stream

bookmark

#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);
}

 

Output:
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!