2016-09-30 32 views
0

我最近編寫了一些基本的學習內容:使用動態內存分配的隊列。我會爲你們中的某些人閱讀這樣的新手級代碼而感到痛苦,但是通過測試,我發現了一些有趣的東西。cin發生了什麼?它走向無限循環

我讓我的程序向用戶詢問隊列的初始容量。爲了測試發生了什麼,我輸入了A而不是整數值。

Welcome! this program provides queue with dynamic memory allocation. 
what size of the queue would you want for the starting? minimum would be 100. (type integer) 
A 

(按回車,然後...)

To enqueue, type E and # of elements want to type (e.g.E5) 
To dequeue, type D (e.g.D5) 
To quit, type Q 

queue capacity is now 100 

上述被無限印刷

什麼實際發生的?

主要功能:

#include <iostream> 
using namespace std; 
#include "./Queue2.h" 
int main(){ 
    int initial_size; 
    cout << "Welcome! this program provides queue with dynamic memory allocation." << endl; 
    cout << "what size of the queue would you want for the starting? minimum would be 100. (type integer)" << endl; 
    cin >> initial_size; 
    initial_size=(initial_size>=100)? initial_size:100; 
    Queue Q = Queue(initial_size); //constructor initialized the queue of 100 rooms with head=0 and tail=0 

    char x='\0'; // user input initializing 
    do{ // until we get the instructed input, ask repeatedly. 
    cout << "To enqueue, type E and # of elements want to type (e.g.E5)" << endl; 
    cout << "To dequeue, type D (e.g.D5)" << endl; 
    cout << "To quit, type Q" << endl; 
    cout << endl; 
    cout << "queue capacity is now " << Q.getcap() << endl; 
    cin >> x; 
    if(x=='Q'){ 
     cout << "Bye Bye" << endl; 
     break; 
    } 
    else if(x=='E'){ 
     int numofq, a; 
     cin >> numofq; 
     for (int i=1; i<=numofq; i++){ 
     cin >> a; 
     Q.enqueue(a); 
     } 
    } 
    else if(x=='D'){ 
     int numofdeq; 
     cin >> numofdeq; 
     for (int i=1; i<=numofdeq; i++){ 
     cout << Q.dequeue() << endl; 
     } 
    } 
}while(1); 

return 0; 
} 

頁眉:

//1.class declaration with different member variables 
class Queue{ 
private: 
    int* dataptr; 
    int head; 
    int tail; 
    int capacity; 
public: 
    Queue(int cap); // constructor 
    Queue(int a[], int n); 
    ~Queue(); // destructor 
    int gethead(); // get head value 
    int gettail(); // get tail val 
    int getcap(); // get capacity 
    void enqueue(int x); 
    //puts some # into queue 
    int dequeue(void); 
    //returns the element at the head of the queue 
    bool isEmpty(void); 
    //returns TRUE if queue is empty. 
}; 
int Queue::gethead(){ 
    return head; 
} 
int Queue::gettail(){ 
    return tail; 
} 
int Queue::getcap(){ 
    return capacity; 
} 
void Queue::enqueue(int x){ 
    if(tail>=capacity){ //in case tail==capacity, queue is already full 
    capacity *=2; 
    int* newptr = new int[capacity]; 
    for(int i=0; i<tail; i++){ 
     newptr[i]=dataptr[i]; 
    } 
    delete []dataptr; 
    dataptr=newptr; 
    } 
    dataptr[tail++]=x; 
    return; 
} 
int Queue::dequeue(void){ 
    int y; 
    if(head<tail){ 
    y=dataptr[head++]; 
    } 
    else{ 
    cout << "error!: nothing to dequeue(queue is empty)"<< endl; 
    y=-1; 
    } 
    return y; 
} 
bool Queue::isEmpty(void){ 
    return (head==tail); 
} 
Queue::Queue(int cap){ 
    head=0; 
    tail=0; 
    capacity=(cap>0)? cap:100; 
    dataptr=new int[capacity]; 
} 
//3.function overloading: another constructor 
Queue::Queue(int a[], int n){ 
    head=0; 
    tail=0; 
    capacity=(2*n>=100)? 2*n:100; 
    int* ptr= new int[capacity]; 
    for(int i=0;i<n;i++){ 
    dataptr[i]=a[i]; 
    tail++; 
    } 
} 
Queue::~Queue(){ 
    delete []dataptr; 
} 
+0

你忘記了當用戶輸入一些輸入,例如一個數字時,用戶按下回車鍵結束輸入。該回車鍵作爲換行符放入流的輸入緩衝區中。所以在閱讀你的角色時,它會閱讀換行符而不是下一個字符,並且會出現問題。在循環迭代之前,您需要[忽略](http://en.cppreference.com/w/cpp/io/basic_istream/ignore)輸入的其餘部分。 –

+0

@JoachimPileborg:代碼仍然被破壞。想象一下輸入流結束了...... –

+0

看起來像你做的事,而(1)將永遠是'1'。 – ouflak

回答

1

您正在嘗試從流讀取一個字符到一個整數。 cin >> initial_size;得到錯誤的輸入-failbit被設置,並且cin不被清除。要清除它,您可以撥打clear()方法。我認爲這個代碼可以與limits庫一起使用來克服。

while(!(cin >> initial_size)) 
{ 
    cin.clear(); 
    cin.ignore(numeric_limits<streamsize>::max(), '\n'); 
    cout << "Invalid input. Try again: " << endl; 
}