stacks to convert from decimal to hexadecimal

This is a discussion on stacks to convert from decimal to hexadecimal within the C++ Programming forums, part of the General Programming Boards category; Pardon me chaps, We are using stacks to convert a number, lets say 103, for instance to binary octal and ...

  1. #1
    Registered User
    Join Date
    Apr 2003
    Posts
    78

    stacks to convert from decimal to hexadecimal

    Pardon me chaps, We are using stacks to convert a number, lets say 103, for instance to binary octal and hexadecimal. I am a bit perplexed by the whole situation and I was wondering if you blokes could perhaps share a little bit of your insight with me.I feel as though I've mastered the binary portion of the program.
    Code:
    #include <iostream>
    #include <iomanip>
    using namespace std;
    
    
    template <class T, int n>
    class stack
    {
    	private: T elt[n];
    			 int counter;
    	public:
    		void clearstack() {  counter = -1;                            }
    		bool emptystack() {  return counter==-1?true:false;            }
            bool fullstack()  {  return counter==n -1?true:false;          }
    		void push(T x)    {  counter++; elt[counter]=x;                }
    		T pop()           {  T x; x=elt[counter]; counter--;return x;  }
    };
    
    
    
    
    void main()
    {
     stack <int,7> s;
     
     int m,n,p;
     s.clearstack();
     
     cout<<"Enter a number to convert: ";
     
     cin>>m;
     
     while( !  s.fullstack() )
    {
     
    	
    	 n = m % 2;
    		  
    	 s.push(n);
    
    	 m =	m / 2;
    
    }	
     
        while(! s.emptystack())
    	{
    	 p = s.pop();	
    	 
    	 cout<<p;
    	}
    	cout<<endl;
    }
    So if you could help me out with the whole hexadecimal portion of this program I would be very appreciative thank you very kindly.
    (english enough for you sean)
    Last edited by drdodirty2002; 09-25-2004 at 09:16 PM.

  2. #2
    Super Moderator
    Join Date
    Sep 2001
    Posts
    4,913
    Would you please edit your post and start using English in what we like to call, "the right way". I can't even get past your first sentence.

  3. #3
    Software Developer jverkoey's Avatar
    Join Date
    Feb 2003
    Location
    University of Waterloo
    Posts
    1,903
    punctuation always helps

    But I think what you're looking for could be found with a good board search here, or even a....Google search *gasp*
    Last edited by jverkoey; 09-25-2004 at 09:08 PM.

  4. #4
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,654
    > void main()
    Read the FAQs, watch the Avatar, you know its wrong.

    > I feel as though I've mastered the binary portion of the program.
    > n = m % 2;
    So what's the big jump to %8 for octal and %16 for hex ?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. convert from Single to Decimal
    By George2 in forum C# Programming
    Replies: 0
    Last Post: 06-11-2008, 06:13 AM
  2. Convert DegMinSec to Decimal Degrees
    By JacquesLeJock in forum C Programming
    Replies: 3
    Last Post: 11-21-2007, 10:59 PM
  3. how to convert decimal to hexa decimal in C/
    By kalamram in forum C Programming
    Replies: 4
    Last Post: 09-03-2007, 07:39 AM
  4. Replies: 10
    Last Post: 08-17-2005, 11:17 PM
  5. decimal to binary, decimal to hexadecimal and vice versa
    By Unregistered in forum C++ Programming
    Replies: 9
    Last Post: 12-08-2001, 10:07 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21