STL Stack problem

This is a discussion on STL Stack problem within the C++ Programming forums, part of the General Programming Boards category; I have defined a stack from the STL like this: Code: stack<char> cStack; Now I use this stack in an ...

  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.

  2. #2
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    >> it converts the default case into an ASCII value

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

    cStack.push(storeNum + '0');
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  3. #3
    Registered User
    Join Date
    Mar 2006
    Location
    Ohio
    Posts
    37
    Thanks. That fixed it but what is it doing and why did I have to do that?

  4. #4
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    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. #5
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    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. #6
    Registered User
    Join Date
    Mar 2006
    Location
    Ohio
    Posts
    37
    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!

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, 03: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

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