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

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


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

const int MaxBits = 64;			// largest number of digits/bits

// Char2Digit converts char ADigit into its numeric form
// ADigit must be from 0 to 9, a to f, or A to F.
// Returns -1 if bad char.
int Char2Digit(char ADigit)
{
   if(ADigit >= '0' && ADigit <= '9')	// 0-9?
     return int(ADigit - '0');		// convert and return
   if(ADigit >= 'A' && ADigit <= 'F')	// A-F?
     return int(ADigit - 'A')+10;	// convert and return
   if(ADigit >= 'a' && ADigit <= 'f')	// a-f?
     return int(ADigit - 'a')+10;	// convert and return
   return -1;				// else bad char, return -1
} // end Char2Digit

// ConvertBase converts the base OldBase DigitsOld number into numeric.
// It returns the numeric equivalent (base 10?).
// Assumes DigitsOld a string with null char at end
// Tells user if any "digits" are bad and skips them
int ConvertBase(int OldBase, char DigitsOld[])
{
   int NewNum = 0;	// The DigitsOld number in base 10(?)
   int NextDigit;	// a Digit in numeric form
   
   // for each digit in DigitsOld up to null byte
   // NOTE:  we are going LEFT to RIGHT through the number
   for(int i=0; DigitsOld[i] != '\0'; ++i)
   {
      NextDigit = **********(DigitsOld[i]);	// convert the next digit
      if( NextDigit >= 0 && NextDigit < OldBase) // good digit?
        ********************************	// add digit to number
      else 	// bad digit, print error message and skip
        cout << "You typed in a bad character, " << DigitsOld[i]
             << " skipping.\n";
   } // end for each digit
   return NewNum;		// return the int form of the number
} // end ConvertBase

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

  // output header info
  cout << "This program converts a number in any base 2-16 you type"
          " in into \n"
       << " base 10. End each input with a newline.\n";
  do	// for each number to convert
  {
     do // get base and check >=2 and <=16
     {
       // Ask for, get and check base
       cout << "What base do you want to convert from? [2-16] ";
       cin >> Base;
     } while (Base < 2 || Base > 16);

     // ask for, get number to convert
     cout << "What number do you want to convert? (>=0) ";
     cin.getline(OldDigits, 1);		// skips over newline from prev. cin
     cin.getline(OldDigits, MaxBits);

     // Convert and output number in base 10
     cout << "Converting " << OldDigits << " in base " << Base << endl
          << ConvertBase(Base, OldDigits)
          << " is its value in base 10.\n";
     // 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 Binary.\n";
} // end main


