ADT Stack

Im trying to learn about stacks as an abstract data type creating my own. my code complies but im getting a logical error when displayed I keep getting a 12 or some random number im thinking there is a problem with my printStack function with displaying the copy of the stack from the for loop




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
#include<iostream>
using namespace std;

class Stack
{
public:
	Stack()
	{
		items = 0;
		count = 0;
	}
	bool isEmpty() // returns true if stack is empty
	{
		return count == 0;
	}
	bool isFull()// return true if stack is full
	{
		return count == 8;
	}
	void push(int n)// item n is pushed onto stack, throw exception on full stack.
	{
		if (isFull())
		{
			throw "The Stack Is Full";
		}
		newItem = newItem & 0x0F;
		items = items << 4;
		items = items | newItem;
		count++;
	}
	void pop() // removes top item from stack, throw exception on empty stack.
	{
		if (isEmpty())
		{
			throw "The Stack Is Empty";
		}
		else items = items >> 4;
	    count--;
	}
	int top() // returns top of stack, throw exception on empty stack.
	{
		if (isEmpty())
		{
			throw "The Stack Is Empty";
		}
		return items & 0x0F;
	}
	void printStack() // prints stack, stack is unchanged
	{
		unsigned int copy = items;

		for (int i=0; i < count; i++)
		{
			cout << (copy & 0x0F);
			copy = copy >> 4;
		}

	}
private:
	unsigned int newItem;
	unsigned int items;
	int count;
	int copy;
 


};


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
#include<iostream>
#include"stack.h"
using namespace std;


int main()
{
	Stack strange;
	int choice = 1;
	int num = 0;
	
	cout << " Strange stack " << endl;
	
	while(choice!=0)
	{
		
		cout << " 1=Push	2=Pop	3=Top	0=Quit " << endl;
		cin >> choice;
		try{
			if (choice == 1)
			{
				cout << " Enter A Number 0-15 "<<endl;
				cin >> num;
				if (num>15||num<0)
				{
					num = 0;
				}
				strange.push(num);
				strange.printStack();
			}
			if (choice == 2)
			{
				strange.top();
			 cout << " number popped " << num << endl;
			 strange.pop();
			 strange.printStack();
			}
			if (choice == 3)
			{
				strange.top();
			}
		}
		catch (char* str)
		{
			cout << str;
		}
		if (choice == 0)
		{
			cout << "Bye" << endl;
			return 0;
		}
	}

Have you learned of arrays? You must have, because you're using a class.

Anyway, it's conventional to use an array to hold the elements. You seem to be doing some weird shift thing. You're not storing bits your storing ints.
now I'm kind of confused sorry so I need to create an array to hold the values of the user number inputs and cycle through them in the for loop
I'm not suppose to use an array now I'm completely lost
via link list u can do
There is a warning on line 26:

warning: 'strange.Stack::newItem' may be used uninitialized in this function

in push() you do not use n at all.
I was just looking at that lol. I changed it to
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
 void push(newItem)// item n is pushed onto stack, throw exception on full stack.
	{
		if (isFull())
		{
			throw "The Stack Is Full";
		}
		newItem = newItem & 0x0F;
		items = items << 4;
		items = items | newItem;
		count++;
	}
	void pop() // removes top item from stack, throw exception on empty stack.
	{
		if (isEmpty())
		{
			throw "The Stack Is Empty";
		}
		else items = items >> 4;
	    count--;} 

but now im getting a logic error with my pop function when I push 321 on to the stack and I pop it off 3 times it just says 1 was popped off instead of popped number 1 popped number 2 popped number 3
On line 34 you show num which is irrelevant. Change lines 33/34:

cout << " number popped " << strange.top()<< endl;
I believe Line 1 of your corrected code ArtisticMess should be the following instead
1
2
3
4
5
6
7
8
9
10
void push(int newItem)// item n is pushed onto stack, throw exception on full stack.
	{
		if (isFull())
		{
			throw "The Stack Is Full";
		}
		newItem = newItem & 0x0F;
		items = items << 4;
		items = items | newItem;
		count++;
Topic archived. No new replies allowed.