Question: Using C++ (specifically stacks structures of C++), write a converter that converts a binary to decimal. This is the code I have so far but

Using C++ (specifically stacks structures of C++), write a converter that converts a binary to decimal.

This is the code I have so far but I can't get it to work:

/* for this program, the algorithm I am trying to use for binary-decimal conversion is to convert each digit by different powers of 2, and then add them up to get the decimal value. For example: binary 1101 = 1*2^0 + 0*2^1 + 1*2^2 + 1*2^3 = 1 + 0 + 4 + 8 = decimal 13 I am still confused on how to use push, pop, and top.*/

#include #include #include

using namespace std;

int main() { stack intStack; cout << "the binary is 1101, convert to decimal please?" << endl; intStack.push(1); intStack.push(0); intStack.push(1); intStack.push(1);

int base = 2; // base of 2 int power = 0; // power of 2 int decimal = 0; // the decimal that binary is converted to

while (!intStack.empty()) {

decimal = intStack.top() * (base^power); power++; intStack.pop(); cout << "The decimal is: " << decimal << endl; }

}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!