1. ## 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?

2. >> it converts the default case into an ASCII value

rather it stores the integer without converting to ASCII. try:

cStack.push(storeNum + '0');

3. Thanks. That fixed it but what is it doing and why did I have to do that?

4. Well, the ascii value for the character zero is 48, so just add the default case to 48 and you will get the matching character for the number.

5. Thanks. That fixed it but what is it doing and why did I have to do that?
A computer can only store numbers. So what happens when you want to store a character? Characters are represented by numeric codes, and a computer stores those numeric codes when you ask it to store a character. If you look at an ascii table, it lists the numeric codes for all the characters. Specifically, you should note that the character '9' is not represented by the numeric code: 9. Rather, the character '9' is represented by the numeric code: 57. Crazy, huh? Some other character is represented by the numeric code: 9.

In any case, these statements are equivalent:
Code:
```char ch;

ch = '9';
cout<<ch<<endl;

ch = 57;
cout<<ch<<endl;```
In the first assignment, C++ automatically converts the character '9' to its numeric code, which is 57, and stores that. Remember, computers can't store characters, they can only store numbers!

The problem you are having is because you are doing this:

ch = 9;

and the numeric code 9 represents some other character(something called an HT character--whatever that is).

6. I get now. Thanks a lot you guys. I went and did some studying on my own and read your posts and it helped out a lot. Thanks a ton!