0
#include<iostream>
using namespace std;

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;
    }
}
int menu();
void toBinary();

int main()
{
            menu();
            toBinary();
            return 0;
}
int menu()
{
    int num;
    cout << " *****convert***** " << endl;
    cout << "Convert the number from decimal into Binary" << endl;
}

void toBinary()
{
    int num;
    int total = 0;
    stack reverse; // declare a local stack!!!!!!!!!!!!!!!!!!
    int ctr=0; // declare a local counter!!!!!!!!!!!!!!!
    cout << "Please enter a decimal: ";
    cin >> num;
    cout << "The decimal number " << num << " converts to the binary number: ";
    while(num > 0)
        {
            total = num % 2;
            num /= 2;
            //cout << total << " ";
            reverse.push(total); // save to stack instead of printing!!!!!!!!!
            ctr++; // count the number of digits saved!!!!!!!!!!!!
        }
    while (ctr > 0)
             {
                cout << reverse.pop() << " ";
                ctr--;
             }
}


//i have to display it in binary number plas help
2
Contributors
1
Reply
2
Views
5 Years
Discussion Span
Last Post by deceptikon
0

What's the problem? Barring a minor semantic error (failure to return something from menu()) the program seems to work properly with a few simple test numbers.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.