// Binary to decimal convert
// By DreamVB 23:04 18/10/2016

#include <iostream>

using namespace std;
using std::cout;
using std::endl;

int BinToDec(char *src){
	int i = 0;
	int n = 0;
	int dec = 0;
	//Get length of string src
	i = strlen(src) - 1;

	while (i > -1){
		//Check if bit is set
		if (src[i] == '1'){
			//Build decimal number
			dec = dec + pow(2, n);
		}
		//INC counters
		n += 1;
		i--;
	}
	return dec;
}

int main(int argc, char *argv[]){
	char ch = '\0';
	char bin[31];

	while (1){
		system("cls");
		cout << "Enter a binary number to convert to decimal : ";
		//Get number.
		cin >> bin;
		cout << endl;
		//Write out binary value
		cout << bin << " in decimal is : " << BinToDec(bin) << endl << endl;

		//Ask user do they want to convert agian.
		cout << "Do you want to convert another number (Y/N) : ";
		cin >> ch;

		if ((ch == 'n') || (ch == 'N')){
			cout << endl << "Thanks for trying the program." << endl;
			break;
		}
		else{
			if ((ch != 'y') && (ch != 'Y')){
				cout << "Syntex error program will now exit." << endl;
				break;
			}
		}
	}
	system("pause");
	return 0;
}