如何调试我的C ++队列代码?

时间:2015-08-21 02:37:22

标签: c++

我正在尝试创建用于实现队列的代码,但在第五次输入之后,发生了错误。我该怎么办?

这是我实现队列的代码:

#include<iostream>
  #include<queue>
  #include<string>

  using namespace std;


    int capacity = 10, front=0,back = 0;
    string x;
    string Q[10];

     class LapyTopy{
    public:
    void enqueue(string);
    void dequeue();
    void show();

    };
//function to add into queue
    void LapyTopy :: enqueue(string x) 
   {
   int newback = (back + 1) % capacity;

   if(newback!= front){
    Q[back] = x;
    back = newback + 1;
    }

   else
    cout << "***Queue is Full! " << endl;
   }

   void LapyTopy :: dequeue() //function to remove from a queue
   {
   if(front!= back) {
    front = (front + 1) % capacity;
   }

   else
    cout << "***Queue is empty! " << endl;

   }
//function to display queue
   void LapyTopy :: show() 
   {
   for(int i = front; i != back; i = (i + 1) % capacity){
    cout << Q[i] <<" ";
   }
     cout << endl;
   }

  int main() // Is the problem here?
 {
  LapyTopy j;
  string brand[capacity];

  cout << "Please enter a few brand names: " << endl;

  for(int i = 0; i < capacity; i++){
    cin >> brand[i];
    x = brand[i];
  j.enqueue(x);``
  cout << "The queue so far: " << endl;
    j.show();

 }
    j.dequeue();
 return 0;
 }

您认为问题在哪里?

1 个答案:

答案 0 :(得分:1)

将行back = newBack + 1更改为back = newBack。你只是加了两次。