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