My code works for numbers that dont have a remainder of 10 or above, but whenever I get one with a remainder 10 or above it prints out the number, not the letter. Please help

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

If the code relies on printing out remainders in base 16, why don't you use the hex part of the cout?

int main(){
    int remainder=159%16;
    cout<<remainder<<endl;
    cout<<hex<<remainder<<endl;
    cout<<159%16;
    return (0);
}

This will print the letter f. For more information about printing out or working with hexadecimal check this website: Click Here

Edited 4 Years Ago by Lucaci Andrew

You caluclate the remainder.
You push the remainder on the stack.
You convert the remainder to a letter.
What do you print? Is it the value on the stack or the one you converted?

This article has been dead for over six months. Start a new discussion instead.