关于c++中cin的一个小问题。请大家帮我解答一下

来源:百度知道 编辑:UC知道 时间:2024/05/05 06:13:42
下面这个小程序中为什么我输入数字的时候程序没有问题,可以用switch捕捉的到。可以输入字母的时候却会死循环。请各位高手都来帮帮忙吧。不胜感激。

#include<iostream.h>
#include<conio.h>
#include<iomanip.h>
#include<stdlib.h>
#define DELAY 9999999

int menu(void);
void delay(void);

void main(void)
{

while(1){
switch(menu()){
case 0:{
cout<<"\nExecuting choice 1..."<<endl;
delay();
break;
}
case 13:{
cout<<"\nExecuting choice enter..."<<endl;
delay();
break;
}
case 65:{
cout<<"\nExecuting choice enter..."<<endl;
delay();
break;
}
default:{
cout<<"\nInvalid choice. Try again..."<<endl;
delay();
break;
}
}

}
}
int menu(void)
{
int select;
// c

我看了很久,还是没看出来有问题呀,郁闷.

select 定义成 char select

我也不是很清楚,不过,我简单调试发现,当输入字母是,select被赋值为0,在此后的死循环中,它被赋以858993460。
这原因我就不知道了。
你可以对输入之进行一次判断,如果是字母就让重新输入,从而避免这种情况。