Stacks

Hello, I am having trouble getting my original value of an int that I use in a while loop and cant seem to display the rest of the base numbers.

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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
#include<iostream>
using namespace std;

class stack
{
private: 

	int a[8];
	int counter;

public: void clearStack() {counter = 0;}
		bool emptyStack() { if (counter == 0) return true; else return false; }
		bool fullStack() { if (counter == 8) return true; else return false; }
		void pushStack(int x) { a[counter] = x; counter++; }
		int popStack() { counter--; return a[counter]; }
};

int main()

{
	int n;
	stack s;

	s.clearStack();

	cout << "Enter a positive number: "; 
	cin >> n; 

	cout << n <<" at base 2 is: ";

	while (n != 0)
	{
		int r;
		r = n % 2; s.pushStack(r);
		n = n / 2;
	}

	while (!s.emptyStack())
	{
		int x = s.popStack();
		cout << x;
	}

	cout << endl;

	s.clearStack();

	cout << n << " at base 8 is: ";

	while (n != 0)
	{
		int r;
		r = n % 8; s.pushStack(r);
		n = n / 8;
	}

	while (!s.emptyStack())
	{
		int x = s.popStack();
		cout << x;
	}

	cout << endl;

	s.clearStack();

	cout << n << " at base 16 is: ";

	while (n != 0 )
	{
		int r;
		r = n % 16; s.pushStack(r);
		n = n / 16;
	}

	while (!s.emptyStack())
	{
		int x = s.popStack();
		cout << x;
	}
}

/*
Enter a positive number: 163
163 at base 2 is: 10100011
0 at base 8 is:
0 at base 16 is: Press any key to continue . . .
*/
You are changing the value of n. How is your base 8 section meant to know what the user input was?
Okay, should I sent n equal to another variable (for example: int n2 = n;) then use n2 for the while loops? What would be more efficient?
Okay, should I sent n equal to another variable (for example: int n2 = n;) then use n2 for the while loops?
Yes.

What would be more efficient?
Efficient for what? Memory use? It's basically free. Run time? It's basically free.
Topic archived. No new replies allowed.