cin发生了什么事?它无限循环

时间:2016-09-30 08:57:24

标签: c++

我最近编写了一些基本的学习内容:使用动态内存分配进行排队。对于你们中的一些人来说,阅读这样一个新手级别的代码会很痛苦,但是测试一下,我发现了一些有趣的东西。

我让我的程序向用户询问队列的初始容量。为了测试发生了什么,我键入了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;
} 

1 个答案:

答案 0 :(得分: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; 
}