I'm working on a program to convert decimal to binary, octal and hexadecimal. I can get the code correct to convert decimal to binary and octal, but in reverse. I was 'given' the code to use for STACK class to push and pop o reverse teh order, but I'm having trouble organizing everything. I need to use a switch case to have teh user input a decimal number then chose what to convert it to (bin, octal or hex). Here's what I know so far:
Here's the stack class:
const int STACK_SIZE = 100;
class stack {
private:
int count; // number of items in the stack
int data[STACK_SIZE];
public:
stack();
~stack();
void push(const int item); // push an item on the stack
int pop(void); // pop item off the stack
};
stack::stack() // constructor
{
count = 0; // zero the stack
}
stack::~stack() {} // default destructor
void stack::push(const int item)
{
if (count < STACK_SIZE)
{
data[count] = item;
++count;
}
else cout << "Overflow!\n";
}
int stack::pop(void)
{
if (count >0)
{
--count;
return (data[count]);
}
else
{
cout << "Underflow!\n";
return 0;
}
}
This code works to convert (but in reverse):
Binary
#include<iostream>
using namespace std;
int main()
{
int num;
int total = 0;
cout << "Please enter a decimal: ";
cin >> num;
while(num > 0)
{
total = num % 2;
num /= 2;
cout << total << " ";
}
return 0;
}
Octal
#include<iostream>
using namespace std;
int main()
{
int num;
int total = 0;
cout << "Please enter a decimal: ";
cin >> num;
while(num > 0)
{
total = num % 2;
num /= 2;
cout << total << " ";
}
return 0;
}