Stack Question

Hi,

I have to convert a number (integer) to binary.  If I use the following code...

**************************************************************************
#include <iostream.h>
#include <conio.h>
#include <windows.h>

void main()
{

      int number = 0;
      int digit = 0;
      int convert = 0;
      int choice = 0;
      bool exit = false;

      do {
            do {
                  cout << "Menu:" << endl;
                  cout << "1) Convert an integer to binary" << endl;
                  cout << "2) Exit" << endl;
                  cin >> choice;
                  system("cls");
            } while (choice != 1 && choice != 2);
      
            switch (choice) {

            case 1:
                  cout << "Please enter a number: ";
                  cin >> number;
                  cout << endl;

                  while (number > 0) {
                        digit = number % 2;
                        cout << digit;
                        number = number/2;
                  }

                  cout << endl << endl;
                  cout << "Press any key to continue..." << endl;
                  getch();
                  system("cls");
                  exit = false;
                  break;

            case 2:
                  exit = true;

            }
      } while (!exit);
   
}

**************************************************************************

This will produce a backwards binary value.  I need to use a stack to convert it.  I have the following header file...

**************************************************************************
#ifndef S__H__
#define S__H__

#include <list>
//necessary for specifying to use standard headers provided
using namespace std;



template <class bd>
class Stacks: protected list<bd>
{public:
                  Stacks ();
                  ~Stacks ();
                  void push(bd);
                  void pop();
                  bd top();
                  bool emptyS();
                  bool fullS();
};

template <class bd>
Stacks<bd>::Stacks(){}

template <class bd>
Stacks<bd>::~Stacks(){}

template <class bd>
void Stacks<bd>::push(bd item)
{
      if(!fullS())
            push_front(item);
}

template <class bd>
void Stacks<bd>::pop()
{
      if(!emptyS())
      {      iterator i = begin();
            erase(i);
      }
}

template <class bd>
bd Stacks<bd>::top()
{
      iterator i = begin();
      return *i;
}

template <class bd>
bool Stacks<bd>::emptyS()
{return empty();}

template <class bd>
bool Stacks<bd>::fullS()
{  
      //number of items in list
      //== maximum number
      return size() == max_size();
}
      
#endif
**************************************************************************

Does anyone have any ideas on how to do it (using the header file)?  Thanks.

NeoTek
LVL 1
NeoTekAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

HendrikTYRCommented:
Hi, of course it is much easier to reverse your output, BUT if you HAVE to use the provided header file then change your main() to:

void main()
{

     int number = 0;
     int digit = 0;
     int convert = 0;
     int choice = 0;
     bool exit = false;
               Stacks<int> stack;

     do {
          do {
               cout << "Menu:" << endl;
               cout << "1) Convert an integer to binary" << endl;
               cout << "2) Exit" << endl;
               cin >> choice;
               system("cls");
          } while (choice != 1 && choice != 2);
     
          switch (choice) {

          case 1:
               cout << "Please enter a number: ";
               cin >> number;
               cout << endl;

               while (number > 0) {
                    digit = number % 2;
                    //cout << digit;
                    stack.push(digit);
                    number = number/2;
               }
               while(!stack.emptyS()) {
                   cout << stack.top();
                   stack.pop();
               }

               cout << endl << endl;
               cout << "Press any key to continue..." << endl;
               getch();
               system("cls");
               exit = false;
               break;

          case 2:
               exit = true;

          }
     } while (!exit);
   
}
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Hamed ZaghaghiProgrammerCommented:
hi ,
you can use STL for stack,
using

#include "stack"
using namespace std;

and then, for example :

stack<int> st;
st.push(10);
st.push(12);
....

cout << st.top() << endl;

st.pop();

have a good programming day.


0
HendrikTYRCommented:
same thing...
remember to put :
>> cout << st.top() << endl;

>> st.pop();
in a while
0
Hamed ZaghaghiProgrammerCommented:
hi HendrikTYR,

the code above is only an example for using the satck class.

have good push and pop. ;)
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C++

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.