c++ stacks converting decimal to hexadecimal

Okay so all I need help with is the part where the remainder is 10-15 it outputs the number not the letter. I'm new with stacks and my teacher wants us to use this stack example that he gave us and write the conversion ourselves for decimal to octal and decimal to hex. I have already gotten the octal to work doing it the same way without the if statements. Please help all the other websites I've uses suck and tell me to do things that make no sense and would be too complicated for my level. Thank you
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
#include <iostream>
#include <fstream>
using namespace std;
const int maxstack = 51;
class stack_type
{
public:
	void clear_stack();
	bool empty_stack();
	bool full_stack();
	void push (int numb);
	void pop (int& numb);

	int stack[maxstack];
	int top;
};

void main()
{
	stack_type remainder_stack; 
	int n, number, remainder;
	char response;
	remainder_stack.clear_stack();
	do
	{
	cout << "Enter positive integer to convert to base 16: \n\n";
	cin >> number;
	n = number;
	cout << number << "\n";
	while (number != 0)
	{
		remainder = number % 16;
		
		remainder_stack.push(remainder);
		if (remainder==10)
		{
			remainder='A';
			remainder=number;
		}
		else if (remainder==11)
		{
			remainder='B';
			remainder=number;
		}
		else if (remainder==12)
		{
			remainder='C';
			remainder=number;
		}
		else if (remainder==13)
		{
			remainder='D';
			remainder=number;
		}
		else if (remainder==14)
		{
			remainder='E';
			remainder=number;
		}
		else if (remainder==15)
		{
			remainder='F';
			remainder=number;
		}
		number /= 16;
	}
	
	cout << "Base 16 representation of " << n << " is ";
	while (!remainder_stack.empty_stack())
	{
		remainder_stack.pop(remainder);
		cout << remainder;
	}

	cout << endl;
	cout << "\nMore (Y or N)? :";
	cin >> response;
	}
	while (response=='Y' || response=='y');
}

void stack_type::clear_stack()

{
	top=0;
}

bool stack_type::empty_stack()

{
	if (top==0)
		return true;
	else
		return false;
}

bool stack_type::full_stack()

{
	if (top==maxstack-1)
		return true;
	else 
		return false;
}

void stack_type::push (int numb)

{
	top = top + 1;
	stack[top] = numb;
}

void stack_type::pop (int& numb)

{
	numb = stack[top];
	top = top - 1;
}
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
		if (remainder==10)
		{
			remainder='A';
			remainder=number;
		}
		else if (remainder==11)
		{
			remainder='B';
			remainder=number;
		}
		else if (remainder==12)
		{
			remainder='C';
			remainder=number;
		}
		else if (remainder==13)
		{
			remainder='D';
			remainder=number;
		}
		else if (remainder==14)
		{
			remainder='E';
			remainder=number;
		}
		else if (remainder==15)
		{
			remainder='F';
			remainder=number;
		}


All of this code does nothing. You can completely remove it without changing the behavior of your program. If we do then main looks like this:

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
void int main()
{
    stack_type remainder_stack; 
    int n, number, remainder;
    char response;
    remainder_stack.clear_stack();
    do
    {
        cout << "Enter positive integer to convert to base 16: \n\n";
        cin >> number;
        n = number;
        cout << number << "\n";

        while (number != 0)
        {
            remainder = number % 16 ;
            remainder_stack.push(remainder);
            number /= 16;
        }

        cout << "Base 16 representation of " << n << " is ";
        while (!remainder_stack.empty_stack())
        {
            remainder_stack.pop(remainder);
            cout << remainder ;
        }

        cout << endl;
        cout << "\nMore (Y or N)? :";
        cin >> response;
    }
    while (response=='Y' || response=='y');
}


There are two things you need to change to make this work. You need to add an array in there:

char hex_digits[] = "0123456789ABCDEF" ;

And then, what is line 25 in the code just above can be changed to:

cout << hex_digits[remainder] ;

We know remainder is a number from 0 to 15, so we just use that as an index into the array to get the correct character to output.
Last edited on
Thank you so much I'll see what I can do
Topic archived. No new replies allowed.