/* Program that uses a bounded stack to convert the base-ten
 * representation of a positive integer to base two.  It uses 
 * an object StackOfRemainders of type BoundedStack to store 
 * and process the remainders produced by the repeated-division
 * algorithm.
 *
 * Input:  A positive integer
 * Output: Base-two representation of the number
 *         Error messages if some bits are lost
 ***************************************************************/

#include "BoundedStack" 
#include <iostream>
using namespace std;

int main()
{
  const int STACK_LIMIT = 20;        // limit on size of stack

  unsigned number,                   // the number to be converted
           remainder;                // remainder of number divided by 2
  BoundedStack<short int>
    stackOfRemainders(STACK_LIMIT);  // bounded stack of remainders   
  char response;                     // user response
  do
  {
    cout << "Enter positive integer to convert: ";
    cin >> number;

    while (number != 0)
    {
      remainder = number % 2;
      stackOfRemainders.push(remainder);
      number /= 2;
    }

    cout << "WARNING:  If a bounded stack overflow occurred,\n"
            "          some bits are missing in the following.\n";

    cout << "Base two representation: ";
    while (!stackOfRemainders.empty())
    {
      remainder = stackOfRemainders.top();
      stackOfRemainders.pop();
      cout << remainder;
    }

    cout << endl;
    cout << "\nMore (Y or N)? ";
    cin >> response;
  }
  while (response == 'Y' || response == 'y');
}

