0

Hey guys I am currently writing a code that will take the user input of a number and convert it to its hexadecimal form with the use of Stack ADT. This is what I have so far and would appreciate any help.

#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include "stack.h"

int main(void)
{
int num;
char alpha;
int* digit;
STACK* stack;

stack = createStack ();

printf("Please enter your integer (decimal): \n");
scanf("%d" , &num);

while(num > 0)
{
	digit = (int*) malloc (sizeof(int));
	*digit = num % 16;
	push (stack, digit);
	num = num/16;
}

while (!emptyStack (stack))
{
	digit = (int*)popStack (stack);
	
	if(*digit > 9)
	{
		if(*digit == 10)
		alpha = 'A';
		else if(*digit == 11)
		alpha = 'B';
		else if(*digit == 12)
		alpha = 'C';
		else if (*digit == 13)
		alpha = 'D';
		else if (*digit == 14)
		alpha = 'E';
		else if (*digit == 15)
		alpha = 'F';
	}
	
	printf("%c", alpha);

	else
		printf("%d", digit);	
}

destroyStack(stack);
return 0;
}
2
Contributors
1
Reply
4
Views
6 Years
Discussion Span
Last Post by WaltP
0

Hey guys I am currently writing a code that will take the user input of a number and convert it to its hexadecimal form with the use of Stack ADT. This is what I have so far and would appreciate any help.

With what? It's helpful to explain in full what the problem is rather than make us guess.

Edited by WaltP: n/a

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.