大家好,我是C++初学者,下面是一个模仿堆栈的程序,不知道怎么处理输入异常,如开始选择操作时不规范输入,输入字母什么的就进入死循环,改怎么解决啊?
#include <iostream.h>
#include <stdio.h>
class Stack
{
public:
Stack();
~Stack();
void push(int i);
int pop();
private:
int *p;
int count;
};
Stack::Stack()
{
p=new int[1024];
count=1024;
}
Stack::~Stack()
{
delete p;
}
void Stack::push(int i)
{
count--;
if(count<0)
{
cout<<"The stack is empty"<<endl;
return;
}
else
{
*p=i;
p++;
}
}
int Stack::pop()
{
if(count>=1024)
{
cout<<"The stack is full"<<endl;
return (-1);
}
else
{
p--;
count++;
return(*p);
}
}
enum{ push=1,pop,exit};
void main()
{
Stack A;
int choise;
int i;
while(1)
{
cout<<"What do you want to do: 1:push, 2:pop 3:exit:"<<endl;
cin>>choise;
if(choise==push)
{
cout<<"Input the number you want to push"<<endl;
cin>>i;
A.push(i);
continue;
}
if(choise==pop)
{
if((A.pop())!=-1)
{
cout<<A.pop()<<endl;
continue;
}
else
{
continue;
}
}
if(choise==exit)
break;
else
{
cout<<"You input the wrong"<<endl;
continue;
}
}
}