### 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
 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118`` ``````#include #include 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; }``````
 ``123456789101112131415161718192021222324252627282930`` `````` 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:

 ``123456789101112131415161718192021222324252627282930313233`` ``````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.