// decimal2binary.cpp   P. Conrad for CISC181, Fall 2004

#include <iostream>
using std::cout;
using std::cin;
using std::endl;
using std::cerr;


int decimal2binary(int decValue)
{
  // convert a decimal value to a binary value
  // the binary value will actually be "decimal disguised as binary"
  // For example, to represent a five in binary (101), we will
  // construct the number one-hundred and one.

  // Note: this technique has limitations, as we shall explore!


  int binValue = 0; // the value we will return at the end
  int powerOfTen = 1; // starts at 1, then goes 10, 100, 1000, etc...


  int remainder; // each digit as we pick it off

  if (decValue > 1023)
    {
      cerr << "\n*** Error in decimal2binary function\n";
      cerr << "*** decValue=" << decValue 
	   << " which exceeds limit of 1023" << endl;
      exit(-1);
    }
 
  while(decValue > 0)
    {
      remainder = decValue % 2;
      decValue /= 2;

      // the value of remainder is each digit in the binary result,
      // starting from the least significant digit, going towards the most
      // significant digit.   Put this into the result by multiplying by
      // powerOfTen, and adding into binValue

      binValue += remainder * powerOfTen; 

      // now multiply powerOfTen by 10 to get ready for next time through loop

      powerOfTen *=10; // or: powerOfTen = powerOfTen * 10;
      

    }
  
  return binValue;
  

}

int main(void)
{
  // declare variable to store user input
  int usersDecimalValue;
  
  // prompt for input



  cout << "Enter a decimal value: ";
  cin >> usersDecimalValue;
 
  // print result

  cout << "Binary value is " << decimal2binary(usersDecimalValue) << endl;

 int anotherDecimalValue;
  
  // prompt for input

  cout << "Enter another decimal value: ";
  cin >> anotherDecimalValue;
 
  // print result

  cout << "Binary value is " << decimal2binary(anotherDecimalValue) << endl;

  return 0; // tell Unix we completed successfully

}

