/* gettoken.cpp -- It reads from input a token (a maximal sequence of non-whitespace characters)
 */

#include <iostream>
#include <string>

// It returns an identifier from the current line (like if using '>>')
// and if no identifier is on the current line, returns "". If more than
// one identifier is on a line all but the first are discarded.
string gettoken(void)
{
  string token = "";     // Value returned to user.
  int gotnonwhite = 0;  // It becomes 1 after we read a non-whitespace character.
  for(;;){
      char c;
      cin.get(c);
      if (c == '\n')break;
      if (isspace(c)) {
	if (gotnonwhite) 
	  {
	    cin.ignore(256, '\n');
	    break;
	  }
      }else{
	  gotnonwhite = 1;
	  token += c;
	}
    }
  return token;
}

void main(void)
{
  string who;
  cout << "Enter name: ";
  while ((who = gettoken()) != "")
    {
      cout << who << endl;
      cout << "Enter name: ";
    }
  cout << "Goodbye!\n";
}
