代码没有错误,但命令提示符中没有任何内容

时间:2017-12-06 21:22:26

标签: c++ compiler-errors compilation

我无法让我的代码在命令提示符下运行,我没有错误但是当我运行代码时没有任何反应。

#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 个答案:

答案 0 :(得分:1)

我怀疑你只是没有执行你的代码。它编译并运行。

您正在编译程序(不执行它):

g++ -o ehQue ehQue.cpp

该命令可以理解为调用程序“g ++”,它应该只是编译器“gcc”的别名。它接受源代码并生成目标代码,然后将其链接以生成可执行的二进制文件(程序。)

-o ehQue

是命令参数,用于指定输出文件名。编译器将获取提供的文件并(尝试)生成名为“ehQue”的工作可执行文件。

ehQue.cpp

是您为编译器指定的源代码。

在您的终端(您输入g ++命令的位置)中,您还需要使用以下命令调用程序:

./ehQue

或者特定于Windows命令提示符:

ehQue

您应该在哪里找到您的程序。

(切向)除非您特别需要重新发明轮子,否则CPP定义功能之一是标准模板库(STL),它是核心规范的一部分......在类中包含std::deque建议您使用打印功能。