コマンドプロンプトでコードを実行する際に問題が発生しました。エラーは表示されませんが、コードを実行すると何も起こりません。コードでエラーは発生しませんが、コマンドプロンプトに何も表示されません
#include <iostream>
#include <stdexcept>
using namespace std;
//defines the maximum queue size
#define MAX_QUE_SIZE 10
//creates the "rules" for the queue
class queue {
private:
int A[MAX_QUE_SIZE];
int front;
int rear;
public:
queue() {
front = -1;
rear = -1;
}
//checks to see if the queue is empty
bool isEmpty() {
return (front == -1 && rear == -1);
}
//checks to see if the queue if full
bool isFull() {
return (rear + 1) % MAX_QUE_SIZE == front ? true : false;
}
//checks to see if the queue is full, if not then it adds to the queue.
//if so it gives an error message.
void enqueue(int element) {
if (isFull()) {
throw std::overflow_error("QUEUE FULL");
}
if (isEmpty()) {
front = 0;
rear = 0;
}
else {
rear = (rear + 1) % MAX_QUE_SIZE;
}
A[rear] = element;
}
//checks to see if the queue is empty, if not then it deletes from the queue
//if sos it gives an error message.
void dequeue() {
if (isEmpty()) {
throw std::underflow_error("QUEUE EMPTY");
}
else if (front == rear) {
rear = -1;
front = -1;
}
else {
front = (front + 1) % MAX_QUE_SIZE;
}
}
//checks to see if the queue is empty, if so it gives a message saying so
//if not then it prints all the items in the queue
void printqueue() {
if (isEmpty()) {
cout << "EMPTY QUEUE";
}
else {
int count = (rear + MAX_QUE_SIZE - front) % MAX_QUE_SIZE + 1;
cout << "Queue : ";
for (int i = 0; i < count; i++) {
int index = (front + i) % MAX_QUE_SIZE;
cout << A[index] << " ";
}
cout << "\n\n";
}
}
};
int main()
{
queue Q; // creating an instance of Queue.
int i;
int k = 0;
int x;
std::cout << "Please enter some integers (enter 0 to exit):\n";
//a do-while statement that adds to the queue
do {
std::cin >> i;
//tries to add to the queue, if the queue is full it gives and overflow error
try {
Q.enqueue(i);
}
catch (std::overflow_error e) {
std::cout << e.what() << endl;
}
} while (i != 0);
std::cout << endl;
Q.printqueue();
std:cout << "How many values do you want to dequeue:\n";
std::cin >> x;
cout << endl;
//a for loop that dequeues the number of items the user wants to delete
//try the foor loop and dequeue function, if the queue is empty then it gives an underflow error
try {
for (int k = 0; k < x; k++) {
Q.dequeue();
}
}
catch (std::underflow_error e) {
std::cout << e.what() << endl;
}
Q.printqueue();
return 0;
}
また、g ++ -o ehQue ehQue.cppを入力してコンパイルします。これがエラーを引き起こしているのか、コード自体がエラーを引き起こしているのかわかりません。どんなに助けても大丈夫です。
これは読みにくいです。あなたの書式設定/字下げで作業してください。 –
ここで入力 '1 2 3 4 5 0'でうまく動作しますが、終端の0もエンキューされます。 –
*エラーは発生しませんが、コードを実行しても何も起こりません。一歩前に戻ってコードを削除して、入力が確実に得られるようにしてください。次に、入力を取得できなくなるまでコードを追加します。しかし、それは疑問を投げかけます。このコードをすべて実行しないで一気に追加しました。 – PaulMcKenzie