# Decimal to hexadcimal using stacks

Page 1 of 1

## 2 Replies - 507 Views - Last Post: 04 October 2012 - 09:00 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=294332&amp;s=ed2241c08f05d149e9f985b74b01101c&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 danglesauce19

Reputation: 0
• Posts: 7
• Joined: 20-February 12

# Decimal to hexadcimal using stacks

Posted 04 October 2012 - 06:08 PM

My teacher wants us to use stacks to convert decimal to hexadecimal. I get the right answer but when the remainder is 10-15 it doesn't output the letter it does the number. I moved the if statements around and still didn't work.

```#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);
number /= 16;

if (remainder==10)
{
remainder='A';
}
else if (remainder==11)
{
remainder='B';
}
else if (remainder==12)
{
remainder='C';
}
else if (remainder==13)
{
remainder='D';
}
else if (remainder==14)
{
remainder='E';
}
else if (remainder==15)
{
remainder='F';
}

}

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;
}

```

Is This A Good Question/Topic? 0

## Replies To: Decimal to hexadcimal using stacks

### #2 danglesauce19

Reputation: 0
• Posts: 7
• Joined: 20-February 12

## Re: Decimal to hexadcimal using stacks

Posted 04 October 2012 - 06:16 PM

Any help would be greatly appreciated. Thank you

### #3 jimblumberg

Reputation: 4736
• Posts: 14,768
• Joined: 25-December 09

## Re: Decimal to hexadcimal using stacks

Posted 04 October 2012 - 09:00 PM

Look at the following snippet:
```		else if (remainder==15)
{
remainder='F';
}

}

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

What is the purpose of your series of if/else/else if that ends with the first part of the above code? Why do you go through all these if/elses to assign a value to remainder and then replace that variable (remainder) with another value before you actually use the value, your pop() alters the parameter (remainder).

There is probably other problems with your printout because remainder is a int not a character.

Jim