#include <iostream>
#include <cctype>

const int READTYPE = 1;    // set to 0 for "cin >> val;" 

// The following function will read in input and return the
// next integer in the input stream.  If non-integer input
// is encountered, an error message is printed.
int readNextInt(char &ch)
{
 int num = 0;
 bool hasNeg = false;
 bool reportedError = false;

 // get input until the next digit character is read in
 while (!isdigit(ch))
    {
     // report error (only one error) for non-integer input
     if (reportedError == false && 
         (!isspace(ch) && ch != '-' || hasNeg== true))
        {
         cout << "Error: input was not an integer.  "
              << "Skipping not-integer input." << endl;
         reportedError = true;
        }
    
     // check for minus sign to indicate negative input
     if (ch == '-')
        hasNeg = true;  // could be "hasNeg = !hasNeg;"
     else
        hasNeg = false;

     // read next character
     cin.get (ch);
    }

 num = 0;

 // process the digits
 while (isdigit(ch))
    {
     // translate the character to the proper integer with "ch - '0'"
     // increament the order of magnitude with "num * 10"
     num = num * 10 + ch - '0';
     cin.get(ch);
    }

 // change sign if negative exists
 if (hasNeg == true)
    num = -num;

 // return the value
 return num;
}

// the following is a floating point version of the same code
float readNextFloat(char &ch)
{
 float num = 0.0;
 bool hasNeg = false;
 bool  reportedError = false;
 float place;
 bool hasDigit = false;

 while (hasDigit == false)
    {

     while (!isdigit(ch) && ch != '.')
        {
         if (reportedError == false && 
             (!isspace(ch) && ch != '-' || hasNeg== true))
            {
             cout << "Error: input was not an integer.  "
                  << "Skipping not-integer input." << endl;
             reportedError = true;
            }
    
         // check for a minus sign for a negative number
         if (ch == '-')
            hasNeg = true;
         else
            hasNeg = false;

         cin.get (ch);
        }

     num = 0.0;

     // process the digits
     while (isdigit(ch))
        {
         hasDigit = true;
         num = num * 10 + ch - '0';
         cin.get(ch);
        }

     // check for floating point
     if (ch == '.')
        {
         cin.get (ch);
         place = 10.0;

         while (isdigit(ch))
            {
             hasDigit = true;
             num = num + (ch - '0')/place;
             place = place * 10;
             cin.get(ch);
            }
        }

     // change sign if negative exists
     if (hasNeg == true)
        num = -num;

     // if hasDigit == false, then only a period was entered
     if (reportedError == true && hasDigit == false)
        {
         cout << "Error: input was not an integer.  "
              << "Skipping not-integer input." << endl;
         reportedError = true;
        }
    } // end of while (hasDigit == false)

 // return the value
 return num;
}


main()
{
 int val;
 char ch = '\n';

 // prompt for input
 cout << "Enter an integer (9 to quit): ";
 if (READTYPE == 0)
    cin >> val;
 else
    val = readNextInt(ch);

 while (val != 9)
    {
     // display the value entered
     cout << "The value entered was : " << val << endl << endl;

     // prompt for input
     cout << "Enter an integer (9 to quit): ";
     if (READTYPE == 0)
        cin >> val;
     else
        val = readNextInt(ch);
    }

 cout << "Goodbye" << endl;
}