Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Help I need to convert binary to octal its the only thing that I dont have HELP

ID: 3594958 • Letter: H

Question

Help I need to convert binary to octal its the only thing that I dont have HELP c++

#include <iostream>

#include <stdlib.h>

using namespace std;

void inputToBinary();

void inputToOct();

void inputToHex();

int main()

{

int menuInput, goAgain;

bool continueAgain = true;

while (continueAgain == true)

{

cout << "What do you want this to be converted into?" << endl;

cout << "1: Binary" << endl;

cout << "2: Octal" << endl;

cout << "3: Hexadecimal" << endl;

cout << "4: Exit" << endl << ">";

cin >> menuInput;

cout << endl;

switch (menuInput)

{

case 1:

inputToBinary();

break;

case 2:

inputToOct();

break;

case 3:

inputToHex();

break;

case 4:

cout << "EXITING..." << endl;

exit(0);

break;

default:

cout << "Error, please try a valid input...";

}

cout << "Do you want to do another number?" << endl;

cout << "1: Yes" << endl;

cout << "2: No" << endl << ">";

cin >> goAgain;

cout << endl;

switch (goAgain)

{

case 1:

break;

case 2:

cout << "Exit..." << endl;

continueAgain = false;

}

}

return 0;

}

void inputToBinary()

{

int userInput, extraInput, remainder;

bool negative = false;

int counter = 0, tab = 0;

int table[10000];

cout << "Enter a decimal number: " << endl << ">";

cin >> userInput;

if (userInput < 0)

{

negative = true;

userInput = userInput * -1;

}

cout << endl;

extraInput = userInput;

while (userInput != 0)

{

remainder = userInput % 2;

userInput = userInput / 2;

counter++;

}

counter--;

userInput = extraInput;

table[0] = table[counter];

while (userInput != 0)

{

remainder = userInput % 2;

userInput = userInput / 2;

table[tab] = remainder;

tab++;

}

tab--;

if (negative == true)

{

cout << "The binary number is: " << endl << "> -";

}

else {

cout << "The binary number is: " << endl << "> ";

}

while (tab >= 0)

{

cout << table[tab];

tab--;

}

cout << endl << endl;

}

void inputToOct()

{

int userInput, extraInput, remainder;

bool negative;

int counter = 0, tab = 0;

int table[10000];

cout << "Enter a decimal number: " << endl << ">";

cin >> userInput;

cout << endl;

if (userInput < 0)

{

negative = true;

userInput = userInput * -1;

}

extraInput = userInput;

while (userInput != 0)

{

remainder = userInput % 8;

userInput = userInput / 8;

counter++;

}

counter--;

userInput = extraInput;

table[0] = table[counter];

while (userInput != 0)

{

remainder = userInput % 8;

userInput = userInput / 8;

table[tab] = remainder;

tab++;

}

tab--;

cout << endl;

if (negative == true)

{

cout << "The octal number is: " << endl << "> -";

}

else {

cout << "The octal number is: " << endl << "> ";

}

while (tab >= 0)

{

cout << table[tab];

tab--;

}

cout << endl << endl;

}

void inputToHex()

{

int userInput, extraInput, remainder, a;

int counter = 0, tab = 0;

int table[10000];

bool negative;

cout << "Enter a decimal number: " << endl << ">";

cin >> userInput;

cout << endl;

if (userInput < 0)

{

negative = true;

userInput = userInput * -1;

}

cout << endl;

extraInput = userInput;

for (counter = 0; userInput != 0; counter++)

{

a = userInput % 16;

table[counter] = a;

userInput = userInput / 16;

}

counter--;

userInput = extraInput;

if (negative == true)

{

cout << "The hexadecimal number is: " << endl << "> -";

}

else {

cout << "The hexadecimal number is: " << endl << "> ";

}

for (counter - 1; counter >= 0; counter--)

{

if (table[counter] == 10)

cout << "A";

else if (table[counter] == 11)

cout << "B";

else if (table[counter] == 12)

cout << "C";

else if (table[counter] == 13)

cout << "D";

else if (table[counter] == 14)

cout << "E";

else if (table[counter] == 15)

cout << "F";

else

cout << table[counter];

}

cout << endl << endl;

}

Explanation / Answer

I couldn't figure out where should I put binaryToOct in your doe so here it is as a separate program

#include <iostream>

using namespace std;

void binaryToOct() {

string binaryInput;

cout << "Enter a binary number: " << endl << ">";

cin >> binaryInput;

int less = binaryInput.length()%3;

for (int i = 0; less!= 0 && i < 3-less; i++) {

binaryInput = "0"+binaryInput;

}

cout << binaryInput << endl;

string oct = "";

for (int i = 0; i < binaryInput.length(); i = i+3) {

int num = 4*(binaryInput[i] - '0') + 2*(binaryInput[i+1] - '0') + (binaryInput[i+2] - '0');

oct += num + '0';

}

oct.erase(0, min(oct.find_first_not_of('0'), oct.size()-1));

cout << oct << endl;

}

int main() {

binaryToOct();

}

// smalpe execution

Enter a binary number:
>0111
000111
7