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
using namespace std;
int main() { stack
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
Get step-by-step solutions from verified subject matter experts
