Thread: int to char

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

    int to char

    hi all check out the code I've put together
    Code:
    #include <iostream>
    #include <iomanip>
    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, Z;	            //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 
     int  z;					//VARIALBE Z TO USE IN HEXADECIMAL OPERATIONS SPECIFICALLY
     char p;					//VARIABLE P FOR POP OPERATIONS 
     stack <int, 10> 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;
     
    
     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<<p;
    		}
    		cout<<endl;
    
    	 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() )
    	{
    		if (m % 16< 10 || m % 16>15) 
    				 
    				 {
    	 				Z=m%16;
    					 s.push(Z);m = m / 16;
    				 }			
    	 
    	else if(m % 16>= 10 || m % 16<=15)
    			{
    	
    	z = m % 16;
    	
    	switch (z)
    				{
    	case 10: z='A'; s.push(z); m = m / 16; break;
    	case 11: z='B'; s.push(z); m = m / 16; break;
    	case 12: z='C'; s.push(z); m = m / 16;break;
    	case 13: z='D'; s.push(z); m = m / 16;break;
    	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();	
    		 cout<<p;
    		}
    		cout<<endl;
    
    	
    	case '4':
    	 
    		return 0;
    	
    	 break;
    
    	}
    	cout<<"\nPRESS 'y' TO CONTINUE: ";
    	cin>>j;
    	while(j !='y')
    	{
    	cout<<"\nYou entered an invalid response:";
    	cout<<"\nPlease re-enter your choice: "; 
    	cin>>j;
    	}
    	
     }
     
     
    }#include <iostream>
    #include <iomanip>
    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, Z;	            //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 
     int  z;					//VARIALBE Z TO USE IN HEXADECIMAL OPERATIONS SPECIFICALLY
     char p;					//VARIABLE P FOR POP OPERATIONS 
     stack <int, 10> 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;
     
    
     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<<p;
    		}
    		cout<<endl;
    
    	 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() )
    	{
    		if (m % 16< 10 || m % 16>15) 
    				 
    				 {
    	 				Z=m%16;
    					 s.push(Z);m = m / 16;
    				 }			
    	 
    	else if(m % 16>= 10 || m % 16<=15)
    			{
    	
    	z = m % 16;
    	
    	switch (z)
    				{
    	case 10: z='A'; s.push(z); m = m / 16; break;
    	case 11: z='B'; s.push(z); m = m / 16; break;
    	case 12: z='C'; s.push(z); m = m / 16;break;
    	case 13: z='D'; s.push(z); m = m / 16;break;
    	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();	
    		 cout<<p;
    		}
    		cout<<endl;
    
    	
    	case '4':
    	 
    		return 0;
    	
    	 break;
    
    	}
    	cout<<"\nPRESS 'y' TO CONTINUE: ";
    	cin>>j;
    	while(j !='y')
    	{
    	cout<<"\nYou entered an invalid response:";
    	cout<<"\nPlease re-enter your choice: "; 
    	cin>>j;
    	}
    	
     }
     
     
    }
    alright it works for characters but when it hits an integer I get ascii symbols would any of you have some suggestions on how to remedy this situation
    Last edited by drdodirty2002; 09-27-2004 at 09:53 PM. Reason: wrong question

  2. #2
    Sweet
    Join Date
    Aug 2002
    Location
    Tucson, Arizona
    Posts
    1,820
    just for one instance you can do a quick typecast eg
    Code:
    int a = 10;
    cout<<(char)a;
    Woop?

  3. #3
    Registered User manofsteel972's Avatar
    Join Date
    Mar 2004
    Posts
    317
    You can use typecast to convert int to char but a char is only valid with integers 0 to 255 after that it just wraps around ie 69 would be E and 325 would also be E just do a
    Code:
    cout<<(char)number;
    "Knowledge is proud that she knows so much; Wisdom is humble that she knows no more."
    -- Cowper

    Operating Systems=Slackware Linux 9.1,Windows 98/Xp
    Compilers=gcc 3.2.3, Visual C++ 6.0, DevC++(Mingw)

    You may teach a person from now until doom's day, but that person will only know what he learns himself.

    Now I know what doesn't work.

    A problem is understood by solving it, not by pondering it.

    For a bit of humor check out xkcd web comic http://xkcd.com/235/

  4. #4
    Sweet
    Join Date
    Aug 2002
    Location
    Tucson, Arizona
    Posts
    1,820
    Well I would help you out on your edited post but you didn't put the closing code tag
    Woop?

  5. #5
    Wannabe Geek
    Join Date
    Aug 2004
    Posts
    19

    int to char ??y :confused:

    Why would you want to do that?
    Anyway hope this helps:-
    Code:
    #include <iostream>
    
    int main()
    {
    	int int_char;
    	char C;
    	
    	cout<<"Please enter a +ve interger less than 256: ";
    	cin>>int_char;
    	while(int_char < 0 || int_char > 255)
    	{
    		cout<<"Input error ";
    		cin>>int_char;
    	}
    	C = int_char;
    	cout<<endl<<C;
    
    	return 0;
    }

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

    here is why

    the output looks like this
    ===========================Conversion Menu================================
    1.Decimal to binary
    2.Decimal to octal
    3.Decimal to hexadecimal
    4.Exit
    3
    Enter a number to convert: 987
    ♥DB
    Press any key to continue

    okay now as you can see instead of an integer I get a heart shaped ascii thingy which isn't what I need the object of the code is to convert decimal to hexadecimal

  7. #7
    Registered User manofsteel972's Avatar
    Join Date
    Mar 2004
    Posts
    317
    Don't print out numbers <10 as chars thats why your getting the funny symbols. On some ASCII charts the code page used allows you to print those graphics. if you
    Code:
    cout<<(char)3;
    you will get the same symbol. you will have to pop them off as an integer with a typecast of (int)p but only for those values under 10. [edit] disregard what is said about arrays or string it will still print the symbols until you convert to an integer.
    Last edited by manofsteel972; 09-27-2004 at 10:55 PM.
    "Knowledge is proud that she knows so much; Wisdom is humble that she knows no more."
    -- Cowper

    Operating Systems=Slackware Linux 9.1,Windows 98/Xp
    Compilers=gcc 3.2.3, Visual C++ 6.0, DevC++(Mingw)

    You may teach a person from now until doom's day, but that person will only know what he learns himself.

    Now I know what doesn't work.

    A problem is understood by solving it, not by pondering it.

    For a bit of humor check out xkcd web comic http://xkcd.com/235/

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 48
    Last Post: 09-26-2008, 03:45 AM
  2. can some one please tell me the cause of the error ?
    By broli86 in forum C Programming
    Replies: 8
    Last Post: 06-26-2008, 08:36 PM
  3. Replies: 14
    Last Post: 06-28-2006, 01:58 AM
  4. Game Won't Compile
    By jothesmo in forum C++ Programming
    Replies: 2
    Last Post: 04-01-2006, 04:24 PM
  5. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 01:27 PM