Trying to convert from binary to hex but for some reason it keeps bringing up the error instead of the output

#include<iostream>
#include<string>
#include<cmath>

using namespace std;

void BintoHex (int[]);
bool CheckBin (int[], bool);
void Hexadecimal (int[]);

string hexadecimal;

void main()
{
	int bin[4];
	int binary;
	bool reentry = true;
	do	
	BintoHex(bin);
	while (CheckBin(bin, reentry)== true);
	Hexadecimal (bin);
	cin>> binary;
}

void BintoHex( int bin[])
{
	string Bina;
	int binval;
	bool loop = true;
	cout<< "Enter binary value: ";
	cin>> Bina;
	binval = Bina.length();
	
	//if(Bina.length() >= 33)
	//	cout<<"Error: Invalid character length"<<endl;
			
	do 
	{
		if (binval == 4)
		{
			for(int h = 0; h <= 4; h++)
			bin[h] = Bina[h];			
		}
	}
	while (loop = false);
}
bool CheckBin(int bin[], bool reentry)
		{
			for (int h = 0; h < 4; h++)
			if (bin[h]=='0000'||bin[h]=='0001'||bin[h]=='0010'||bin[h]=='0011'
			||bin[h]=='0100'||bin[h]=='0101'||bin[h]=='0110'
			||bin[h]=='0111'||bin[h]=='1000'||bin[h]=='1001'
			||bin[h]=='1010'||bin[h]=='1011'||bin[h]=='1100'
			||bin[h]=='1101'||bin[h]=='1110'||bin[h]=='1111')
						
			reentry = false;
			else 
			{
				reentry = true;
				h=5;
				cout<< "Error: incorrect character" <<endl;
		}
			return reentry;
}
void Hexadecimal(int bin[])
	{
		for (int i = 0; i < 4; i++)

		switch (bin[i])
			{
				case '0000':
				hexadecimal=hexadecimal+ "0 ";
				break;
				case '0001':
				hexadecimal=hexadecimal+ "1 ";
				break;
				case '0010':
				hexadecimal=hexadecimal+ "2 ";
				break;
				case '0011':
				hexadecimal=hexadecimal+ "3 ";
				break;
				case '0100':
				hexadecimal=hexadecimal+ "4 ";
				break;
				case '0101':
				hexadecimal=hexadecimal+ "5 ";
				break;
				case '0110':
				hexadecimal=hexadecimal+ "6 ";
				break;
				case '0111':
				hexadecimal=hexadecimal+ "7 ";
				break;
				case '1000':
				hexadecimal=hexadecimal+ "8 ";
				break;
				case '1001':
				hexadecimal=hexadecimal+ "9 ";
				break;
				case '1010':
				hexadecimal=hexadecimal+ "a ";
				break;
				case '1011':
				hexadecimal=hexadecimal+ "b ";
				break;
				case '1100':
				hexadecimal=hexadecimal+ "c ";
				break;
				case '1101':
				hexadecimal=hexadecimal+ "d ";
				break;
				case '1110':
				hexadecimal=hexadecimal+ "e ";
				break;
				case '1111':
				hexadecimal=hexadecimal+ "f ";
				break;
				
				default:
				cout<< "Enter binary value " ;
				
		}
		cout<<"Your binary value in hexadecimal is: " << hexadecimal <<endl;
	}

I don't understand either. Maybe you should specify what "the error" is as well as what "the input" is and what "the output" you were expecting. :icon_rolleyes:

Comments
cluless, eh?

"the error" says i'm inputting the wrong character as shown in the program, but i'm inputting binary so not sure why the check keeps making the loop false, and i'm trying to output hex values

but i'm inputting binary

No, you're inputting text. Your code is making incorrect assumptions. Try working with strings only and not worrying about conversion to int. Since you're using a switch to convert the "string" into hexadecimal anyway, it's not a big difference. For example with validating the input:

#include <iostream>
#include <string>

bool CheckBin(const std::string& bin)
{
    if (bin.size() % 4 != 0)
        return false;

    const std::string nibbles[] = {
        "0000","0001","0010","0011",
        "0100","0101","0110","0111",
        "1000","1001","1010","1011",
        "1100","1101","1110","1111"
    };

    for (int i = 0; i < bin.size(); i += 4) {
        std::string nibble = bin.substr(i, 4);
        int j;

        for (j = 0; j < sizeof nibbles / sizeof *nibbles; j++) {
            if (nibble == nibbles[j])
                break;
        }

        if (j == sizeof nibbles / sizeof *nibbles)
            return false;
    }

    return true;
}

int main()
{
    std::string bin;

    while (getline(std::cin, bin))
        std::cout<< std::boolalpha << CheckBin(bin) <<'\n';
}

Edited 5 Years Ago by Narue: n/a

This article has been dead for over six months. Start a new discussion instead.