// CIS 213 lab 1a. Converts decimal numbers to any base 2-16
// Asks for: A decimal number, a base. Converts and shows that
// number in that base. Asks if user wants to convert another.
// 				FtG 8/97-9/97

// Student task: Fill in body of loop in ConvertBase function.

#include <iostream.h>			// for cin/cout

const int MaxBits = 64;		// largest number of digits/bits
const int MaxBase = 16;		// maximum base is 16
const char Digits[]="0123456789ABCDEF";  // first 16 digits

// Digit2Char converts ADigit into its character form.
// ADigit must be from 0 to 15 in base 10. Uses table lookup.
char Digit2Char(int ADigit)
{
   return Digits[ADigit]; 	// lookup char equiv. in Digits and return it
} // end Digit2Char

// ConvertBase converts the OldNum into the NewBase. The digits
// of the new number are stored as chars in DigitsNew.
// It returns a pointer to the first digit of the number in DigitsNew
// Assumes DigitsNew is MaxBits+1 long with a null char at end
char* ConvertBase( int OldNum, int NewBase, char DigitsNew[])
{

   int i = MaxBits;	// starting filling in digits from _end_ of array
   int NextDigit;	// a Digit in the new base
   
   // conversion method is by repeated division by new base
   // remainders are the digits of the number in _reverse_ order
   while ( OldNum > 0)	// while not out of digits
   {
      --i;			// i moves towards _front_ of array
      *********************	// the value of next (as move left) digit
      *********************	// convert and store char equiv.
      *********************	// get smaller number
   }
   return &DigitsNew[i];     	// return pointer to first digit
} // end ConvertBase

// main: Asks for/gets number and base, converts and writes out in new base
// Asks if user wants to convert another.
void main()
{
  int Number;		// The number to convert
  int Base;		// The base of the new number
  char NewDigits[MaxBits+1];  // The char digits of the new number
  char YesNo;		// response for "convert another?"

  NewDigits[MaxBits] = '\0';	// make sure string ends with null byte

  // output header info
  cout << "This program converts a decimal number you type in into any given\n"
       << " base up to 16.\n";
  do	// for each number to convert
  {
     do // get number and check >0
     {
       // ask for, get, check number to convert
       cout << "What decimal number do you want to convert? (>0) ";
       cin >> Number;
     } while ( Number <= 0 );
     do // get base and check >=2 and <=16
     {
       // Ask for, get and check base
       cout << "What base do you want to convert it to? [2-16] ";
       cin >> Base;
     } while (Base < 2 || Base > 16);

     // Convert and output number in new base
     cout << Number << " is " 
          << ConvertBase(Number, Base, NewDigits)
          << " in base " << Base << endl;
     // ask for, get, check of user wants to convert another
     cout << "Do you want to convert another? [y/n] ";
     cin >> YesNo;
   } while (YesNo == 'Y' || YesNo == 'y');

   cout << "Good day and remember to think Hexadecimal.\n";
} // end main


