Converting decimal to binary, octal and hexadecimal

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:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
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
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#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
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include<iostream>
using namespace std;

int main()
{
	    int num;
		int total = 0;  
		cout << "Please enter a decimal: ";
		cin >> num;
		while(num > 0)
		{  
			total = num % 8;  
			num /= 8;  		 
			cout << total << " ";
		}	
			return 0;  
 }
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
int main()
{
int hexa = 16, deci, quotient, ctr;
cout << ("enter decimal number: "); << endl;
cin >> hexa;


for (ctr = 1; ctr<=deci; ctr++)
quotient = deci / hexa; 


cout << "Equivalent in Hexadecimal is"  << quotient << endl;
cout << "Try this for Hexadecimal:" << hex << deci << endl;   

return 0;
}


Decimal to Hexadecimal system..
Last edited on
Okay, thanks for that! Am I doing this switch case right?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#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;  
 }
Can anyone help me with this? I have gotten my code to work to convert dec to binary and octal, but they are still 'cout'ing in reverse order. I need to figure out the code for converting dec to hex. Then I need to use the STACK class to correct the number order in the cout. I was given the complete STACk class as part of the assignment but I'm not sure where to put it or how to use it. I understand the push pop concept, but not the implementation. Do I use it 3 times-once for each conversion? Or??? Please help if you understand this...Here is my code followed by the STACk class.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
#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;   
//	
//}
//const int STACK_SIZE = 100;//I was given this STACK class to use.
//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;
//	}
//}



	
Last edited on
Still working here folks! Anyone with input Please respond! 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? Can anyone help me make the hex use "stack class" too? Or any obvious coding problems? Any input is appreciated. thanks.
Here is my code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
#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;
	cout << "The decimal number " << num << " converts to the binary number: ";
	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;
	cout << "The decimal number " << num << " converts to the octal number: ";
	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 Decimal number "<<num<<" converts to the Hexadecimal number:  ";
	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];
					}
			}
	}

	
Topic archived. No new replies allowed.