/* Program that uses a bounded stack to convert the base-ten
 * representation of a positive integer to base two numbers
 * that have numBits bits.  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

  int numBits;                       // number of bits in binary rep.
  cout << "How many bits do you want in the binary representations? ";
  cin >> numBits;

  unsigned number,                   // the number to be converted
           remainder;                // remainder of number divided by 2
  BoundedStack<unsigned>
    stackOfRemainders(STACK_LIMIT);  // bounded stack of remainders   
  char response;                     // user response

  do
  {
    cout << "Enter positive integer to convert: ";
    cin >> number;

    int count = 0;           // counts bits in binary representation
    while (number != 0)
    {
      remainder = number % 2;
      stackOfRemainders.push(remainder);
      count++;
      number /= 2;
    }

    // Add leading 0's to obtain a representation 
    // having exactly numBits bits
    stackOfRemainders.RepeatedPush (0, numBits - count);

    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');
}

