I'm working on a program to convert decimal to binary, octal and hexadecimal. I can get the code correct to convert decimal to binary and octal, but in reverse. I was 'given' the code to use for STACK class to push and pop o reverse teh order, but I'm having trouble organizing everything. I need to use a switch case to have teh user input a decimal number then chose what to convert it to (bin, octal or hex). Here's what I know so far:
Here's the stack class:

const int STACK_SIZE = 100;
class stack {
	private:
		int count;			// number of items in the stack
		int data[STACK_SIZE];
	public:
		stack();
		~stack();
		void push(const int item);	// push an item on the stack
		int pop(void);			// pop item off the stack
	};
stack::stack()	// constructor
{
	count = 0;	// zero the stack
}
stack::~stack() {}	// default destructor
void stack::push(const int item)
{
	if (count < STACK_SIZE)
	{
		data[count] = item;
		++count;
	}
	else cout << "Overflow!\n";
}
int stack::pop(void)
{
	if (count >0)
	{
		--count;
		return (data[count]);
	}
	else
	{
		cout << "Underflow!\n";
		return 0;
	}
}

This code works to convert (but in reverse):
Binary

#include<iostream>
using namespace std;

int main()
{
	    int num;
		int total = 0;  
		cout << "Please enter a decimal: ";
		cin >> num;
		while(num > 0)
		{  
			total = num % 2;  
			num /= 2;  		 
			cout << total << " ";
		}	
			return 0;  
 }

Octal

#include<iostream>
using namespace std;

int main()
{
	    int num;
		int total = 0;  
		cout << "Please enter a decimal: ";
		cin >> num;
		while(num > 0)
		{  
			total = num % 2;  
			num /= 2;  		 
			cout << total << " ";
		}	
			return 0;  
 }

are you trying to learn C++?? or trying to code for some project??. Reason I ask is you can use lot of inbuilt methods to do such conversion

Yes, I am taking a class and learning C++.

Can someone see if I'm on the right path with this switch?

#include<iostream>
using namespace std;
int menu();
void toBinary();
void toOctal();
void toHex();

int main()
{
		int choice = menu();
		switch(choice)
		{
		case (0):
			toBinary();
			break;
		case (1):
			toOctal();
			break;
		case(2):
			toHex();
			break;
		}
		return 0;
}
int menu()
{
	int choice;
	cout << " *****Menu***** " << endl << 
	cout << "Convert the number from decimal into: " << endl;
	cout << "0-Binary" << endl;
	cout << "1-Octal" << endl;
	cout << "2-Hexadecimal" << endl;
	cin >> choice;
	return choice;
}
void toBinary()
{
	int num;
		int total = 0;  
		cout << "Please enter a decimal: ";
		cin >> num;
		while(num > 0)
		{  
			total = num % 2;  
			num /= 2;  		 
			cout << total << " ";
		}	
			return 0;  
 }

that looks good, it should work without any problems

Okay, this one works (without hex-that part's not working yet); but where do I use the stack class that I was given? Here's my code:

#include<iostream>
using namespace std;
int menu();
void toBinary();
void toOctal();
void toHex();

int main()
{
		int choice = menu();
		switch(choice)
		{
		case (0):
			toBinary();
			break;
		case (1):
			toOctal();
			break;
		/*case(2):
			toHex();
			break;*/
		}
		return 0;
}
int menu()
{
	int choice;
	cout << " *****Menu***** " << endl;
	cout << "Convert the number from decimal into: " << endl;
	cout << "0-Binary" << endl;
	cout << "1-Octal" << endl;
	cout << "2-Hexadecimal" << endl;
	cin >> choice;
	return choice;
}

void toBinary()
{
	int num;
		int total = 0;  
		cout << "Please enter a decimal: ";
		cin >> num;
		while(num > 0)
		{  
			total = num % 2;  
			num /= 2;  		 
			cout << total << " ";
		}	
}

void toOctal()
{
	int num;
		int total = 0;  
		cout << "Please enter a decimal: ";
		cin >> num;
		while(num > 0)
		{  
			total = num % 8;  
			num /= 8;  		 
			cout << total << " ";
		}	
		  
 }
//void toHex()
//{
//	int hexa = 16, deci, quotient, ctr;
//	cout << ("Please enter a decimal: "); << endl;
//	cin >> hexa;
//	
//	for (ctr = 1; ctr<=deci; ctr++)
//		quotient = deci / hexa; 
//	
//	cout << ("Equivalent in Hexadecimal is %d",quotient)" << endl;
//	cout << "(Try this for Hexadecimal:" << hex << deci << endl;   
//	
//}

see, stack is kind of queue which works as LIFO (last in first out).

While doing conversion from Decimal to Octal, do a step and push the same to stack (the command itself). After the conversion is done completely. Use the pop method to get the last performed step and perform it on the octal value. This way if you empty the stack you will perform the Octal to Decimal conversion.

Hope you now understand the use of STACK class

I understand it in theory, but I'm not sure how to implemen it in this case. I repeat it for each conversion I'm making? Once in the decimal to binary conversion, once in the octal and once in hex? (Once I get the hex formula correct-help with that while I'm at it?)I was 'given' the STACK CLASS to use so I am hesitant to change anything, do I need to change it so that I use my 'num' variable? Can you show me where to put it? My program code is above and this is the STACK code I was given...

const int STACK_SIZE = 100;
class stack {
	private:
		int count;			// number of items in the stack
		int data[STACK_SIZE];
	public:
		stack();
		~stack();
		void push(const int item);	// push an item on the stack
		int pop(void);			// pop item off the stack
	};
stack::stack()	// constructor
{
	count = 0;	// zero the stack
}
stack::~stack() {}	// default destructor
void stack::push(const int item)
{
	if (count < STACK_SIZE)
	{
		data[count] = item;
		++count;
	}
	else cout << "Overflow!\n";
}
int stack::pop(void)
{
	if (count >0)
	{
		--count;
		return (data[count]);
	}
	else
	{
		cout << "Underflow!\n";
		return 0;
	}
}

Thanks so much for your help.

Can anyone check my last post and offer me some guidance?

You call stack() to initialize the stack. You only need one stack.
You call push() to put values on the stack.
You call pop() to remove values from the stack.

That's about it...

When you calculate one of the digits, push it. When done, start popping.

Okay, I've figured out some of this. I get the stack part and I've changed my code to reflect that, but I'm still stuck on how to convert decimal to hex. Does anyone know or can look at how I've done it with binary and octal and apply that to the hex conversion? Thanks!

#include<iostream>
using namespace std;

const int STACK_SIZE = 100;
class stack {
	private:
		int count;			// number of items in the stack
		int data[STACK_SIZE];
	public:
		stack();
		~stack();
		void push(const int item);	// push an item on the stack
		int pop(void);			// pop item off the stack
	};
stack::stack()	// constructor
{
	count = 0;	// zero the stack
}
stack::~stack() {}	// default destructor
void stack::push(const int item)
{
	if (count < STACK_SIZE)
	{
		data[count] = item;
		++count;
	}
	else cout << "Overflow!\n";
}
int stack::pop(void)
{
	if (count >0)
	{
		--count;
		return (data[count]);
	}
	else
	{
		cout << "Underflow!\n";
		return 0;
	}
}
int menu();
void toBinary();
void toOctal();
void toHex();

int main()
{
		int choice = menu();
		switch(choice)
		{
		case (0):
			toBinary();
			break;
		case (1):
			toOctal();
			break;
		/*case(2):
			toHex();
			break;*/
		}
		return 0;
}
int menu()
{
	int choice;
	cout << " *****Menu***** " << endl;
	cout << "Convert the number from decimal into: " << endl;
	cout << "0-Binary" << endl;
	cout << "1-Octal" << endl;
	cout << "2-Hexadecimal" << endl;
	cin >> choice;
	return choice;
}

void toBinary()
{
	int num;
	int total = 0;
	stack reverse; // declare a local stack!!!!!!!!!!!!!!!!!!
	int ctr=0; // declare a local counter!!!!!!!!!!!!!!!
	cout << "Please enter a decimal: ";
	cin >> num;
	while(num > 0)
		{
			total = num % 2;
			num /= 2;
			//cout << total << " ";
			reverse.push(total); // save to stack instead of printing!!!!!!!!!
			ctr++; // count the number of digits saved!!!!!!!!!!!!
		}
	while (ctr > 0)
             {
				cout << reverse.pop() << " ";
				ctr--;
	         }
}

void toOctal()
{
	int num;
	int total = 0;
	stack reverse; // declare a local stack!!!!!!!!!!!!!!!!!!
	int ctr=0; // declare a local counter!!!!!!!!!!!!!!!
	cout << "Please enter a decimal: ";
	cin >> num;
	while(num > 0)
		{
			total = num % 8;
			num /= 8;
			//cout << total << " ";
			reverse.push(total); // save to stack instead of printing!!!!!!!!!
			ctr++; // count the number of digits saved!!!!!!!!!!!!
		}
	while (ctr > 0)
             {
				cout << reverse.pop() << " ";
				ctr--;
	         }
}

//void toHex()
//{
//	int hexa = 16, deci, quotient, ctr;
//	cout << ("Please enter a decimal: "); << endl;
//	cin >> hexa;
//	
//	for (ctr = 1; ctr<=deci; ctr++)
//		quotient = deci / hexa; 
//	
//	cout << ("Equivalent in Hexadecimal is %d",quotient)" << endl;
//	cout << "(Try this for Hexadecimal:" << hex << deci << endl;   
//	
//}

Decimal to octal:
Divide the number by 8, the remainder is the one's digit
Divide the new number by 8, the remainder is the eight's digit
Divide the new number by 8, the remainder is the 64th's digit
etc. until number is < 8

101 decimal
101/8 ==> 12 R5
12/8 ==> 1 R4 ==> Answer 145 octal

Thanks Walt, I appreciate your responding. I got the Octal one though...its the decimal -hex conversion I'm stuck on. I know how to do it manually, I just can't get it to convert correctly in the program. Also, do you think I should be using classes here other than the Stack class? Maybe class 'convert' with derived classes toBinary, toOctal, toHex? I'll post any progress I make. Thanks for keeping up with me!

Well, I got the code to work to do all of the conversions from decimal to binary, octal and hex. It is a bit inconsistent because I didn't use the stack on the hex conversion. Is there anything you all would do to clean this up? Or obvious coding problems? Any input is appreciated. thanks.

#include<iostream>
using namespace std;

const int STACK_SIZE = 100;
class stack {
	private:
		int count;			// number of items in the stack
		int data[STACK_SIZE];
	public:
		stack();
		~stack();
		void push(const int item);	// push an item on the stack
		int pop(void);			// pop item off the stack
	};
stack::stack()	// constructor
{
	count = 0;	// zero the stack
}
stack::~stack() {}	// default destructor
void stack::push(const int item)
{
	if (count < STACK_SIZE)
	{
		data[count] = item;
		++count;
	}
	else cout << "Overflow!\n";
}
int stack::pop(void)
{
	if (count >0)
	{
		--count;
		return (data[count]);
	}
	else
	{
		cout << "Underflow!\n";
		return 0;
	}
}
int menu();
void toBinary();
void toOctal();
void toHex();

int main()
{
		int choice = menu();
		switch(choice)
		{
		case (0):
			toBinary();
			break;
		case (1):
			toOctal();
			break;
		case(2):
			toHex();
			break;
		}
		return 0;
}
int menu()
{
	int choice;
	cout << " *****Menu***** " << endl;
	cout << "Convert the number from decimal into: " << endl;
	cout << "0-Binary" << endl;
	cout << "1-Octal" << endl;
	cout << "2-Hexadecimal" << endl;
	cin >> choice;
	return choice;
}

void toBinary()
{
	int num;
	int total = 0;
	stack reverse; // declare a local stack!!!!!!!!!!!!!!!!!!
	int ctr=0; // declare a local counter!!!!!!!!!!!!!!!
	cout << "Please enter a decimal: ";
	cin >> num;
	while(num > 0)
		{
			total = num % 2;
			num /= 2;
			//cout << total << " ";
			reverse.push(total); // save to stack instead of printing!!!!!!!!!
			ctr++; // count the number of digits saved!!!!!!!!!!!!
		}
	while (ctr > 0)
             {
				cout << reverse.pop() << " ";
				ctr--;
	         }
}

void toOctal()
{
	int num;
	int total = 0;
	stack reverse; // declare a local stack!!!!!!!!!!!!!!!!!!
	int ctr=0; // declare a local counter!!!!!!!!!!!!!!!
	cout << "Please enter a decimal: ";
	cin >> num;
	while(num > 0)
		{
			total = num % 8;
			num /= 8;
			//cout << total << " ";
			reverse.push(total); // save to stack instead of printing!!!!!!!!!
			ctr++; // count the number of digits saved!!!!!!!!!!!!
		}
	while (ctr > 0)
             {
				cout << reverse.pop() << " ";
				ctr--;
	         }
}
void toHex()
{
	int num,counter,x,a,hex[100];
	//char c[100];
	cout<<"Please enter a decimal: ";
	cin>>num;
	cout<<"\nThe Hexadecimal of "<<num<<" is: ";
	for(counter=0;num!=0;counter++)
		{
			a=num%16;
			hex[counter]=a;
			num=num/16;
			}
	for(x=counter-1;x>=0;x--)
		{
			if(hex[x]==10)
				{
					cout<<"A";
					}
			else if(hex[x]==11)
				{
					cout<<"B";
					}
			else if(hex[x]==12)
				{
					cout<<"C";
					}
			else if(hex[x]==13)
				{
					cout<<"D";
					}
			else if(hex[x]==14)
				{
					cout<<"E";
					}
			else if(hex[x]==15)
				{
					cout<<"F";
					}
			else
				{
					cout<<hex[x];
					}
			}
	}

>> obvious coding problems?

The basic functionality is somewhat lacking, e.g. toBinary() would not output anything, given the input zero -- a do/while loop might work better.

>> It is a bit inconsistent because I didn't use the stack on the hex conversion.

Why not use a stack for that too? To print the hex digits, you could write a simple function or maybe use hex manipulator (if your assignment permits).

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.