Thread: so close oh my lord

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

    so close oh my lord

    okay here it is friends one last question before I give this to the torture technician that devised it
    first here is the code
    Code:
    #include<iostream>
    #include <iomanip>
    #include <string>
    using namespace std;
    
    //===============================CLASSE DEFINITION FOR STACK============================================================
    
    
    
    template <class T, int n>
    class stack
    {
    	private: T elt[n];                                                  
    			 int counter;
    	public:
    		void clearstack() {  counter = -1;                             } //INITIALIZE COUNTER
    		bool emptystack() {  return counter==-1?true:false;            } //EVALUATION IF STACK IS EMPTY
            bool fullstack()  {  return counter== n-1?true:false;          } //EVALUATION IF STACK IS FULL
    		void push(T x)    {  counter++; elt[counter] = x;              } //FUNCTION TO ADD TO STACK
    		T pop()           {  T x; x= elt[counter]; counter--;return x; } //FUNCTION TO POP THE STACK
    };
    
    
    
    
    //======================================================================================================================
    
    
    
    int main()
    {
       
     
     int  q, m, p ;	            //THREE VARIABLES M Q AND P DECLARED FOR PUSH AND POP OPERATIONS 
     char j;				    //VARIABLE J TO DETERMINE WETHER TO CONTINUE PROGRAM OR NOT
     char k;		 		    //VARIABLE K TO DETERMINE TYPE OF OPERATION BINARY OCTAL OR HEXADECIMAL 
     char z; 					//VARIALBE Z TO USE IN HEXADECIMAL OPERATIONS SPECIFICALLY
     stack <int, 32> s;         //OBJECT OF STACK TYPE DECLARED
     while(j!='n')              //BEGINNING OF WHILE LOOP TO CONTINUE PROGRAM UNTIL VARIABLE J IS EQUAL TO 'n'
     {
    
    	 
    
     cout<<"===========================Conversion Menu================================"<<endl;
     cout<<"1.Decimal to binary"<<endl;
     cout<<"2.Decimal to octal"<<endl;
     cout<<"3.Decimal to hexadecimal"<<endl;
     cout<<"4.Exit"<<endl;
     cout<<"Enter your choice: ";
     cin>>k;                   //REQUEST TO USER TO ENTER THE TYPE OF OPERATION
     switch(k)                 //SWITCH STATEMENT USED TO RUN TYPE OF OPERATION CHOSEN BY USER
     {
     //======================================================================================================================= 
    	 case '1':             //CASE 1 FOR BINARY OPERATIONS
      
    	 s.clearstack(); 
    	 
    	 cout<<"Enter a number to convert: ";
    	 cin>>m;
    	 
    	 while( !s.fullstack() )
    		{
    		 q = m % 2;        
    		 s.push(q);
    		 m =	m / 2;
    		}	
     
    	 while(!s.emptystack())
    		{
    		 p = s.pop();	
    		 cout<<setfill(char(7))<<p;
    		}
    	cout<<"\nPRESS 'y' TO CONTINUE: ";
    	cin>>j;
    	while(j !='y')
    	{
    	cout<<"\nYou entered an invalid response:";
    	cout<<"\nPlease re-enter your choice: "; 
    	cin>>j;
    	}
    
    	 break;
    //=======================================================================================================================
    	case '2':               //CASE 2 FOR OCTAL OPERATIONS
     
    	s.clearstack(); 
    	 cout<<"Enter a number to convert: ";
    	 cin>>m;
    	 while( !s.fullstack() )
    		{
    		 q = m % 8;
    		 s.push(q);
    		 m =	m / 8;
    		}	
     
    	 while(!s.emptystack())
    		{
    		 p = s.pop();	
    		 cout<<p;
    		}
    		cout<<endl;
    
    	 break;
    //=======================================================================================================================
    	case '3':                              //CASE 3 FOR HEXADECIMAL OPERATIONS
    
    	 s.clearstack(); 
    	 cout<<"Enter a number to convert: ";
    	 cin>>m;
    	while( !s.fullstack() )
    	{
    	 z = m % 16;
    				 if(z<10 || z>15)         //IF STATEMENT TO DETERMINE WETHER OR NOT CHAR OR INT VALUE DISPLAYED
    				 {
    	 				s.push(z);m = m / 16;
    				 }			
    	 
    			else if(z>=10 || z<=15)
    					{
    			 switch (z)
    						{
    			case 10: z='A'; s.push(z); m = m / 16; break; ////////////////////////////////////////////////////
    			case 11: z='B'; s.push(z); m = m / 16; break; // EMBEDDED SWITCH STATEMENT TO CONVERT			//
    			case 12: z='C'; s.push(z); m = m / 16; break; // FROM INTEGER TO CHAR VALUES FOR HEXADECIMAL    //
    			case 13: z='D'; s.push(z); m = m / 16; break; // OUTPUT											//
    			case 14: z='E'; s.push(z); m = m / 16; break; //												//
    			case 15: z='C'; s.push(z); m = m / 16; break; ////////////////////////////////////////////////////							
    						} 
    					}
    	}	
     
    	 while(!s.emptystack())
    		{
    		  
    		 p = s.pop();	
    		 
    		 if(p<9)                                        //LAST IF STATEMENT TO DETERMINE IF OUTPUT IS CHAR OR INT
    		 {
    		 cout<<(int)p;
    		 }
    		 else if(p>9)
    		 {
    		 cout<<(char)p;
    		 }
    
    		
    		}
    		cout<<"\nPRESS 'y' TO CONTINUE: ";
    	cin>>j;
    	while(j !='y')
    	{
    	cout<<"\nYou entered an invalid response:";
    	cout<<"\nPlease re-enter your choice: "; 
    	cin>>j;
    	}
    		break;
    
    	
    	case '4':
    	 
    		return 0;
    	
    	 break;
    	
    	
    	}
    	
    	
     }
     
    
    	
    	return 0;
    }
    and here is the output
    any idea how to get rid of the zeros here I was wondering if there was something in the iomanip library to cover this problem

    1.Decimal to binary
    2.Decimal to octal
    3.Decimal to hexadecimal
    4.Exit
    Enter your choice: 3
    Enter a number to convert: 987
    000000000000000000000000000003DB
    PRESS 'y' TO CONTINUE:

  2. #2
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    Well an easy way to get rid of the leading 0s is:

    First add a peek() function to your stack
    Code:
    T peek()		  {  return elt[counter]; }
    Then the following code to get rid of leading 0s before you print the result:
    Code:
    	while(!s.emptystack())
    	{
    		if(!s.peek())
    			s.pop();
    		else
    			break;
    	}
    I think you may have a bigger problem though. I tried converting 79 decimal to hex, and your program gave me 4C when the answer should be 4F.
    Last edited by bithub; 09-28-2004 at 02:24 AM.

  3. #3
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    at a second glance, your problem is pretty simple.

    case 15: z='C';

    should be changed to

    case 15: z='F';

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Close an HTTPListenerResponse.OutputStreram
    By George2 in forum C# Programming
    Replies: 0
    Last Post: 04-23-2008, 11:00 PM
  2. open() and close()
    By SoFarAway in forum C Programming
    Replies: 3
    Last Post: 04-08-2005, 01:16 PM
  3. God
    By datainjector in forum A Brief History of Cprogramming.com
    Replies: 746
    Last Post: 12-22-2002, 12:01 PM
  4. Ghost in the CD Drive
    By Natase in forum A Brief History of Cprogramming.com
    Replies: 17
    Last Post: 10-12-2001, 05:38 PM