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: