I have defined a stack from the STL like this:
Now I use this stack in an algorithm to convert a decimal number to hexadecimal format. This is the algorithm I have.Code:stack<char> cStack;
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?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(); } }
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?



LinkBack URL
About LinkBacks


