// binConvert.cc   P. Conrad 9/14/2005 (for 9/16/05 lecture)
// demonstrate how to convert decimal to binary
// output is a C-string

#include <iostream>
using namespace std;

void convertToBinary(int decimalNumber, char binaryNumber[9])
{

  for (int i=0; i<8; i++)
    binaryNumber[i] = '0';

  binaryNumber[8] = '\0';

  // at this point, the array contains the digit 0 in positions
  // 0 through 7 representing eight bits, and there is a \0 in
  // position 8, representing the "stop sign" that tells the
  // system to stop printing out the string.
  // so binaryNumber contains "00000000\0"

  // our next job is to repeatedly use % and / to find out
  // where the ones are in the binary version of decimalNumber.
  // Each time we find one, we'll change that bit to be a '1'
  // instead of a zero.  We work from the least significant digit
  // to the most significant digit.

  // @@@ FIX ME!!!


}



int main(void)
{
  int usersDecimalNumber;
  char binaryVersion[9];

  // Note: this version has magic numbers in it, which is a bad 
  // programming practice... finish this version, then show a "better" version

  cout << "Please enter a decimal number between 0 and 255: " << endl;
  cin >> usersDecimalNumber;

  if (usersDecimalNumber < 0 || usersDecimalNumber > 255)
    {
      cerr << "Sorry, your number " << usersDecimalNumber << " is out of range"
	   << endl; // cerr (pronounced "see air") is for error messages
      exit(1); // we use 1 not 0 because "non-zero" indicates a problem
    }

  convertToBinary(usersDecimalNumber, binaryVersion);
  
  cout << "Your number in decimal is: " << usersDecimalNumber << endl;
  cout << "Your number in binary is : " << binaryVersion << endl;


  return(0);

}


