Endless loop and I don't understand why. Help.

Hi. So I wrote an algorithm that has to measure exactly 4 liters using 2 buckets of 8 liters(bucket1) and 5 liters(bucket2). The problem is when I run the program it goes into an infinite loop and I can't understand why. In few words, this is what should happen in the main() function: while( bucket1 OR bucket2 doesn't contain 4 liters), while (bucket2 is empty AND bucket1 is not full) fill bucket1 and cout bucket1.topLevel and bucket2.topLevel, then transfer everything from bucket2 into bucket1, then cout topLevels again. Then there is an if(bucket1 is full) empty it and print topLevels of both. And another if(bucket2 is not empty) transfer everything from bucket2 to bucket1 and print topLevels. This algorithm should go on until bucket2 finally reaches 4 liters. Here are the header file and main file below. Thanks.
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
89
90
91
92
93
94
95
96
97
98
99
THIS IS THE HEADER FILS mystack.h

#include <iostream>
using namespace std;

template<typename T, int N> 
class Stack {
    private:
		T stackArray[N]; 
    public:
    	int topLevel;
        void push(T x);
        int isEmpty();
        T pop();
		T empty();      
	  T peek();
	  Stack();
	 ~Stack();
	 void fill(T x);
	 T getTopLevel();
	 int isFull();
};

template<typename T, int N>
void Stack<T, N>::push(T x) {
	if (topLevel >= N - 1) 
	 {
		cout<<"The stack is full!\n";
                	return;
        	}
		stackArray[++topLevel] = x;
  }

template<typename T, int N>
int Stack<T, N>::isEmpty() {
	return (topLevel < 0); 
}

template<typename T, int N>
int Stack<T, N>::isFull() {
	return (topLevel = N - 1); 
}

template<typename T, int N>
T Stack<T, N>::pop() {
	if (isEmpty()) {
	          	cout<<"The stack is empty! \n";
               	T x;
                	return x;
            }					
            return stackArray[topLevel--];
}

template<typename T, int N>
T Stack<T, N>::peek() {
	       if (isEmpty()) {
		cout<<"The stack is empty! \n";
	               T x;
	               return x;
        	}	
          return stackArray[topLevel]; 
}
		
template<typename T, int N>
Stack<T, N>::Stack() { // constructor
	        topLevel = -1; 
}

template<typename T, int N>
Stack<T, N>::~Stack() { // destructor
}

template<typename T, int N>
void Stack<T, N>::fill(T x){
		while (topLevel <N-1) {
  		stackArray[++topLevel] = x;
}
}

template<typename T, int N>
T Stack<T, N>::empty(){
		while (isEmpty()) {
  		return stackArray[topLevel--];
}
}

template<typename T, int N, int M>
void transfer(Stack<T, N>& st, Stack<T, M>& other) {
    while ((!st.isEmpty())||(!other.isFull())) {
        other.push(st.peek());
        st.pop();
    }
}

template<typename T, int N>
T Stack<T, N>::getTopLevel() {
	    	int x = topLevel;
	    	return x+1;
    }

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
THIS IS THE MAIN FUNCTION

#include <iostream>
#include "mystack.h"
using namespace std;
int main()
{
	Stack<int, 8> bucket1;
	Stack<int, 5> bucket2;
	Stack<int, 20> barrel;
	
	
	
	while((bucket1.getTopLevel() != 4)||(bucket2.getTopLevel()!=4)){
		while((bucket2.isEmpty())&&(bucket1.isFull())){
		bucket2.fill(1);
		cout<<"bucket1: "<<bucket1.getTopLevel()<<" bucket2: "<<bucket2.getTopLevel()<<endl;
		transfer(bucket2, bucket1);
		cout<<"bucket1: "<<bucket1.getTopLevel()<<" bucket2: "<<bucket2.getTopLevel()<<endl;
		}
		
		if(bucket1.isFull()){
			bucket1.empty();
			cout<<"bucket1: "<<bucket1.getTopLevel()<<" bucket2: "<<bucket2.getTopLevel()<<endl;
		}
		if(!bucket2.isEmpty()){
			transfer(bucket2, bucket1);
			cout<<"bucket1: "<<bucket1.getTopLevel()<<" bucket2: "<<bucket2.getTopLevel()<<endl;
		}}
		
}
This loop will not stop until BOTH bucket1.getTopLevel() and bucket2.getTopLevel() returns 4.

 
while((bucket1.getTopLevel() != 4)||(bucket2.getTopLevel()!=4)){
Ohhhhhh! Right. Thanks!
Topic archived. No new replies allowed.