Having issues with stacks

Hello all, I am new to here, but not so new to programming. I'm no expert which is why I am here to seek help. I'm working on a number converting program which will convert a base 10 number to any other base that the user selects.

The way I initially went about this problem would be is every time the function would find the remainder it would cout that remainder, unfortunately this was giving me output in reverse order. Then a friend suggest I try using stacks. Here's my problem I think I've set up the stacks correctly, but every time I run the program it hangs or stalls at the point where it should display the converted number. To help I shall post snippets of my code.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#include <iostream>
#include <string>
#include <stack>
using namespace std;

void converter (long long& number, int& base);

int main()
 {
	long long	number;			//This is the number that the user wants to convert
	int			base;			//This isthe base that the user wants to convert their number to
	int			choice;			//This is the variable used in the opening menu
	bool		numbert;		//This is the boolean variable used to say whether the number / base entered in by the user is valid
	bool		answer = true;	//This is the boolean variable used to say whether the program should keep running or should stop.

	while (answer == true)		//This is to say while answer == true keep running the program
	{
		system ("cls");
		cout << "Welcome to the decimal (base 10) number converter program  by Cole Feeser & Jeff Anderson." << endl << endl;
		cout << "What would you like to do?: " << endl << "1. Convert a decimal (base 10) number." << endl << "2. Get more info about the different number bases." << endl << "3. Get more info about this program and it's creators." << endl << "4. Exit the program." << endl << endl;
		cout << "Answer: ";
		cin >> choice;			//This is where the user decides what they want to do

		if (choice == 1)
		{
			numbert = true;
			while (numbert == true)
			{
				system ("cls");
				cout << "Please enter a whole decimal (base 10) number: ";
				cin >> number;	//The user enters their number here
				cout << "Please enter a base between -36 and 36 (except -1 (For all numbers), 0 (For all numbers), or 1 (Only for numbers <= 0)) to have your number converted to: ";
				cin >> base;	//The user enters the base they want to convert to, here
				numbert= false;	//This makes the numbers appear valid to the program

				if (base == 0 || base == -1 || (base == 1 && number <= 0))
				{
					numbert = true;	//This changes the numbert variable to say that the number / base inputted by the user is invalid
					cout << endl << "The number and/or base you entered is invalid. Please enter a new number and base." << endl << endl;
					system ("pause");
				}
			}
			cout << endl << "Your number " << number << " converted to base " << base << " is: ";
			converter (number, base);	//This is the line where the program shifts into the main functions below to make the necessary conversion.
		}


1
2
3
4
5

void converter (long long& number, int& base)
{
	stack <char>	convertBase;
	long long		remainder;


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
	else if ((-2 >= base && base >= -10) || (2 <= base && base <= 10))
	{
		if ((number < 0) && (2 <= base && base <= 10))
		{
			number = -number;
			cout << "-";
		}
		while (number != 0)
		{
			remainder = (number % base);
			if (remainder < 0)
			{
				remainder = remainder + (-base);
				number = (number / base) + (1);
			}
			else
			{
			number = (number / base);
			}
			convertBase.push(remainder);
		}
		while(!convertBase.empty());
		{
			cout << convertBase.top();
			convertBase.pop();
		}
		cout << endl << endl;
		system ("pause");
	}


Anyone know why this isn't working, and can anyone help me fix this. I appreciate everyone's help and do forgive my noobness, thank you.
Last edited on
Not sure on this one, but my guess would be that the stack is of char, so when you push the remainder (eg 3) the compiler pushes the number 3 not '3'.
The 'quick fix' for this would be to change
convertBase.push(remainder);
to
convertBase.push(remainder+48);
so that 3 becomes 51 which is the character code for '3'
You would also need to check for remainder > 9 if using a base >10, at which point you need to add 55 ('A' = 65).

NEVERMIND - DUH! I figured it out, a careful observer would have seen that I was putting semicolons at the end of the while statements. *smacks head*

Now that I solved that problem, and fixed up a few other things this program works perfectly like I want it to. This topic can now be closed.
Last edited on
Topic archived. No new replies allowed.