| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 427 人关注过本帖
标题:[求助]如何处理输入异常
只看楼主 加入收藏
yesxdf
Rank: 1
等 级:新手上路
帖 子:24
专家分:0
注 册:2006-6-1
收藏
 问题点数:0 回复次数:1 
[求助]如何处理输入异常

大家好,我是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;
}
}
}

搜索更多相关主题的帖子: 输入 
2006-10-25 15:50
飞在天上的鱼
Rank: 1
等 级:新手上路
帖 子:32
专家分:0
注 册:2005-11-6
收藏
得分:0 

这个彻程序的count使用的有问题!(把它当作出栈的计数器)
堆栈是后进先出的!你编的这个不合理!


2006-10-25 17:07
快速回复:[求助]如何处理输入异常
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.019011 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved