ok so for some reason its telling me my "else" has no matching "if" in line 51, i have to make it so the binary output is spaced which im pretty sure i did and has to have a separate output if the character length is more than 8

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

using namespace std;

void HextoBin (char[]);
void Binary (char[]);
void CheckHex (char[]);
string binary;

void main()
{
	char hex[8];
	int hexadecimal;

	HextoBin (hex);
	Binary (hex);
	cin>> hexadecimal;
}

void HextoBin( char hex[])
{
	string Hexa;
	int hexval;

	cout<< "Enter hexadecimal value: ";
	cin>> Hexa;
	hexval = Hexa.length();

	do 
	{
		if (hexval == 8)
		{
			for(int h = 0; h <= 8; h++)
			hex[h] = Hexa[h];	
		}
	}
}
void CheckHex(char hex[])
		{
			for (int h = 0; h < 8; h++)
			if (hex[h]=='0'||hex[h]=='1'||hex[h]=='2'||hex[h]=='3'
			||hex[h]=='4'||hex[h]=='5'||hex[h]=='6'
			||hex[h]=='7'||hex[h]=='8'||hex[h]=='9'
			||hex[h]=='a'||hex[h]=='b'||hex[h]=='c'
			||hex[h]=='d'||hex[h]=='e'||hex[h]=='f'
			||hex[h]=='A'||hex[h]=='B'||hex[h]=='C'
			||hex[h]=='D'||hex[h]=='E'||hex[h]=='F')

			else
			cout<< "Error: incorrect character or length" <<endl;
		}

void Binary(char hex[])
	{
		for (int i = 0; i < 8; i++)

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

Recommended Answers

All 9 Replies

I'm the compiler. I know what you want me to do if that big long condition is false. But what shall I do if it's true? You need to tell me. That's the error.

Side note: Take a look at the xdigit function. It'll save you a lot of Logical OR (||) statements.

http://www.cplusplus.com/reference/clibrary/cctype/isxdigit/

if its true i just want the binary equivalent to output with a space separating every 4 binary digits

and thank you for that link definitely saves from all that typing

>> if its true i just want the binary equivalent to output with a space separating every 4 binary digits

Seems reasonable. But I'm seeing an empty space on line 50. If you want the computer to send something to output if the if condition true, it's more than willing to do that. You just have to tell it what you want it to do and stick that code on line 50. Right now it gets there and has no idea what you want it to do.

>> and thank you for that link definitely saves from all that typing

You're welcome.

ok i got the code to work but i added an error message to show up when the length of values entered is longer than 8 but when that happens it also displays the invalid character error message as well

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

using namespace std;

void HextoBin (char[]);
bool CheckHex (char[], bool);
void Binary (char[]);

string binary;

void main()
{
	char hex[8];
	int hexadecimal;
	bool reentry = true;
	do	
	HextoBin (hex);
	while (CheckHex(hex, reentry)== true);
	Binary (hex);
	cin>> hexadecimal;
}

void HextoBin( char hex[])
{
	string Hexa;
	int hexval;
	bool loop = true;
	cout<< "Enter hexadecimal value: ";
	cin>> Hexa;
	hexval = Hexa.length();
	
	if(Hexa.length() >= 9)
		cout<<"Error: Invalid character length"<<endl;
			
	do 
	{
		if (hexval == 8)
		{
			for(int h = 0; h <= 8; h++)
			hex[h] = Hexa[h];			
		}
	}
	while (loop = false);
}
bool CheckHex(char hex[], bool reentry)
		{
			for (int h = 0; h < 8; h++)
			if (hex[h]=='0'||hex[h]=='1'||hex[h]=='2'||hex[h]=='3'
			||hex[h]=='4'||hex[h]=='5'||hex[h]=='6'
			||hex[h]=='7'||hex[h]=='8'||hex[h]=='9'
			||hex[h]=='a'||hex[h]=='b'||hex[h]=='c'
			||hex[h]=='d'||hex[h]=='e'||hex[h]=='f'
			||hex[h]=='A'||hex[h]=='B'||hex[h]=='C'
			||hex[h]=='D'||hex[h]=='E'||hex[h]=='F')
			
			reentry = false;
			else 
			{
				reentry = true;
				h=9;
				cout<< "Error: incorrect character" <<endl;
		}
			return reentry;
}
void Binary(char hex[])
	{
		for (int i = 0; i < 8; i++)

		switch (hex[i])
			{
				case '0':
				binary=binary+ "0000 ";
				break;
				case '1':
				binary=binary+ "0001 ";
				break;
				case '2':
				binary=binary+ "0010 ";
				break;
				case '3':
				binary=binary+ "0011 ";
				break;
				case '4':
				binary=binary+ "0100 ";
				break;
				case '5':
				binary=binary+ "0101 ";
				break;
				case '6':
				binary=binary+ "0110 ";
				break;
				case '7':
				binary=binary+ "0111 ";
				break;
				case '8':
				binary=binary+ "1000 ";
				break;
				case '9':
				binary=binary+ "1001 ";
				break;
				case 'a':
				binary=binary+ "1010 ";
				break;
				case 'b':
				binary=binary+ "1011 ";
				break;
				case 'c':
				binary=binary+ "1100 ";
				break;
				case 'd':
				binary=binary+ "1100 ";
				break;
				case 'e':
				binary=binary+ "1110 ";
				break;
				case 'f':
				binary=binary+ "1111 ";
				break;
				case 'A':
				binary=binary+ "1010 ";
				break;
				case 'B':
				binary=binary+ "1011 ";
				break;
				case 'C':
				binary=binary+ "1100 ";
				break;
				case 'D':
				binary=binary+ "1100 ";
				break;
				case 'E':
				binary=binary+ "1110 ";
				break;
				case 'F':
				binary=binary+ "1111 ";
				break;

				default:
				cout<< "Enter hexadecimal value " ;
				
		}
		cout<<"Your hexadecimal value in binary is: " << binary <<endl;
	}

Seems to me all "checking" should occur in CheckHex. You're checking the length in HexoBin, a void function. The check fails, but you keep "checking" the individual characters. Why bother? Have a series of checks in CheckHex. After the first error, immediately return false or true, whichever signifies an error. No need to heck for a second error once you already know there's a first.

yea i know but unfortunately its a requirement to check both

>> yea i know but unfortunately its a requirement to check both

It's a requirement to keep checking after it's already flunked? For God's sake why? I would confirm that if I were you.

You're printing that second "error" for a variety of reasons, but you really should step back and look at the whole program. It may look like it "works" and it may actually work, but a lot of that's just dumb luck.

Line 45 - You supposedly have a "loop" here, but do you really have one and do you really need one and do you really even want one.

Line 34 - You've checked for "over". How about "under"?

Line 42 - this is a seg fault. If it doesn't crash, it's just dumb luck. Check your array indexes.

Why both having a string AND a character array? Pick one of them.

Anyway, aside from all that, before asking for input, initialize the hex[] array to something legal and see if you still get that second error message.

yea i know but unfortunately its a requirement to check both

No, it's a requirement to have a check for both. It's not a requirement to continue checking after a failure as Vernon suggests.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.