//R. A. Hillyard
//cinTest4.cpp
//program to demonstrate input errors
//and use of cin member functions
//
//part four - make clearLine a function

#include<iostream>       //new style of include
#include<cctype>

using namespace std;

void clearLine(istream& myStream);  /function prototype
//pre condition -  will be sent a valid input stream object
//post condition - will read all stream input to next new line

int main()
  {
  char charChoice;        //hold character entered by user
  int intChoice;          //hold int entered by user  

  do
    {
    cout << "\n*** Welcome to some program ***\n\n";

    bool flag = true;
    do
      {
      cout << "Enter an integer : ";
      cin >> intChoice;
      
      if(cin.fail())     //had a failure reading the input
        {
        cout << "Invalid input - try again\n";
        cin.clear();     //must reset stream after failure !!!!
        clearLine(cin);
        }
      else
        {
        clearLine(cin);
        flag = false;
        }
      }while(flag);
      
    cout << "You entered a: " << intChoice << endl;

    cout << "\nTry again?? [Y/N]: ";
    cin >> charChoice;
    charChoice = toupper(charChoice);
    }while(charChoice != 'N');
  return 0;
  }//end main
  
/**********************************************************/ 
//clearLine - consumes any and all characters up to next 
//new line, from an input stream.
/**********************************************************/ 
 void clearLine(istream& myStream)
   {
   char next;
   myStream.get(next);     //get any remaining chars from the stream
   while(next != '\n')     //reads until end of line
     myStream.get(next);
   }
/**********************************************************/