2016-09-30 59 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; 
}