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;
}

Dani AI

Generated

Quick diagnosis: the program is taking substrings from the wrong thing and doing it before the input exists, which leads to a runtime exception (std::string::substr throws std::out_of_range when the start position is past the string length). There is also a logic bug in the loop (i += 4 skips groups) and an extremely long if/else chain where a simple lookup or small numeric conversion would do.

Correct flow and fixes (concise):

  • Read the binary input first, then validate length (16 or 32) and that every character is '0' or '1'.
  • Process the input in 4-bit groups (number of groups = input.size() / 4).
  • Convert each 4-bit group to a 0..15 value (either with bit arithmetic or stoi with base 2) and index a small hex lookup string, e.g. "0123456789ABCDEF".
  • Use a simple loop instead of 16 chained if/else and use char for single hex digits.

Example (minimal, different approach from the code already in the thread):

#include <iostream>
#include <string>

int main() {
  std::string b;
  std::cin >> b;
  if (b.size() != 16 && b.size() != 32) return 0;
  for (char c : b) if (c != '0' && c != '1') return 0;

  const char hex[] = "0123456789ABCDEF";
  for (size_t i = 0; i < b.size(); i += 4) {
    int nibble = ((b[i]-'0')<<3) | ((b[i+1]-'0')<<2) | ((b[i+2]-'0')<<1) | (b[i+3]-'0');
    std::cout << hex[nibble];
  }
  std::cout << '\n';
}

Notes tied to the thread: was right to push for a loop and a single-character hex type; is correct that the substrings must come from the input (after reading it); ’s advice to report the exact runtime error will show the out_of_range source. For clarity and robustness prefer the bitwise nibble conversion above—no big lookup table and no fragile substring positions.

Recommended Answers

All 5 Replies

#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.

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.