c++ - cin怎么了?它进入无限循环

标签 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;
} 

最佳答案

您正在尝试将流中的一个字符读入一个整数。 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; 
}

关于c++ - cin怎么了?它进入无限循环,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39787547/

相关文章:

c++ - 由 C++ 编译器优化时,F2C 翻译的代码会中断

c++ - 将一个结构写成二进制,其中一个成员是一个字符串?

c++ - BOOST_SCOPE_EXIT 在幕后做了什么?

c++ - const 变量的初始化

c++ - N 位环绕的整数减法

c++ - 尝试使用 C++ 在 Linux 上执行命令时被阻止

c++ - QTextDocument:获取特定页面的内容

c++ - 寻找合数

C++ 类的对象是否可以直接访问父类(super class)的公共(public)变量

c++ - 哪些编译器对 C++0x 的当前状态有最先进的支持?