请教:当cin输入数据类型不匹配时,下次循环到cin时就不执行了??
程序作用:循环输入一整数,判断是否为质数,当输入“0”时退出。以下为原代码:
#include<iostream>
using namespace std;
int main()
{
int i;
int a;
while(1)
{ cout<<"please enter the number:[enter 0 to exit]"<<endl;
cout<<" a = ";
cin>>a;
cout<<a<<"rrrrrrrrrrrrrrrrrrr"<<endl;
//cout<<a<<endl; 调试输入小数时,系统把小数点截断后赋值给a,a还是整型
if(a==0) //输入“0”退出
break;
else if(a<0) //输入负数时,显示输入错误,等待重新输入
{
cout<<"please enter a number that bigger than 0:"<<endl;
continue;
}
else if(a==1) //把1作为特殊书处理
cout<<a<<" is not a sushu"<<endl;
else //判断是否为质数
{
for(i=2;i<=a/2;i++)
{ if( a%i==0)
{
cout<<a<<" is not a sushu."<<endl;
break;
}
}
if (i==(a/2+1))
cout<<a<<" is a sushu."<<endl;
}
}
return 0;
}
当输入为整数时,程序按预定的运行;当输入为带小数点的数时,cin>>a;就跳过不执行了;请问这是什么原因?当怎么解决这个问题?












