
//
// This is example code from Chapter 11.5 "Line-oriented input" of
// "Programming -- Principles and Practice Using C++" by Bjarne Stroustrup
//
// Try experimenting with uncommenting some of the commented code...what's going on
// between the iostream and the buffer?
//

#include <iostream>
#include <string>
#include <sstream>
#include <vector>

//------------------------------------------------------------------------------

using namespace std;

int main()
{
	string name;
	cout << "Enter a name: ";
	cin >> name;            // input: Dennis Ritchie
	cout << name << '\n';   // output: Dennis
	cin.ignore('\n', 100);
	//cin.clear();
	//cout << "hi ho!  Silver!\n";
	//cin.unget();

	cout << "Enter a name (this time getline()): ";
	getline(cin, name);      // input: Dennis Ritchie
	cout << "You entered:" << name << '\n';   // output: Dennis Ritchie	
	//cin.ignore('\n', 100);

	string first_name;
	string second_name;
	istringstream ss{ name };
	ss >> first_name;         // input Dennis
	ss >> second_name;        // input Ritchie
	cout << "hi mom, press \"enter\" to continue: \n";
	cin.get();
	cout << "first_name == " << first_name << ", second_name == " << second_name << endl;
	{
		string command;
		getline(cin, command);   // read the line

		stringstream ss(command);
		vector<string> words;
		string s;
		cout << "\nss contains " << ss.str() << endl;
		cin.get();
		while (ss >> s) words.push_back(s);    // extract the individual words
		for (string st : words) cout << st << endl;
	}
	cout << "The rest of the original ss: ";
	while (ss >> name) cout << name << " ";
}

/*
Enter a name: carpe diem
carpe
Enter a name (this time getline()): You entered: diem
hi mom, press "enter" to continue:

Now is the time

ss contains Now is the time

Now
is
the
time
*/
//------------------------------------------------------------------------------
