Thread: STL Stack problem

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Registered User
    Join Date
    Mar 2006
    Location
    Ohio
    Posts
    37

    STL Stack problem

    I have defined a stack from the STL like this:

    Code:
    stack<char> cStack;
    Now I use this stack in an algorithm to convert a decimal number to hexadecimal format. This is the algorithm I have.

    Code:
    void decToHex(int num){
    	int showNum = num;
    	int storeNum;
    	
    	while(num != 0){
    		storeNum = num % 16;
    		switch(storeNum){
    			case 10:
    				cStack.push('A');
    				break;
    			case 11:
    				cStack.push('B');
    				break;
    			case 12:
    				cStack.push('C');
    				break;
    			case 13:
    				cStack.push('D');
    				break;
    			case 14:
    				cStack.push('E');
    				break;
    			case 15:
    				cStack.push('F');
    				break;
    			default:
    				cStack.push(storeNum);
    				break;
    		}
    		num = num / 16;
    	}
    	
    	cout << showNum << " in hexadecimal is ";
    	while(!cStack.empty()){
    		cout << cStack.top();
    		cStack.pop();
    	}
    }
    The program this algorithm belongs to compiles fine and runs great but the problem is that since the type of the stack is of a char type, it converts the default case into an ASCII value and doesn't print the number like I need it to. Any suggestions on how to correct this?

    Another question is I had an idea of making the stack dual types using a union that had an integer and char in it. Is this a legitimate idea?
    Last edited by xmltorrent; 04-03-2006 at 05:43 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. STL problem
    By ltanusaputra in forum C++ Programming
    Replies: 3
    Last Post: 09-21-2007, 10:56 AM
  2. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 02:51 PM
  3. stl containers allocation in heap or stack?
    By sawer in forum C++ Programming
    Replies: 9
    Last Post: 08-06-2006, 03:08 PM
  4. Stack problem
    By silicon in forum C++ Programming
    Replies: 3
    Last Post: 11-11-2003, 04:30 PM
  5. overloading new/delete with STL problem?
    By Unregistered in forum C++ Programming
    Replies: 4
    Last Post: 06-23-2002, 03:17 PM