2017-12-06 13 views
-1

コマンドプロンプトでコードを実行する際に問題が発生しました。エラーは表示されませんが、コードを実行すると何も起こりません。コードでエラーは発生しませんが、コマンドプロンプトに何も表示されません

#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

ここで入力 '1 2 3 4 5 0'でうまく動作しますが、終端の0もエンキューされます。 –

+1

*エラーは発生しませんが、コードを実行しても何も起こりません。一歩前に戻ってコードを削除して、入力が確実に得られるようにしてください。次に、入力を取得できなくなるまでコードを追加します。しかし、それは疑問を投げかけます。このコードをすべて実行しないで一気に追加しました。 – PaulMcKenzie

答えて

1

コードを実行していないと思われます。コンパイルして実行します。

あなたがプログラム(それを実行していない)でコンパイルしている:コマンドはコンパイラである「GCC」にちょうど別名でなければなりませんプログラム「のG ++」を呼び出すこととして理解することができ

g++ -o ehQue ehQue.cpp 

を。これは、ソースコードを取得し、その後、実行可能バイナリ生成するためにリンクされたオブジェクトコードを生成する(プログラム)

-o ehQue 

は、出力ファイル名を指定するコマンド・パラメータです。コンパイラは提供されたファイルを受け取り、 "ehQue"という実行可能な実行可能ファイルを生成しようとします。

ehQue.cpp 

コンパイラに指定したソースコードです。 (あなたはG ++のコマンドを入力した)あなたの端末内

あなたは、また、次のようなコマンドを使用してプログラムを呼び出す必要があります:

./ehQue 

またはWindowsコマンド・プロンプトに特異的であるように:

ehQue 

あなたのプログラムが実行されているはずです。

(タンジェンシャル)ホイールを再発明する必要がない限り、CPPの定義する機能の1つは、標準仕様の一部である標準テンプレートライブラリ(STL)です。std::dequeを印刷したクラスにラップします機能をお勧めします。

関連する問題