cin怎么了?它无限循环
What's happening with cin? it goes infinite loop
我最近编写了一些基本的东西来学习:队列使用动态内存分配。对于你们中的一些人来说,阅读这样一个新手级别的代码会很痛苦,但是在测试这个代码时,我发现了一些有趣的事情。
我编写的程序向用户询问队列的初始容量。为了测试会发生什么,我输入了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
(push enter then…)
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=' '; // 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++
- 过载'operator new'如何导致无限循环?
- C++正则表达式无限循环
- 程序在尝试猜测它选择的随机数时进入无限循环?
- WinUsb_WritePipe进入了一个无限循环
- 从二进制文件加载列表后,我的代码似乎进入了无限循环
- 为什么我的埃拉托色尼代码筛子无限循环.我已经用几个数字进行了测试
- 为什么嵌入类别的类别继承了外部类时没有无限循环
- 我做错了什么,导致无限循环
- Tcl_CreateInterp怎么会变成一个无限循环呢
- 如果输入了非整数,如何防止无限循环
- 这个递归函数(回文)变成了一个无限循环
- 数独求解矩阵,而语句给出了一个无限循环
- 为什么我在do while语句中得到了无限循环
- 代码完整,但创建了一个无限循环
- 陷入无限循环,该怎么做
- 为什么下面的验证代码卡住了?当我输入1时忽略.输入w,进入一个无限循环
- cin怎么了?它无限循环
- c++链表打印…我得到了一个无限循环
- Ifstream停留在一个单词上,创建了一个无限循环