2 Replies - 385 Views - Last Post: 04 October 2012 - 09:00 PM Rate Topic: -----

#1 danglesauce19  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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
Was This Post Helpful? 0
  • +
  • -

#3 jimblumberg  Icon User is online

  • member icon


Reputation: 4232
  • View blog
  • Posts: 13,293
  • 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
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1