HELP ASAP! I need to submit this program right now, but its not properly running
ID: 3720262 • Letter: H
Question
HELP ASAP! I need to submit this program right now, but its not properly running. I need to have no leading zeros as an output after 0b. Except it is okay when I do 0x0 output 0b0. For instance, I should get 0x7d output 0b1111101 and it should not output 0b01111101. When you input 0x3E its output should be 0b111110 and not 0b00111110. When I input 0x1f I should get 0b11111. When making changes make sure that you always have 0x0 to output 0b0. Please help!
#include <iostream>
#include <string>
using namespace std;
int main() {
string hexa, value; // Hexadecimal and binary numbers in string
bool void_num = false;
cout << "Enter a hexadecimal number: "<<endl;
getline(cin, hexa);
value = ""; // Empty string is assigned value
// since the first 2 character of hexadecimal starts from 0x, ignore them during conversion
for (size_t i = 2; i<hexa.length(); i++)
{
if (void_num) // if any character is invalid exit from loop
break;
// switch command to convert the hexadecimal character to binary strings
switch (hexa.at(i))
{
case '0': value = value + "0000";
break;
case '1': value = value + "0001";
break;
case '2': value = value + "0010";
break;
case '3': value = value + "0011";
break;
case '4': value = value + "0100";
break;
case '5': value = value + "0101";
break;
case '6': value = value + "0110";
break;
case '7': value = value + "0111";
break;
case '8': value = value + "1000";
break;
case '9': value = value + "1001";
break;
case 'a':
case 'A': value = value + "1010";
break;
case 'b':
case 'B': value = value + "1011";
break;
case 'c':
case 'C': value = value + "1100";
break;
case 'd':
case 'D': value = value + "1101";
break;
case 'e':
case 'E': value = value + "1110";
break;
case 'f':
case 'F': value = value + "1111";
break;
default: cout << " Invalid hexadecimal number";
void_num = true;
break;
}
}
// if number is not invalid then remove the leading zeros from binary numbar and add 0b at the start
if (!void_num)
{
if (hexa.at(2) == '1' && hexa.length() > 3) {
for (size_t i = 0; i<value.length(); i++)
{
if (value.at(i) != '0')
break;
else {
value.erase(i, 1);
i--;
}
}
}
if (value == "0000") {
value = "0";
}
value = "0b" + value;
cout << "Your number in binary is " << value;
cout << "." << endl;
}
return 0;
}
Explanation / Answer
#include <iostream>
#include <string>
using namespace std;
int main() {
string hexa, value; // Hexadecimal and binary numbers in string
bool void_num = false;
cout << "Enter a hexadecimal number: "<<endl;
getline(cin, hexa);
value = ""; // Empty string is assigned value
// since the first 2 character of hexadecimal starts from 0x, ignore them during conversion
for (size_t i = 2; i<hexa.length(); i++)
{
if (void_num) // if any character is invalid exit from loop
break;
// switch command to convert the hexadecimal character to binary strings
switch (hexa.at(i))
{
case '0': value = value + "0000";
break;
case '1': value = value + "0001";
break;
case '2': value = value + "0010";
break;
case '3': value = value + "0011";
break;
case '4': value = value + "0100";
break;
case '5': value = value + "0101";
break;
case '6': value = value + "0110";
break;
case '7': value = value + "0111";
break;
case '8': value = value + "1000";
break;
case '9': value = value + "1001";
break;
case 'a':
case 'A': value = value + "1010";
break;
case 'b':
case 'B': value = value + "1011";
break;
case 'c':
case 'C': value = value + "1100";
break;
case 'd':
case 'D': value = value + "1101";
break;
case 'e':
case 'E': value = value + "1110";
break;
case 'f':
case 'F': value = value + "1111";
break;
default: cout << " Invalid hexadecimal number";
void_num = true;
break;
}
}
if (!void_num)
{
// changes here!!
if (value == "0000") {
value = "0";
}
else{
while(value.at(0)=='0'){
value.erase(0,1);
}
}
value = "0b" + value;
cout << "Your number in binary is " << value;
cout << "." << endl;
}
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.