Program is suppose to convert binary to hex.. it says that i need a class/struct/union to the left of ".substr" but im pretty sure convert should be working..

#include <iostream>
#include <string>
using namespace std; 

int main ()



{
	string convert [8]; 
	string bin[16]= {"0000", "0001","0010","0011","0100","0101","0110","0111","1000","1001","1010","1011","1100","1101","1110","1111"};
	string hex[16]= { "0","1","2","3","4","5","6","7","8","9","A","B","C","D","E","F"}; 
	string binfinal; 
	
	convert[0]= convert.substr (0,4);
	convert[1]= convert.substr (4,4);
	convert[2]= convert.substr (8,4);
	convert[3]=	convert.substr (12,4);
	convert[4]=	convert.substr (16,4);
	convert[5]= convert.substr (20,4);
	convert[6]= convert.substr (24,4);
	convert[7]= convert.substr (28,4);

		for(int i=0; i< 8; i+=4)

		if (convert[i] == bin[0])
		{
			cout << hex[0];
		}

		else if (convert[i] == bin[1])
		{
			cout << hex[1];
		}
				

		else if (convert[i] == bin[2])
		{
			cout << hex[2];
		}
				

		else if (convert[i] == bin[3])
		{
			cout << hex[3];
		}
				

		else if (convert[i] == bin[4])
		{
			cout << hex[4];
		}
				

		else if (convert[i] == bin[5])
		{
			cout << hex[5];
		}
				

		else if (convert[i] == bin[6])
		{
			cout << hex[6];
		}
				

		else if (convert[i] == bin[7]) 
		{
			cout << hex[7];
		}
				

		else if (convert[i] == bin[8])
		{
			cout << hex[8];
		}
				

		else if (convert[i] == bin[9])
		{
			cout << hex[9];
		}
				

		else if (convert[i] == bin[10])
		{
			cout << hex[10];
		}
				

		else if (convert [i]== bin[11])
		{
			cout << hex[11];
		}
				

		else if (convert[i] == bin[12])
		{
			cout << hex[12];
		}
				
			

		else if (convert[i] == bin[13])	
		{
			cout << hex[13];	
		}
			

		else if (convert[i] == bin[14])
		{
			cout << hex[14];
		}
			

		else if (convert[i]== bin[15])
		{
			cout << hex[15];
		}
			
	}

{
	cout<<"Enter a 32 binary value: ";
	cin>>binfinal;

	
	if(binfinal.length()!=16 && binfinal.length()!=32)
	{
		cout <<"Error! Binary string is invalid length.";
	
	}
	else 
	{   
		for(int i=0; i<binfinal.length(); i++)
		{
			if(binfinal[i]!='0' && binfinal[i]!='1')
			{
				cout<<" Retry only input 0 and 1 values" << binfinal[i]<< endl;
				
		
			}
		}
	}


return 0;
}

#1) Why are you using a string to hold 1 hex character. Use a char.
#2) Make a loop instead of 16 nested IF statements.
#3) Why are you taking the substring of convert and loading it into convert? Isn't that like digging a hole and putting the dirt in the hole?

I was using char at first but then it kept telling my I had to many initializers but it works with the string... and I switched the load to conversion only now the output aborts itself for some reason

#include <iostream>
#include <string>
using namespace std; 

int main ()
{
	string convert[8];
	string conversion;
	string bin[16]= {"0000", "0001","0010","0011","0100","0101","0110","0111","1000","1001","1010","1011","1100","1101","1110","1111"};
	string hex[16]= { "0","1","2","3","4","5","6","7","8","9","A","B","C","D","E","F"}; 
	string binfinal; 
	
	convert[0]= conversion.substr (0,4);
	convert[1]= conversion.substr (4,4);
	convert[2]= conversion.substr (8,4);
	convert[3]=	conversion.substr (12,4);
	convert[4]=	conversion.substr (16,4);
	convert[5]= conversion.substr (20,4);
	convert[6]= conversion.substr (24,4);
	convert[7]= conversion.substr (28,4);

		for(int i=0; i< 8; i+=4)

		if (convert[i] == bin[0])
		{
			cout << hex[0];
		}

		else if (convert[i] == bin[1])
		{
			cout << hex[1];
		}
				

		else if (convert[i] == bin[2])
		{
			cout << hex[2];
		}
				

		else if (convert[i] == bin[3])
		{
			cout << hex[3];
		}
				

		else if (convert[i] == bin[4])
		{
			cout << hex[4];
		}
				

		else if (convert[i] == bin[5])
		{
			cout << hex[5];
		}
				

		else if (convert[i] == bin[6])
		{
			cout << hex[6];
		}
				

		else if (convert[i] == bin[7]) 
		{
			cout << hex[7];
		}
				

		else if (convert[i] == bin[8])
		{
			cout << hex[8];
		}
				

		else if (convert[i] == bin[9])
		{
			cout << hex[9];
		}
				

		else if (convert[i] == bin[10])
		{
			cout << hex[10];
		}
				

		else if (convert [i]== bin[11])
		{
			cout << hex[11];
		}
				

		else if (convert[i] == bin[12])
		{
			cout << hex[12];
		}
				
			

		else if (convert[i] == bin[13])	
		{
			cout << hex[13];	
		}
			

		else if (convert[i] == bin[14])
		{
			cout << hex[14];
		}
			

		else if (convert[i]== bin[15])
		{
			cout << hex[15];
		}
			

	cout<<"Enter a 32 binary value: ";
	cin>>binfinal;

	
	if(binfinal.length()!=16 && binfinal.length()!=32)
	{
		cout <<"Error! Binary string is invalid length.";
	
	}
	else 
	{   
		for(int i=0; i<binfinal.length(); i++)
		{
			if(binfinal[i]!='0' && binfinal[i]!='1')
			{
				cout<<" Retry only input 0 and 1 values" << binfinal[i]<< endl;
				
		
			}
		}
	}
return 0;
}

Point out the changes you've made so we don't have to scan the code.

>> the output aborts itself for some reason

The process can either run to completion and exit with return code 0 (whether you intended it to or not) or you can get a premature termination due to a run-time error, in which case you should get some type of error message and maybe a line number. What's the input, if any, what's the output, if any, what's the error message, if any, what's the line number, if any?

I added line 8 and changed lines 13-20 to have conversion loaded to convert

I think you should use:
convert[0]= binfinal.substr (0,4);

Because you want to cut the binfinal string, not the convert.
Before it, you have to ask for the input.

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