0

This is the method. Define a queue overflow exception and modify enqueue so that it throws this exception when the queue runs out of space.

this is my code:

void IntQueue::enqueue(int num)
{

    if (isFull())
        cout << "The queue is full.\n";
    else
    {
        // Calculate the new rear position
        rear = (rear + 1) % queueSize;
        // Insert new item
        queueArray[rear] = num;
        // Update item count
        numItems++;
    }
}

how can i insert an exception message here?

tadman
  • 208,517
  • 23
  • 234
  • 262

2 Answers2

0

You should replace the call to cout << by a

throw std::runtime_error("queue full");
hivert
  • 10,579
  • 3
  • 31
  • 56
0

Assuming that you are required to define your own exception type then something like the following

#include <stdexcept>

struct QueueException : public std::runtime_error
{
    QueueException(const char* msg) : std::runtime_error(msg) {}
};

void IntQueue::enqueue(int num)
{
    if (isFull())
        throw QueueException("queue full");
    else
        ...
}

If you don't want to define your own exception type then just throw a std::runtime_error as hivert showed.

john
  • 85,011
  • 4
  • 57
  • 81