Verifying input , queues, recursion.

Long day, I can't seem to figure this out.
Why is this code not marking that the queue is full.. I'm just including my Add function that verifies if they want to add another number to the queue. The isFull function works fine, have used it on other programs.

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
template <class T> 	//Template currSize function
int Queue<T> :: currSize ()
{
	return (rear - front + arraylength) % arraylength; //
}

void addTo(Queue <int> &nums)
{
	char ans;	//Holds input from user
	int input;
	cout << "Add a number? (Y/N): ";
	cin >> ans;
	
	while (ans != 'Y' && ans != 'y' && ans != 'n' && ans != 'N')
	{
		cout << "\nNot a valid response, try again: ";
		cin >> ans;
	}
	
	if (ans == 'N')
	{
		return;
	}
	else if ((ans == 'Y' || ans == 'y') && !nums.isFull())
	{
		cout << "\nWhat number?: ";
		cin >> input;
		nums.add(input);
		cout << "\n" << 5-nums.currSize() << " spots left! ";
		addTo(nums);
	}
	else if (nums.isFull())
	{
		cout << "\nSorry the queue is already full!";
		return;
	}
		
}


The output goes all the way down to 1 spot left, lets the user enter the last element. Then it asks if they want to add another.. At this point, when they hit Y, it lets them add it and it says there are 5 spots left!

Why?

Thanks.
Is Queue a wrapper for an std container?
queue is implemented as an array... it's a circular queue though.
Any help here?
> Why is this code not marking that the queue is full
> The isFull function works fine
If the queue is full, but `isFull()' does not reflect that, then I would say that `isFull()' does not work fine.

┬┐How do you differentiate an empty queue from a full queue?
This is the IsFull fct..

1
2
3
4
5
6
7
8
9
10
11
12
template <class T>	//Template isFull function
int Queue<T> :: isFull()
{
	if ((rear + 1) % arraylength == front)	//if next spot is the front, we are full
	{
		return 1;
	}
	else
	{
		return 0;
	}
}
Actually here's it all.

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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
//This program prints out the reverse order of a queue
#include <iostream>
#include <cctype>
#include <cstdlib>
using namespace std;

template <class T> //Template for a queue
class Queue	//This is a queue class
{
	public:
		~ Queue (); // destructor for queue
		Queue(int);	//default constuctor, passed a int variable for size
		T getFront();	//returns a COPY of the front element
		void add (T element);	// adds element to the queue
		T deleteIt ();	//deletes an element from the queue
		T getBack ();	//returns a COPY of the back element
		int isEmpty();	//checks if queue is empty
		int currSize();	//returns the current size of the queue
		int isFull();	//checks if queue is full
		
	private:
		int front;	//one ahead of the front element
		int rear;	//position of the back element
		int arraylength;	//queue capacity
		T *q_ptr;	//pointer holds a spot in memory
};

template <class T>	//Template constructor
Queue<T> :: ~ Queue() //passed maxsize from initial declaration of stack
{
	delete [] q_ptr;	//deletes the queue
}

template <class T>	//Template constructor
Queue<T> :: Queue(int maxsize) //passed maxsize from initial declaration of queue
{
	arraylength = maxsize;	//sets private length variable equal to size passed from declaration at main
	front = 0;	//front and rear are initally -1 because nothing is in queue
	rear = 0;
	q_ptr = new T[arraylength];	//dynamically allocates memory to where pointer was stationed
}

template <class T> //Template add function
void Queue<T> :: add (T element)	//passed an element t add to queueu
{
	if (!isFull())	//if isFull function is not true (not full)
	{
		rear = (rear + 1) % arraylength;	//rear goes to next available spot
		q_ptr [rear] = element;	//add the element to this open spot
	}
	else
	{
			cout << "\nQueue is full";
	}
}

template <class T> //Template delete function
T Queue<T> :: deleteIt ()
{
	if (!isEmpty())	//if isEmpty function is not true (not empty)
	{
		front = (front+1) % arraylength; 	//front increments to next spot
											// if it hits the rear, rebounds to zero again
		return q_ptr[front];	//returns the front of the array
		q_ptr[front].~T();	//delete this spot in memory
	}
	else
	{
		cout << "\nQueue is empty!" << endl;
	}
	void add(T element);
}

template <class T> //Template getBack function
T Queue<T> :: getBack ()
{
	if (!isEmpty())
	{
		return q_ptr[rear];	//returns a copy of the rear
	}
	else
	{
		cout << "Queue is empty!" << endl;
	}
}

template <class T>	//Template getFront function
T Queue<T> :: getFront ()
{
	if (!isEmpty()) // if isEmpty function is not true (not empty)
	{
		return q_ptr[front+1]; // returns a COPY of the front element
	}
	else
	{
		cout << "Queue is empty!" << endl;
	}
}

template <class T>	//Template isEmpty function
int Queue<T> :: isEmpty()
{
	if (front == rear)
	{
		return 1;
	}
	else
	{
		return 0;
	}
}

template <class T>	//Template isFull function
int Queue<T> :: isFull()
{
	if ((rear + 1) % arraylength == front)	//if next spot is the front, we are full
	{
		return 1;
	}
	else
	{
		return 0;
	}
}

template <class T> 	//Template currSize function
int Queue<T> :: currSize ()
{
	return (rear - front + arraylength) % arraylength; //
}

void addTo(Queue <int> &nums)
{
	char ans;	//Holds input from user
	int input;
	
	cout << "Add a number? (Y/N): ";
	cin >> ans;
	
	while (ans != 'Y' && ans != 'y' && ans != 'n' && ans != 'N')
	{
		cout << "\nNot a valid response, try again: ";
		cin >> ans;
	}
	
	if (ans == 'N' || ans == 'n')
	{
		return;
	}
	else if ((ans == 'Y' || ans == 'y') && !nums.isFull())
	{
		cout << "\nWhat number?: ";
		cin >> input;
		nums.add(input);
		cout << "\n" << 5-nums.currSize() << " spots left! ";
		addTo(nums);
	}
	else if (nums.isFull())
	{
		cout << "\nSorry the queue is already full!";
		return;
	}

		
}

int main()
{
	Queue <int> nums(5);	//declares a queue of ten spot
	char ans;
	int input;
		
	addTo(nums);
	
     return 0;
}


It has to do with the indexes I think...the front is set to -1, but when it reaches the last spot, the isFull fct takes the last spot, and you try to do the next it does ....

1
2
3
4
5
6
7
8
9
10
{
	if ((rear + 1) % arraylength == front)	//if next spot is the front, we are full
	{
		return 1;
	}
	else
	{
		return 0;
	}
}


(4+1) % 5.... equals 0, so basically the rear is now 0 and the front is -1 still..not matching up, thus not realizing it is full..Playing around with the indexes and can't figure it out.
2 hours still can't get this right..any help?
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
//This program prints out the reverse order of a queue
#include <iostream>
#include <cctype>
#include <cstdlib>
using namespace std;

template <class T> //Template for a queue
class Queue	//This is a queue class
{
public:
   ~ Queue (); // destructor for queue
   Queue(int);	//default constuctor, passed a int variable for size
   T getFront();	//returns a COPY of the front element
   void add (T element);	// adds element to the queue
   T deleteIt ();	//deletes an element from the queue
   T getBack ();	//returns a COPY of the back element
   int isEmpty();	//checks if queue is empty
   int currSize();	//returns the current size of the queue
   int isFull();	//checks if queue is full
   void clear();
   friend void addTo(Queue <int> &);
   friend void Print(Queue <int> &);
   
private:
   int front;	//one ahead of the front element
   int rear;	//position of the back element
   int arraylength;	//queue capacity
   T *q_ptr;	//pointer holds a spot in memory
};

template <class T>	//Template constructor
Queue<T> :: ~ Queue() //passed maxsize from initial declaration of stack
{
   delete [] q_ptr;	//deletes the queue
}

template <class T>	//Template constructor
Queue<T> :: Queue(int maxsize) //passed maxsize from initial declaration of queue
{
   arraylength = maxsize;	//sets private length variable equal to size passed from declaration at main
   front = 0;	//front and rear are initally -1 because nothing is in queue
   rear = 0;
   q_ptr = new T[arraylength];	//dynamically allocates memory to where pointer was stationed
}

void Print(Queue <int>& vads)
{
   int k(vads.rear);
   while (!vads.isEmpty())
   {
      cout << vads.getBack() << " ";
      vads.rear--;
   }
   vads.rear = k;
   cout << "\n";
}

template <class T> //Template add function
void Queue<T> :: add (T element)	//passed an element t add to queueu
{
   q_ptr [rear++ % arraylength] = element;	//add the element to this open spot
}

template <class T> //Template delete function
T Queue<T> :: deleteIt ()
{
   rear = front % arraylength;
   delete [] q_ptr;
}

template <class T> //Template getBack function
T Queue<T> :: getBack ()
{
   return q_ptr[rear-1];	//returns a copy of the rear
}

template <class T>	//Template getFront function
T Queue<T> :: getFront ()
{
   return q_ptr[front]; // returns a COPY of the front element
}

template <class T>	//Template isEmpty function
int Queue<T> :: isEmpty()
{
   return (front == rear);
}

template <class T>	//Template isFull function
int Queue<T> :: isFull()
{
   return (!isEmpty() and (rear % arraylength == front));	//if next spot is the front, we are full
}

template <class T> 	//Template currSize function
int Queue<T> :: currSize ()
{
   return (rear - front); //
}

void addTo(Queue <int> &nums)
{
   char ans;	//Holds input from user
   int input;
   
   if (nums.isFull())
      return;
   
   cout <<"\n" << nums.arraylength-nums.currSize() << " spots left! ";
   cout << "Add a number? (Y/N): ";
   cin >> ans;
   cin.ignore();
   
   while ((ans | 0x20) != 'y' and (ans | 0x20) != 'n')
   {
      cout << "\nNot a valid response, try again: ";
      cin >> ans;
      cin.ignore();
   }
   
   if ( (ans | 0x20) != 'n' )
   {
      cout << "\nWhat number?: ";
      cin >> input;
      cin.ignore();
      nums.add(input);
      addTo(nums);
   }
   
}

int main()
{
   int input;
   cout << "Enter number of elements: ";
   cin.ignore();
   cin >> input;
   Queue <int> *nums(new Queue <int>(input));	//declares a queue of ten spot
   addTo(*nums);
   
   cout << "\nPrinting the list starting at back to front\n";
   Print(*nums);
   
   return 0;
}
Last edited on
Topic archived. No new replies allowed.